http://www.eoeandroid.com/forum.php?mod=viewthread&tid=250529

http://www.cocos2d-x.org/boards/6/topics/10055

Chapter 3 - How to Move a sprite

We have added a hero to the scene in the last chapter Chapter 2 - How to Add a sprite. But the hero is so lonely that we should add some enemies for him to beat down.
The function void addTarget() will complete the work, the enemies will be added into the scene from right to left at random speed.

Declare void addTarget() in HelloWorldScene.h and add the following code to HelloWorldScene.cpp, (and don't forget to add using namespace cocos2d; at the start of HelloWorldScene.cpp)

1// cpp with cocos2d-x
2void HelloWorld::addTarget()
3{
4 CCSprite *target = CCSprite::create("Target.png",
5 CCRectMake(0,0,27,40) );
6
7 // Determine where to spawn the target along the Y axis
8 CCSize winSize = CCDirector::sharedDirector()->getWinSize();
9 int minY = target->getContentSize().height/2;
10 int maxY = winSize.height
11 - target->getContentSize().height/2;
12 int rangeY = maxY - minY;
13 // srand( TimGetTicks() );
14 int actualY = ( rand() % rangeY ) + minY;
15
16 // Create the target slightly off-screen along the right edge,
17 // and along a random position along the Y axis as calculated
18 target->setPosition(
19 ccp(winSize.width + (target->getContentSize().width/2),
20 actualY) );
21 this->addChild(target);
22
23 // Determine speed of the target
24 int minDuration = (int)2.0;
25 int maxDuration = (int)4.0;
26 int rangeDuration = maxDuration - minDuration;
27 // srand( TimGetTicks() );
28 int actualDuration = ( rand() % rangeDuration )
29 + minDuration;
30
31 // Create the actions
32 CCFiniteTimeAction* actionMove =
33 CCMoveTo::create( (float)actualDuration,
34 ccp(0 - target->getContentSize().width/2, actualY) );
35 CCFiniteTimeAction* actionMoveDone =
36 CCCallFuncN::create( this,
37 callfuncN_selector(HelloWorld::spriteMoveFinished));
38 target->runAction( CCSequence::create(actionMove,
39 actionMoveDone, NULL) );
40}

Here, callfuncN_selector(HelloWorld::spriteMoveFinished) backcalls the function spriteMoveFinished(), we need to declare it in the HelloWorldScene.h and define it as follows,
1// cpp with cocos2d-x
2void HelloWorld::spriteMoveFinished(CCNode* sender)
3{
4 CCSprite *sprite = (CCSprite *)sender;
5 this->removeChild(sprite, true);
6}


TIPs:
1. About rand function. srand and rand are c std function. For each platform, you could get the mili-second system time as sand to get a random number. On IPhone, you could get the random number by arc4random() directly

2. The YES and NO in objc are true and false in cpp

3. The callback function is selector:@selector(spriteMoveFinished) in objc, but it is a little complicated to implement in cpp, you could refer to the declarations in cocos2dx\include\selector_protocol.h. There are five different callback types:

  • schedule_selector
  • callfunc_selector
  • callfuncN_selector
  • callfuncND_selector
  • menu_selector

How to use them is according to the callback function definition. For example, when use the function CCTimer::initWithTarget whose second parameter is a type of SEL_SCHEDULE, we could find its macro-definition schedule_selector(_SELECTOR) in selector_protocol.h, then we declare a callback function void MyClass::MyCallbackFuncName(float), and transform it as the second parameter of CCTimer::initWithTarget.

Then, we should put the enemies into the scene at intervals, add the codes before init() function returns.

1// cpp with cocos2d-x
2// Call game logic about every second
3this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 );

and implement gameLogic() in HelloWorldScene.cpp. Notice that gameLogic() should be declared as public, otherwise it won't be backcalled.

1// cpp with cocos2d-x
2void HelloWorld::gameLogic(float dt)
3{
4 this->addTarget();
5}

Ok, everything is done, build and run, and enjoy your fruit.
Win32

最新文章

  1. 构建高可用ZooKeeper集群
  2. python基础补漏-06-其他常用模块
  3. ubuntu中的Wine详解
  4. Jenkins-测试自动化(实例1-RF)
  5. HTTP 错误 405.0 - Method Not Allowed
  6. 【HDOJ】3560 Graph’s Cycle Component
  7. ubuntu进入命令登录界面
  8. Geodatabase - 修改字段别名(Field Alias)
  9. JavaScript2谁刚开始学习应该知道4最佳实践文章(翻译)
  10. 面向对象重写(override)与重载(overload)区别
  11. c语言清屏、等待、随机函数
  12. Flutter上拉加载下拉刷新---flutter_easyrefresh
  13. Effective Java 第三版——46. 优先考虑流中无副作用的函数
  14. RabbitMQ 主题
  15. 开例外!微信小程序登录绕过CAS单点登录(SSO)认证检查
  16. sql server单个字段列转行由,隔开
  17. 简单3D翻页相册制作教程
  18. RIGHT-BICEP单元测试——“二柱子四则运算升级版”
  19. spring 4.0+quartz2.2 实现持久化
  20. Spring.net页面属性注入

热门文章

  1. cf 219D
  2. eclipse+maven搭建cxf webservice 完整例子
  3. Tabhost嵌套以及Tab中多个Activity跳转的实现
  4. Cocos2d—X游戏开发之CCScrollView(滑动视图)(十二)
  5. logstahs 匹配isslog
  6. 【HDOJ】1072 Nightmare
  7. wcf中netTcpBinding的元素构成
  8. React入门2
  9. 老的acm & oj学习站点
  10. 【转】基于Ubuntu 14.04 LTS编译Android4.4.2源代码