当前位置:编程学习 > wap >>

cocos2d-2 游戏中人物的移动

//让当前图层实现触摸
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);
}
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,