//讓當前圖層實現觸摸
this->setTouchEnabled(true);
// 覆蓋cclayer中注冊觸摸分發器
voidFishGame::registerWithTouchDispatcher()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,INT_MIN,false);
}
voidFishGame::gamelogic(float t){
CCSprite * nowplayer=(CCSprite *)this->getChildByTag(999);
nowplayer->setPositionX(nowplayer->getPositionX()+20);
if (nowplayer->getPositionX()>500) {
nowplayer->setPositionX(0);//當人物走出屏幕時再回到原點
}
nowplayer->setPositionY(nowplayer->getPositionY()+20);
if (nowplayer->getPositionY()>300) {
nowplayer->setPositionY(50);
}
}
boolFishGame::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent){
CCPoint p=pTouch->getLocation();
CCSprite *nowplayer=(CCSprite *)this->getChildByTag(999);
//nowplayer->setPosition(p);
nowplayer->runAction(CCMoveTo::create(0.5, p));
CCLog("按下 %f,%f",p.x,p.y);
return true;
}
voidFishGame::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent){
CCPoint p=pTouch->getLocation();
CCSprite *nowplayer=(CCSprite *)this->getChildByTag(999);
nowplayer->setPosition(p);
CCLog("移動到 %f,%f",p.x,p.y);
}
voidFishGame::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent){
CCPoint p=pTouch->getLocation();
CCLog("抬起 %f,%f",p.x,p.y);
}