Cocos2d入门 <三>如何移动精灵角色
2013-01-16 16:23
405 查看
void HelloWorld::addTarget() { CCSprite *target = CCSprite::create("Target.png",CCRectMake(0,0,27,40)); // Determine where to spawn the target along the Y axis CCSize 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 calculated target->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 actions CCFiniteTimeAction* 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-x void HelloWorld::spriteMoveFinished(CCNode* sender) { CCSprite *sprite = (CCSprite *)sender; this->removeChild(sprite, true); } // cpp with cocos2d-x void HelloWorld::gameLogic(float dt) { this->addTarget(); } // on "init" you need to initialize your instance bool HelloWorld::init() { ...... // Call game logic about every second this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 ); return true; }
相关文章推荐
- Cocos2d入门 <二> 添加一个精灵
- cocos2d-x基础<三> 播放动画
- Cocos2d入门 <一>建立一个新工程
- Cocos2d入门 <五> 碰撞检测
- <转+备份>Cocos2d-x精灵加亮及灰度调整
- Cocos2d入门 <四> fire bullet
- Cocos2d入门 <六> 播放声音
- Cocos2d入门 <七> 游戏结束
- Git的入门教程<三>
- 如何实现具有层次结构的 TreeView <三> (WPF/TreeView/Style/Template)
- quick-cocos2d-x的热更新机制实现<三>Updater(C++)
- 如何把List<T>转成XML操作、增删改查
- SPSS Clementine 数据挖掘入门<转>
- java面试知识之ssh<三>
- 3D打印机切片引擎CuraEngine源码分析<三> 系列目录
- Android入门:向TextView添加滚动条(添加<ScrollView>标签)
- 用jQuery实现<select>选项上下移动
- redis入门——redis常用命令<转>
- 如何使用 <fstream> 类进行文件的 I/O 处理(转)
- 一起学Maven(Maven的依赖管理特性)<三>