【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 2638|回复: 3

如何移动一个cocos2d-x精灵

[复制链接]
发表于 2013-8-8 14:10:51 | 显示全部楼层 |阅读模式
void addTarget()函数将会帮我们完成这一工作,敌人将会以随机的速度,从游戏场景左移动到右。
在HelloWorldScence.h里声明void addTarget(),并在HelloWorldScene.cpp里添加以下的代码,(请不要忘记在HelloWorldScene.cpp的开头加入using namespace cocos2d)
1// cpp with cocos2d-x
2void HelloWorld::addTarget()
3{
4 CCSprite *target = CCSprite::spriteWithFile("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::actionWithDuration( (ccTime)actualDuration,
34 ccp(0 - target->getContentSize().width/2, actualY) );
35 CCFiniteTimeAction* actionMoveDone =
36 CCCallFuncN::actionWithTarget( this,
37 callfuncN_selector(HelloWorld::spriteMoveFinished));
38 target->runAction( CCSequence::actions(actionMove,
39 actionMoveDone, NULL) );
40}
1// objc with cocos2d-iphone
2-(void)addTarget
3{
4 CCSprite *target = [CCSprite spriteWithFile"Target.png"
5 rect:CGRectMake(0, 0, 27, 40)];
6
7 // Determine where to spawn the target along the Y axis
8 CGSize winSize = [[CCDirector sharedDirector] winSize];
9 int minY = target.contentSize.height/2;
10 int maxY = winSize.height - target.contentSize.height/2;
11 int rangeY = maxY - minY;
12
13 int actualY = (arc4random() % rangeY) + minY;
14
15 // Create the target slightly off-screen along the right edge,
16 // and along a random position along the Y axis as calculated
17 target.position =
18 ccp(winSize.width + (target.contentSize.width/2),
19 actualY);
20 [self addChild:target];
21
22 // Determine speed of the target
23 int minDuration = 2.0;
24 int maxDuration = 4.0;
25 int rangeDuration = maxDuration - minDuration;
26
27 int actualDuration = (arc4random() % rangeDuration)
28 + minDuration;
29
30 // Create the actions
31 id actionMove =
32 [CCMoveTo actionWithDuration:actualDuration
33 position:ccp(-target.contentSize.width/2, actualY)];
34 id actionMoveDone =
35 [CCCallFuncN actionWithTarget:self
36 selectorselector(spriteMoveFinished];
37 [target runAction:[CCSequence actions:actionMove,
38 actionMoveDone, nil]];
39}
这里用callfuncN_selector(HelloWorld::spriteMoveFinished)回调了spriteMoveFinished方法,我们需要在HelloWorldScene.h里声明并如下来定义它,
1// cpp with cocos2d-x
2void HelloWorld::spriteMoveFinished(CCNode* sender)
3{
4 CCSprite *sprite = (CCSprite *)sender;
5 this->removeChild(sprite, true);
6}
1// objc with cocos2d-iphone
2-(void)spriteMoveFinishedid)sender
3{
4 CCSprite *sprite = (CCSprite *)sender;
5 [self removeChild:sprite cleanup:YES];
6}
要点
1. 关于随机函数。srand和rand是C标准库函数。对于每一个平台来说,你可以先获取毫秒级时间来得到一个随机数。在沃Phone上,这个函数是TimGetTickes(),而在iPhone上,你可以直接通过arc4random()函数来获得随机数。
2. Objc中的YES和NO,在cpp中变为true和false。
3. 回调函数,在objc中用selectorselector(spriteMoveFinished),但在cpp中实现就比较复杂了,你可以参考cocos2dx\include\selector_protocol.h里的声明。一共有5种回调函数类型
 schedule_selector
 callfunc_selector
 callfuncN_selector
 callfuncND_selector
 menu_selector
如何使用它们,根据所用函数的定义来决定。比如使用CCTimer::initWithTarget函数,它的第二个参数是SEL_SCHEDULE类型,到selector_protocol.h里查一下,可以看到对应的是schedule_selector(_SELECTOR)宏,所以调用时就需要在类里头实现一个void MyClass::MyCallbackFuncName(ccTime)函数,然后把schedule_selector(MyClass::MyCallbackFuncName)作为CCTimer::initWithTarget的第二个参数传入。
之后,我们应该定时地为游戏加入敌人,把以下代码加入到init()函数的返回值前。
1// cpp with cocos2d-x
2// Call game logic about every second
3this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 );
1// objc with cocos2d-iphone
2// Call game logic about every second
3[self scheduleselector(gameLogic interval:1.0];
然后在HelloWorldScence.cpp里实现gameLogic()。请注意gameLogic()应该声明为pubilc,否则是无法回调的。
1// cpp with cocos2d-x
2void HelloWorld::gameLogic(ccTime dt)
3{
4 this->addTarget();
5}
1// objc with cocos2d-iphone
2-(void)gameLogicccTime)dt
3{
4 [self addTarget];
5}
好了,所有事情都做完了,编译并运行,好好享用你的成果。
iPhone
Android
沃Phone
Win32
发表于 2014-4-19 13:53:41 | 显示全部楼层
学习了不错,很好
发表于 2014-4-25 23:37:20 | 显示全部楼层
干休楼主分享
发表于 2014-8-23 21:29:05 | 显示全部楼层
学到了,,楼主加油。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-11-23 21:38

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表