Cocos2d-x 通过虚拟按键控制人物移动

玩过手机RPG的同学都知道,游戏中人物的移动一般都是通过触摸屏上的虚拟按键来实现,那么究竟是怎么实现的呢,我们来做一个简单的测试。
这里并没有按照其他Blog的实现来讲解,仅仅是通过我自己的想法。

为了方便起见,我并没有新建其他类,只是在新建程序的HelloWorld中来实现。


头文件:

	cocos2d::CCSprite* player;

	cocos2d::CCPoint ctrlPoint;

	virtual bool ccTouchBegan(cocos2d::CCTouch* pTouch, cocos2d::CCEvent* pEvent);
	virtual void ccTouchMoved(cocos2d::CCTouch* pTouch, cocos2d::CCEvent* pEvent);
	virtual void ccTouchEnded(cocos2d::CCTouch* pTouch, cocos2d::CCEvent* pEvent);

	cocos2d::CCAction* actionUp;
	cocos2d::CCAction* actionDown;
	cocos2d::CCAction* actionLeft;
	cocos2d::CCAction* actionRight;

首先我们利用一张拼合的角色图片来展示人物行走的动画


CCTexture2D* playerTexture = CCTextureCache::sharedTextureCache()->addImage("Player.png");

//新建四个数组,分别用来存放上下左右四个动画

		CCArray* framesUp = CCArray::create();
		CCArray* framesDown = CCArray::create();
		CCArray* framesLeft = CCArray::create();
		CCArray* framesRight = CCArray::create();

//通过循环将帧存入创建好的数组里

		CCSpriteFrame* frame = NULL;

		CCSpriteFrame* frameOriginal = NULL;

		for(int i = 0; i < 4; i ++)
		{
			for(int j = 0; j < 4; j ++)
			{
				frame = CCSpriteFrame::createWithTexture(playerTexture, CCRectMake(70 * j, 124 * i, 70, 124));
				if(i == 0 && j == 0)
				{
					frameOriginal = frame;
				}
				if(i == 0)
				{
					framesDown->addObject(frame);
				}else if(i == 1)
				{
					framesLeft->addObject(frame);
				}else if(i == 2)
				{
					framesRight->addObject(frame);
				}else if(i == 3)
				{
					framesUp->addObject(frame);
				}
			}
		}

//通过数组创建帧序列
		CCAnimation *animitionUp = CCAnimation::createWithSpriteFrames(framesUp, 0.1f);
		CCAnimation *animitionDown = CCAnimation::createWithSpriteFrames(framesDown, 0.1f);
		CCAnimation *animitionLeft = CCAnimation::createWithSpriteFrames(framesLeft, 0.1f);
		CCAnimation *animitionRight = CCAnimation::createWithSpriteFrames(framesRight, 0.1f);

//通过帧序列动画和移动创建上人物下左右移动动作
		actionUp = CCSpawn::createWithTwoActions(CCAnimate::create(animitionUp), CCMoveBy::create(0.4f, ccp(0, 32)));
		actionDown = CCSpawn::createWithTwoActions(CCAnimate::create(animitionDown), CCMoveBy::create(0.4f, ccp(0, -32)));
		actionLeft = CCSpawn::createWithTwoActions(CCAnimate::create(animitionLeft), CCMoveBy::create(0.4f, ccp(-32, 0)));
		actionRight = CCSpawn::createWithTwoActions(CCAnimate::create(animitionRight), CCMoveBy::create(0.4f, ccp(32, 0)));

		actionUp->retain();
		actionDown->retain();
		actionLeft->retain();
		actionRight->retain();

//将第一帧用来创建人物精灵
		player = CCSprite::createWithSpriteFrame(frameOriginal);

		CCSize winSize = CCDirector::sharedDirector()->getWinSize();

		this->addChild(player, 1);
		player->setPosition(ccp(winSize.width / 2, winSize.height / 2));



//处理触摸事件
bool HelloWorld::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent)
{
	CCPoint location = this->convertTouchToNodeSpace(pTouch);
	int x = location.x - ctrlPoint.x;
	int y = location.y - ctrlPoint.y;

//根据触摸到控制杆的不同位置向不同的方向移动,画个直角坐标系就出来了
	if(y > x && y > -x)
	{
		player->runAction(actionUp);
	}
	else if(-y > x && -y > -x)
	{
		player->runAction(actionDown);
	}
	else if(-x > y && -x > -y)
	{
		player->runAction(actionLeft);
	}
	else if(x > y && x > -y)
	{
		player->runAction(actionRight);
	}
	return true;
}



这个就是运行的最终效果图了,背景可以自己添加,通过点击那块黑乎乎的控制杆就能移动人物了


你可能感兴趣的:(cocos2d-x)