偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

如何移動(dòng)一個(gè)cocos2d-x精靈

移動(dòng)開(kāi)發(fā) iOS Android 游戲開(kāi)發(fā)
在前一章中我們介紹了如何添加一個(gè)cocos2d-x精靈中,為游戲場(chǎng)景添加了一個(gè)精靈。但一個(gè)英雄或許太過(guò)孤單,我們應(yīng)該加入一些敵人,讓他來(lái)打敗。本文我們將講述如何移動(dòng)一個(gè)cocos2d-x精靈。

void addTarget()函數(shù)將會(huì)幫我們完成這一工作,敵人將會(huì)以隨機(jī)的速度,從游戲場(chǎng)景左移動(dòng)到右。

在HelloWorldScence.h里聲明void addTarget(),并在HelloWorldScene.cpp里添加以下的代碼,(請(qǐng)不要忘記在HelloWorldScene.cpp的開(kāi)頭加入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)回調(diào)了spriteMoveFinished方法,我們需要在HelloWorldScene.h里聲明并如下來(lái)定義它,

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}

要點(diǎn)

1. 關(guān)于隨機(jī)函數(shù)。srand和rand是C標(biāo)準(zhǔn)庫(kù)函數(shù)。對(duì)于每一個(gè)平臺(tái)來(lái)說(shuō),你可以先獲取毫秒級(jí)時(shí)間來(lái)得到一個(gè)隨機(jī)數(shù)。在沃Phone上,這個(gè)函數(shù)是TimGetTickes(),而在iPhone上,你可以直接通過(guò)arc4random()函數(shù)來(lái)獲得隨機(jī)數(shù)。

2. Objc中的YES和NO,在cpp中變?yōu)閠rue和false。

3. 回調(diào)函數(shù),在objc中用selector:@selector(spriteMoveFinished),但在cpp中實(shí)現(xiàn)就比較復(fù)雜了,你可以參考cocos2dx\include\selector_protocol.h里的聲明。一共有5種回調(diào)函數(shù)類(lèi)型

 schedule_selector

 callfunc_selector

 callfuncN_selector

 callfuncND_selector

 menu_selector

如何使用它們,根據(jù)所用函數(shù)的定義來(lái)決定。比如使用CCTimer::initWithTarget函數(shù),它的第二個(gè)參數(shù)是SEL_SCHEDULE類(lèi)型,到selector_protocol.h里查一下,可以看到對(duì)應(yīng)的是schedule_selector(_SELECTOR)宏,所以調(diào)用時(shí)就需要在類(lèi)里頭實(shí)現(xiàn)一個(gè)void MyClass::MyCallbackFuncName(ccTime)函數(shù),然后把schedule_selector(MyClass::MyCallbackFuncName)作為CCTimer::initWithTarget的第二個(gè)參數(shù)傳入。

之后,我們應(yīng)該定時(shí)地為游戲加入敵人,把以下代碼加入到init()函數(shù)的返回值前。

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里實(shí)現(xiàn)gameLogic()。請(qǐng)注意gameLogic()應(yīng)該聲明為pubilc,否則是無(wú)法回調(diào)的。

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}

好了,所有事情都做完了,編譯并運(yùn)行,好好享用你的成果。

iPhone

Android

沃Phone

Win32

責(zé)任編輯:佚名 來(lái)源: cocos2d-x
相關(guān)推薦

2012-04-17 10:06:08

cocos2d-x

2011-12-12 10:40:08

Cocos2d-X游戲開(kāi)發(fā)開(kāi)發(fā)環(huán)境

2012-04-17 12:44:38

cocos2d-x

2012-04-17 12:38:46

cocos2d-x

2013-05-22 15:49:46

2013-04-16 10:02:47

cocos2d-x懶人Android開(kāi)發(fā)

2012-04-17 09:30:45

cocos2d-x創(chuàng)建

2012-04-17 12:47:27

cocos2d-x

2014-07-31 16:57:30

2012-04-17 13:12:00

2013-12-03 10:58:50

Cocos2D-X磚塊地圖

2013-05-22 14:38:44

iOS開(kāi)發(fā)Cocos2d-x坐標(biāo)系統(tǒng)

2012-04-17 12:58:44

Cocos2D-X

2013-06-03 17:04:20

CocoStudioCocos2D-X添加CocoStudi

2012-02-19 20:10:23

Cocos2d-x fCocos2dWindows Pho

2015-07-17 10:38:21

教程COCOS射箭游戲

2012-04-17 12:52:01

cocos2d-x

2013-11-13 16:31:32

Cocos2d-x

2013-06-07 14:06:52

移動(dòng)開(kāi)發(fā)Android開(kāi)發(fā)cocos2d-x

2012-05-09 10:09:57

Cocos2d-xAndroidiOS
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)