Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_2(2)

来源:未知 责任编辑:责任编辑 发表时间:2015-09-09 15:29 点击:
//根据角色的上下左右四个vector 创建四个方向的动作 Animation * player1_animation_left = Animation::createWithSpriteFrames(player1_anim_left_vector,0.1f); Animation * player1_animation_right = Animation::createWithSpriteFrames(player1_anim_right_vector,0.1f); Animation * player1_animation_down = Animation::createWithSpriteFrames(player1_anim_down_vector,0.1f); Animation * player1_animation_up = Animation::createWithSpriteFrames(player1_anim_up_vector,0.1f); Animation * player2_animation_left = Animation::createWithSpriteFrames(player2_anim_left_vector,0.1f); Animation * player2_animation_right = Animation::createWithSpriteFrames(player2_anim_right_vector,0.1f); Animation * player2_animation_down = Animation::createWithSpriteFrames(player2_anim_down_vector,0.1f); Animation * player2_animation_up = Animation::createWithSpriteFrames(player2_anim_up_vector,0.1f); ///根据角色的上下左右四个动作 创建四个方向的动画 player1_animate_left = Animate::create(player1_animation_left); player1_animate_right = Animate::create(player1_animation_right); player1_animate_down = Animate::create(player1_animation_down); player1_animate_up = Animate::create(player1_animation_up); player2_animate_left = Animate::create(player2_animation_left); player2_animate_right = Animate::create(player2_animation_right); player2_animate_down = Animate::create(player2_animation_down); player2_animate_up = Animate::create(player2_animation_up); }

 

2、 创建完角色需要的文件后,还需要角色在地图的位置,由于能走的路径都在way图层中,在setWayPassToGrid()方法中,我们把way图层中sprite的坐标保存到wayLayerPass_vector中,这样就可以根据其中的坐标设置角色的位置了

void  GameBaseScene::setWayPassToGrid()
{
	TMXLayer* wayLayer = _map->layerNamed(way);


	Size _mapSize = wayLayer->getLayerSize(); 
	for (int j = 0;  j < _mapSize.width; j++) {  
		for (int i = 0;  i < _mapSize.height; i++) {  
			Sprite* _sp = wayLayer->tileAt(Point(j, i));  
			if (_sp) 
			{  
				float x = _sp->getPositionX();
				float y = _sp->getPositionY();
				int col = x/tiledWidth;
				int row = y/tiledHeight;
				canPassGrid[row][col] = true;
				//取得该位置的坐标,保存到对象wayLayerPass_vector中
				Vec2 p = _sp->getPosition();
				wayLayerPass_vector.push_back(p);
				log(canPassGrid row=  %d ,col =%d ,canpass = %d ,row,col,canPassGrid[row][col]);
			}  

		}  
	}  
	log(setWayPassToGrid finished);
}

3、 添加的角色我们先封装成一个RicherPlayer类,该类记录角色的信息,包括角色名称、资金、体力、敌友

 

 

RicherPlayer* RicherPlayer::create(char* name,SpriteFrame* spriteFrame,bool enemy,int money,int strength)
{
	RicherPlayer* player = new RicherPlayer();
	player->init(name,spriteFrame, enemy,money,strength);
	player->autorelease();
	return player;

}


bool RicherPlayer::init(char* name,SpriteFrame* spriteFrame,bool enemy,int money,int strength)
{

	Sprite::initWithSpriteFrame(spriteFrame);
	_name = name;
	_enemy = enemy;
	_money = money;
	_strength = strength;
	return true;
}

	
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • cocos2d-x学习笔记(19)--label 、label atlas
  • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
  • Cocos2d-x 2.0 之 Actions “三板斧” 之一
  • Cocos2d-x学习(一):HelloWorld
  • cocos2d-x学习笔记(18)--游戏打包(windows平台)
  • cocos2d 设置屏幕默认方向
  • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
  • cocos2d-x学习笔记(15)--地图的使用(TMX)
  • cocos2dx在xcode下开发,编译到android上(2)
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1