Cocos2dx For Multi-Platform: Chapter 3 - How to Move a sprite

This article is written by FlyingPacer and Walzer, all rights reserved.

(http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_3_-_How_to_Move_a_sprite)

We have add 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 to let him beat down.
The function void addTarget() will complete the work, the enemies will be add into the scene from right to left at random speed.

Declare void addTarget() in HelloWorldScene.h and add the following code to HelloWorldScene.cpp,

 1 // cpp with cocos2d-x
2 void 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 - target->getContentSize().height/2;
11 int rangeY = maxY - minY;
12 // srand( TimGetTicks() );
13 int actualY = ( rand() % 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->setPosition(
18 ccp(winSize.width + (target->getContentSize().width/2),
19 actualY) );
20 this->addChild(target);
21
22 // Determine speed of the target
23 int minDuration = (int)2.0;
24 int maxDuration = (int)4.0;
25 int rangeDuration = maxDuration - minDuration;
26 // srand( TimGetTicks() );
27 int actualDuration = ( rand() % rangeDuration ) + minDuration;
28
29 // Create the actions
30 CCFiniteTimeAction* actionMove =
31 CCMoveTo::actionWithDuration( (ccTime)actualDuration,
32 ccp(0 - target->getContentSize().width/2, actualY) );
33 CCFiniteTimeAction* actionMoveDone =
34 CCCallFuncN::actionWithTarget( this,
35 callfuncN_selector(HelloWorld::spriteMoveFinished));
36 target->runAction( CCSequence::actions(actionMove,
37 actionMoveDone, NULL) );
38 }
 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) + minDuration;
28
29 // Create the actions
30 id actionMove =
31 [CCMoveTo actionWithDuration:actualDuration
32 position:ccp(-target.contentSize.width/2, actualY)];
33 id actionMoveDone =
34 [CCCallFuncN actionWithTarget:self
35 selector:@selector(spriteMoveFinished:)];
36 [target runAction:[CCSequence actions:actionMove,
37 actionMoveDone, nil]];
38 }

Here, callfuncN_selector(HelloWorld::spriteMoveFinished) backcalls the function spriteMoveFinished(), we need to declare it in the HelloWorldScene.h and define it as follows,


// cpp with cocos2d-x
void HelloWorld::spriteMoveFinished(CCNode* sender)
{
CCSprite *sprite = (CCSprite *)sender;
this->removeChild(sprite, true);
}

// objc with cocos2d-iphone
-(void)spriteMoveFinished:(id)sender
{
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];
}

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 WoPhone, the function is TimGetTickes(), and 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 realize 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(ccTime), 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.


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

// objc with cocos2d-iphone
// Call game logic about every second
[self schedule:@selector(gameLogic:) interval:1.0];

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


// cpp with cocos2d-x
void HelloWorld::gameLogic(ccTime dt)
{
this->addTarget();
}

// objc with cocos2d-iphone
-(void)gameLogic:(ccTime)dt
{
[self addTarget];
}

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

IPhone 220
Android 217
WoPhone 219
Win32 214



转载于:https://www.cnblogs.com/FlyingPacer/archive/2011/03/22/1991240.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值