Cocos2d入门 <三>如何移动精灵角色

来源:互联网 发布:w10无法安装软件 编辑:程序博客网 时间:2024/05/01 22:49

 

void HelloWorld::addTarget(){CCSprite *target = CCSprite::create("Target.png",CCRectMake(0,0,27,40));// Determine where to spawn the target along the Y axisCCSize winSize = CCDirector::sharedDirector()->getWinSize();int minY = target->getContentSize().height/2;int maxY = winSize.height -  target->getContentSize().height/2;int rangeY = maxY - minY;// srand( TimGetTicks() );int actualY = ( rand() % rangeY ) + minY;// Create the target slightly off-screen along the right edge,// and along a random position along the Y axis as calculatedtarget->setPosition(ccp(winSize.width + (target->getContentSize().width/2),actualY) );this->addChild(target);// Determine speed of the target    int minDuration = (int)2.0;int maxDuration = (int)4.0;int rangeDuration = maxDuration - minDuration;// srand( TimGetTicks() );int actualDuration = ( rand() % rangeDuration )+ minDuration;// Create the actionsCCFiniteTimeAction* actionMove =CCMoveTo::create( (float)actualDuration,ccp(0 - target->getContentSize().width/2, actualY) );CCFiniteTimeAction* actionMoveDone = CCCallFuncN::create( this, callfuncN_selector(HelloWorld::spriteMoveFinished));   target->runAction( CCSequence::create(actionMove, actionMoveDone, NULL) );}// cpp with cocos2d-xvoid HelloWorld::spriteMoveFinished(CCNode* sender){  CCSprite *sprite = (CCSprite *)sender;  this->removeChild(sprite, true);}// cpp with cocos2d-xvoid HelloWorld::gameLogic(float dt){    this->addTarget();}// on "init" you need to initialize your instancebool HelloWorld::init(){  ......  // Call game logic about every secondthis->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 );    return true;}

原创粉丝点击