小菜鳥一枚,學習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,發現只能實現點擊一次才能移動一次,不點擊就不動了,不能實現按住按鈕不停移動的狀態,折騰了兩天也搞不定,放棄之。
捨棄代碼:
// 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,將按鈕中的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())) //點擊處座標在左轉按鈕地區中(這裡末尾加分號的話,會導致點哪裡都左移的情況)
{
turnLeft->setVisible(true); //背景按鈕顯現
turnLeftx->setVisible(false); //上層左轉按鈕消失
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; //判斷是否是長時間按住按鈕(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,可惜捯飭了一下午也沒實現。