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

Chapter 3 - How to Move a sprite的更多相关文章

  1. Chapter 2 - How to Add a sprite

    Chapter 2 - How to Add a sprite 1. Add image resources 1.1add resources on win32 2. Add a sprite TIP ...

  2. Cocos引擎开发者指南(1-5)

    Cocos引擎开发者指南 英文原版:http://www.cocos2d-x.org/docs/programmers-guide/1/ 中午翻译:http://www.cocos.com/doc/t ...

  3. cocos2dx day 2 - Sprites

    1.Sprite 对sprite设置anchor point,对应的位置 // DEFAULT anchor point for all Sprites mySprite->setAnchorP ...

  4. iOS开发——UI篇OC篇&SpriteKit详解

    SpriteKit详解 SpriteKit,iOS/Mac游戏制作的新纪元 这是我的WWDC2013系列笔记中的一篇,完整的笔记列表请参看这篇总览.本文仅作为个人记录使用,也欢迎在许可协议范围内转载或 ...

  5. Cocos2d-x CCEditBox & CCTextFieldTTF

    下面简单记录一下如何Cocos2d-x中创建输入编辑框.在引擎中为我们提供了这样两个类:CCEditBox  和  CCTextFieldTTF. 一.CCEditBox ①这个类文件的位置 ②这个类 ...

  6. [RxJS] Drag and Drop example

    Improving our mouse drag event Our mouse drag event is a little too simple. Notice that when we drag ...

  7. 每天一本电子书 - JavaScript for Kids: A Playful Introduction to Programming

    JavaScript for Kids: A Playful Introduction to Programming 作者: Nick Morgan  出版社: No Starch Press 副标题 ...

  8. python pygame实现简单的网游

    此示例为简单的实现游戏服务器端和客户端的消息同步,使用自定定义协议,引入了twisted网络框架,还有诸多不足(其实就是半成品). 资源下载地址: http://download.csdn.net/d ...

  9. JQuery Notes

    <script type="text/javascript" src="script.js"></script> $(document) ...

随机推荐

  1. 引用Excel时 未在本地计算机上注册ace.oledb.12.0

    可能由于未安装数据库补丁 下载地址http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/A ...

  2. 第 10 章 建造者模式【Builder Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> 又是一个周三,快要下班了,老大突然又拉住我,喜滋滋的告诉我“牛叉公司很满意我们做的模型,又签订了一个合同,把奔驰.宝马的 ...

  3. 值得推荐的C/C++框架和库 very good

    [本文系外部转贴,原文地址:http://coolshell.info/c/c++/2014/12/13/c-open-project.htm]留作存档 下次造轮子前先看看现有的轮子吧 值得学习的C语 ...

  4. nyist 61 传纸条 nyist 712 探 寻 宝 藏(双线程dp问题)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=61 http://acm.nyist.net/JudgeOnline/problem.php?pid ...

  5. java的版本区别、下载、配置

    参考:http://blog.csdn.net/liujun13579/article/details/7684604 java的版本区别 常用的 java 程序分为  Java SE.Java EE ...

  6. Web安全测试之XSS(转)

    XSS 全称(Cross Site Scripting) 跨站脚本攻击, 是Web程序中最常见的漏洞.指攻击者在网页中嵌入客户端脚本(例如JavaScript), 当用户浏览此网页时,脚本就会在用户的 ...

  7. wpa_supplicant对wep,wpa,wpa2的psk和隐藏ap的scan_ssid扫描配置

    # 请不要修改下面这一行内容,否则将不能正常工作ctrl_interface=/var/run/wpa_supplicant # 确保只有root用户能读取WPA的配置ctrl_interface_g ...

  8. 算法 python实现(三) 快速排序

    算法学起来真费劲啊,智商只够捉只鸡的.昨晚没看明白就没电了,过两天要考虑偷电了... 今天看看快速排序,有一个博客写的很好,通俗生动形象,适合我这样的算法大白菜.推荐一下 http://www.cnb ...

  9. Javascript 查找字符串中出现最多的字符和出现的次数

    <script type="text/javascript"> //查找字符串中出现最多的字符和出现的次数 var str = 'Thatwheneying its o ...

  10. CodeFirst-数据迁移-Migration

    http://www.cnblogs.com/haogj/archive/2012/02/17/2356537.html 1.安装最新NuGet 2.安装EntityFramework:在程序包管理器 ...