//添加飛(fēi)機(jī)
auto spPlane = Sprite::create();
spPlane->setTag(110);
this->addChild(spP☆™αlane);
//飛(fēi)機(jī)動起來(lái) 。
Vector<SpriteFrame*&¶ ☆gt; allframe;//保存所有(yǒu)幀
for(int i=0;i<2;i++)∑®{
SpriteFrame * sf = SpriteFrame::c®♠reate("plane.png",Rect(i*₹®60,0,60,75));//截取圖片的(de)一(yī)部分(fēn)
allframe.pushBack(sf);
}
Animation * ani = Animation::c©≤βreateWithSpriteFramesλ☆ (allframe,0.1);
spPlane->runAction(RepeatFo↑<₩rever::create(Animate::create(ani)));&↑
//添加觸摸事(shì)件(jiàn) 控制(zhì)飛(fēi<≤♥€)機(jī)移動
EventListenerTouchOneByOne * ↕§→event = EventListenerTouchOneByΩ₽™×One::create();
event->setSwallowTouches(true);
event->onTouchBegan= CC_CALLBACK_2♦¶(GameScene::onTouchBeg®←☆an,this);
event->onTouchMoved= ©₽CC_CALLBACK_2(GameScene::onTouchMoved™≤,this);
event->onTouchEnded= CC_C" ALLBACK_2(GameScene::onTouchεσβ$Ended,this);
_eventDispatcher->a♦¶§£ddEventListenerWithSc↕φαeneGraphPriority(event,this); £ > //注冊到(dào)消息宣&→♥發器(qì)中。
需要(yào)添加 。飛(fēi)機(jī)移©"↕γ動的(de)三個(gè)狀态的(de)函數(shù)∏←∏π。
/* 移動飛(fēi)機(jī)函數(βshù) */
bool GameScene:•∑→ :onTouchBegan(Touch *touch, Eveεγnt *unused_event){
px = touch->getLocatio≥±n().x; //獲得(de)$"'↔當前坐(zuò)标點x
py = touch->getLocati©σon().y; //獲得(de)當前坐(zuò)标點y
return true;
}
void GameScene::onTouchMoved(Touch∞> *touch, Event *unused_event){
int mx=touch->getLocation().✘≠x-px;
int my=touch->getLocation().y-py;
auto spPlane = this-"₹>getChildByTag(110);//λ ↓獲得(de)飛(fēi)機(jī) 。
spPlane->runAction(MoveBy::crea≤♠te(0,Point(mx,my)));//飛(fēi)機(jī)根據σπ移動的(de)坐(zuò)标量 &nbs ♥₹p;改變
px = touch->getLocatio←<n().x;
py = touch->getL♣λ ×ocation().y;
}
void GameScene::"λσonTouchEnded(Touch *touch, ≤♣Event *unused_event){
}