cocos2d-x旅程開始--(实现单击与长按)-飞外

小菜鸟一枚,学习cocos2d-x已经有一段时间了,感觉进度非常慢那,CSDN也再次拾了起来。近期自己还在学习做小游戏,跟着前辈做了《忍者打怪物》的小游戏,又学习了瓦片游戏《吃西瓜》,打算自个做个坦克大战,刚刚起步,希望自己尽力的完毕那。开个帖子记录一下。

前段时间的进度:

了各种素材文件,用TileMap制作地图,这里出现了错误,眼下发现“砖块”和“铁块”必须放在不同的图层以下才干执行

否则就会出错,tmx 式的地图不能全然显示出来,要么仅仅显示砖头要么仅仅显示铁块。

然后写代码:去掉HelloWord其中init()函数中不必要的部分,自己開始写。

先导入地图

tank_war = CCTMXTiledMap::create( tank_war.tmx //增加地图
this- addChild(tank_war);

创建主角而且放到合适的位置

playerBornGroup = tank_war- objectGroupNamed( born //在地图中找到主角的图层
CCDictionary* playerPosition = playerBornGroup- objectNamed( player_1 //依据名称找到本图层的player_1位置
int x = playerPosition- valueForKey( x )- intValue();
int y = playerPosition- valueForKey( y )- intValue(); //得到object的坐标

player_1 = CCSprite::create( p1tank1.png //创建player精灵
player_1- setPosition(ccp(x, y));
this- addChild(player_1);

然后实现player_1的移动,本来使用的是CCMenuItemImage,发现仅仅能实现点击一次才干移动一次,不点击就不动了,不能实现按住button不停移动的状态,折腾了两天也搞不定,放弃之。

舍弃代码:

// CCMenuItemImage* moveLeft = CCMenuItemImage::create( arrow-leftx.png , arrow-left.png , this, menu_selector(HelloWorld::howToMoveL));
// moveLeft- setPosition(ccp(30,80));
// moveLeft- boundingBox();
//
// CCMenuItemImage* moveRight = CCMenuItemImage::create( arrow-rightx.png , arrow-right.png , this, menu_selector(HelloWorld::howToMoveR));
// moveRight- setPosition(ccp(120, 80));
//
// CCMenu* menu = CCMenu::create();
// menu- setPosition(origin);
// menu- addChild(moveLeft);menu- addChild(moveRight);
// this- addChild(menu);

// void HelloWorld::howToMoveR(CCObject* pMove)
// {
// player_1- setRotation(90); //cocos2d-x直接给了一个旋转精灵的函数,都用不到其它的图片了
// CCPoint origPo = player_1- getPosition();
// CCPoint newPo = origPo ccp(10, 0);
// newPo.x = newPo.x WIN_WIDTH - 30 ? newPo.x : WIN_WIDTH - 30;
// player_1- setPosition(newPo);
// return;
// }

今天的进度:

于是使用新的方法,不再用menu,将button中的normalImage与selectImage拿出来用Sprite实现,在CCTouchBegan、CCTouchMoved、CCTouchEnded中实现单击与长按,用schedule和update()函数实现player_1不停运动:

init()函数:

turnLeftx = CCSprite::create( arrow-leftx.png
turnLeftx- setPosition(ccp(30, 80));
this- addChild(turnLeftx);

boolleft = false; reallyMoved = false;

CCTouchBegan()函数:

bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

if (turnLeftx- boundingBox().containsPoint(pTouch- getLocation())) //点击处坐标在左转button区域中(这里末尾加分号的话,会导致点哪里都左移的情况)
{
turnLeft- setVisible(true); //背景button显现
turnLeftx- setVisible(false); //上层左转button消失
boolleft = true; //在update中推断运动方向
this- schedule(schedule_selector(HelloWorld::update),0.1f); //使用schedule每隔0.1秒运行一次update
}
if (reallyMoved == false) //假设运行了update,reallyMoved会设为true
{ //假设没运行,就实现点击一下就移动一次
player_1- setRotation(-90); //player_1图片左转90度
CCPoint origPo = player_1- getPosition(); //获得player_1原始的坐标
CCPoint newPo = origPo - ccp(10, 0); //设定新的坐标,左移10个像素
newPo.x = newPo.x 30 ? newPo.x : 30; //假设跑到屏幕边缘就动了,player_1为30*30
player_1- setPosition(newPo);
}
return true;

}

CCTouchEnded()函数:

void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) //点击结束时还原一些參数
{
this- unschedule(schedule_selector(HelloWorld::update)); //结束update
turnLeft- setVisible(false);
turnLeftx- setVisible(true);
boolleft = false;
return;
}

update()函数:

void HelloWorld::update(float alpha)
{
reallyMoved = true; //推断是否是长时间按住button(0.1秒)
if (boolleft==true)
{
player_1- setRotation(-90);
CCPoint origPo = player_1- getPosition();
CCPoint newPo = origPo - ccp(10, 0);
newPo.x = newPo.x 30 ? newPo.x : 30;
player_1- setPosition(newPo);
}
}

OK!最后要加上(用来实现点击):

void HelloWorld::onEnter()
{
CCLayer::onEnter();
CCDirector::sharedDirector()- getTouchDispatcher()- addTargetedDelegate(this, 0, true);
}
void HelloWorld::onExit()
{
CCDirector::sharedDirector()- getTouchDispatcher()- removeDelegate(this);
CCLayer::onExit();
}

实现单击,长按的方法有非常多,好像还能够用button,可惜捯饬了一下午也没实现。