cocos2dx 精灵的移动(2)
来源:未知 责任编辑:责任编辑 发表时间:2013-12-18 11:33 点击:次
p> this->addTarget();
p>}
p>
p>
p>
p>void HelloWorld::addTarget(){
p>
p> CCSize winSize = CCDirector::sharedDirector()->getWinSize();
p>
p> CCSprite *target = CCSprite::create("Target.png");
p>
p> //随机位置
p> int minY = target->getContentSize().height/2.0;
p> int maxY = winSize.height - target->getContentSize().height/2.0;
p> int rangeY = maxY - minY;
p> int actualY = rand()%rangeY + minY;
p>
p> target->setPosition(ccp(winSize.width - target->getContentSize().width/2.0, actualY));
p> this->addChild(target);
p>
p> //随机速度
p> float minDuration = 2.0;
p> float maxDuration = 4.0;
p> int rangeDuration = maxDuration - minDuration;
p> float actualDuration = rand()%rangeDuration + minDuration;
p>
p>
p>
p>
p> CCFiniteTimeAction *actionMove = CCMoveTo::create(actualDuration, ccp(0 - target->getContentSize().width/2.0, actualY));//0代表屏幕外,这句表示在3秒内从初始位置移动到屏幕外
p>
p>
p> //增加一个回调函数,回收移动到屏幕外的精灵
p> CCFiniteTimeAction *actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished));
p> target->runAction(CCSequence::create(actionMove,actionMoveDone,NULL));
p>
p>}
p>
p>void HelloWorld::spriteMoveFinished(cocos2d::CCNode *sender){
p> CCSprite *sprite = (CCSprite *)sender;
p> this->removeChild(sprite, true);
p>}
p>
p>
p>
p>
p>void HelloWorld::menuCloseCallback(CCObject* pSender)
p>{
p> CCDirector::sharedDirector()->end();
p>
p>#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
p> exit(0);
p>#endif
p>}
p>
p>运行结果如下:
p>
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>