IOSSpriteAndEnemy  
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 selector:@selector(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)spriteMoveFinished:(id)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中用selector:@selector(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 schedule:@selector(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)gameLogic:(ccTime)dt

3{

4 [self addTarget];

5}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()