成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

cocos2d-x如何檢測碰撞

移動開發(fā) iOS Android 游戲開發(fā)
我們的英雄現(xiàn)在可以發(fā)射子彈了,但僅僅只是裝飾而已,如何才能殺掉怪物呢?在這一章,我們將介紹碰撞檢測來實(shí)現(xiàn)這一效果。

首先,跟蹤怪物和子彈是必需的。

在游戲中,我們?yōu)檫@兩種精靈加以不同的tag來區(qū)分它們。當(dāng)tag=1時(shí),表示這是怪物,而tag=2時(shí),則表示這為子彈。由于在CCNode里面有m_nTag這個(gè)成員變量,并且有setTag和getTag方法,因此CCSprite就繼承了這些方法,我們可以利用之。

在HelloWorldScene.h中,把以下兩個(gè)成員變量加入到HelloWorld下,這兩個(gè)成員變量用于緩存現(xiàn)有的怪物和子彈。

1// cpp with cocos2d-x

2protected:

3 cocos2d::CCMutableArray *_targets;

4 cocos2d::CCMutableArray *_projectiles;

1// objc with cocos2d-iphone

2 NSMutableArray *_targets;

3 NSMutableArray *_projectiles;

在cocos2d-x里,CCMutableArray相當(dāng)于iOS SDK中的NSMutableArray,該數(shù)組里的成員可以是NSObject或者他們的子類。但不同的是,你必須告訴它里面要放的是哪種具體的類型。

之后構(gòu)造函數(shù)中初始化這兩個(gè)變量,在init()中new 它們,并在析構(gòu)函數(shù)中release 它們。

1// cpp with cocos2d-x

2

3// in init()

4// Initialize arrays

5_targets = new CCMutableArray;

6_projectiles = new CCMutableArray;

7

8HelloWorld::~HelloWorld()

9{

10 if (_targets)

11 {

12 _targets->release();

13 _targets = NULL;

14 }

15

16 if (_projectiles)

17 {

18 _projectiles->release();

19 _projectiles = NULL;

20 }

21

22 // cpp don't need to call super dealloc

23 // virtual destructor will do this

24}

25

26HelloWorld::HelloWorld()

27:_targets(NULL)

28,_projectiles(NULL)

29{

30}

1// objc with cocos2d-iphone

2// in init()

3// Initialize arrays

4_targets = [[NSMutableArray alloc] init];

5_projectiles = [[NSMutableArray alloc] init];

6

7- (void) dealloc

8{

9 [_targets release];

10 _targets = nil;

11

12 [_projectiles release];

13 _projectiles = nil;

14

15 // don't forget to call "super dealloc"

16 [super dealloc];

17}

現(xiàn)在可以修改addTarget(),把新目標(biāo)添加到目標(biāo)數(shù)組中,并設(shè)置其tag為1。

1// cpp with cocos2d-x

2// Add to targets array

3target->setTag(1);

4_targets->addObject(target);

1// objc with cocos2d-iphone

2// Add to targets array

3target.tag = 1;

4[_targets addObject:target];

修改ccTouchesEnded(),把新子彈加入到子彈數(shù)組中,并設(shè)置其tag為2。

1// cpp with cocos2d-x

2// Add to projectiles array

3projectile->setTag(2);

4_projectiles->addObject(projectile);

1// objc with cocos2d-iphone

2// Add to projectiles array

3projectile.tag = 2;

4[_projectiles addObject: projectile];

之后,按下面修改spriteMoveFinished()。這里根據(jù)標(biāo)記的不同,在對應(yīng)的數(shù)組中移除精靈

1// cpp with cocos2d-x

2void HelloWorld::spriteMoveFinished(CCNode* sender)

3{

4 CCSprite *sprite = (CCSprite *)sender;

5 this->removeChild(sprite, true);

6

7 if (sprite->getTag() == 1) // target

8 {

9 _targets->removeObject(sprite);

10 }

11 else if (sprite->getTag() == 2) // projectile

12 {

13 _projectiles->removeObject(sprite);

14 }

15}

1// objc with cocos2d-iphone

2-(void)spriteMoveFinished:(id)sender

3{

4 CCSprite *sprite = (CCSprite *)sender;

5 [self removeChild:sprite cleanup:YES];

6

7 if (sprite.tag == 1) // target

8 {

9 [_targets removeObject:sprite];

10 }

11 else if (sprite.tag == 2) // projectile

12 {

13 [_projectiles removeObject:sprite];

14 }

15}

下面的update()函數(shù)用于檢測每幀的碰撞,并從游戲中刪除碰撞中的子彈和怪物。

請?jiān)贖elloWorldScene.h中聲明,在HelloWorldScene.cpp中定義。

1// cpp with cocos2d-x

2void HelloWorld::update(ccTime dt)

3{

4 CCMutableArray *projectilesToDelete =

5 new CCMutableArray;

6 CCMutableArray::CCMutableArrayIterator it, jt;

7

8 for (it = _projectiles->begin(); it != _projectiles->end(); it++)

9 {

10 CCSprite *projectile =*it;

11 CCRect projectileRect = CCRectMake(

12 projectile->getPosition().x

13 - (projectile->getContentSize().width/2),

14 projectile->getPosition().y

15 - (projectile->getContentSize().height/2),

16 projectile->getContentSize().width,

17 projectile->getContentSize().height);

18

19 CCMutableArray*targetsToDelete

20 = new CCMutableArray;

21

22 for (jt = _targets->begin(); jt != _targets->end(); jt++)

23 {

24 CCSprite *target =*jt;

25 CCRect targetRect = CCRectMake(

26 target->getPosition().x - (target->getContentSize().width/2),

27 target->getPosition().y - (target->getContentSize().height/2),

28 target->getContentSize().width,

29 target->getContentSize().height);

30

31 if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))

32 {

33 targetsToDelete->addObject(target);

34 }

35 }

36

37 for (jt = targetsToDelete->begin();

38 jt != targetsToDelete->end();

39 jt++)

40 {

41 CCSprite *target =*jt;

42 _targets->removeObject(target);

43 this->removeChild(target, true);

44 }

45

46 if (targetsToDelete->count() >0)

47 {

48 projectilesToDelete->addObject(projectile);

49 }

50 targetsToDelete->release();

51 }

52

53 for (it = projectilesToDelete->begin();

54 it != projectilesToDelete->end();

55 it++)

56 {

57 CCSprite* projectile =*it;

58 _projectiles->removeObject(projectile);

59 this->removeChild(projectile, true);

60 }

61 projectilesToDelete->release();

62}

1// objc with cocos2d-iphone

2- (void)update:(ccTime)dt

3{

4 NSMutableArray *projectilesToDelete

5 = [[NSMutableArray alloc] init];

6

7 for (CCSprite *projectile in _projectiles)

8 {

9

10 CGRect projectileRect = CGRectMake(

11 projectile.position.x - (projectile.contentSize.width/2),

12 projectile.position.y - (projectile.contentSize.height/2),

13 projectile.contentSize.width,

14 projectile.contentSize.height);

15

16 NSMutableArray *targetsToDelete

17 = [[NSMutableArray alloc] init];

18

19 for (CCSprite *target in _targets)

20 {

21

22 CGRect targetRect = CGRectMake(

23 target.position.x - (target.contentSize.width/2),

24 target.position.y - (target.contentSize.height/2),

25 target.contentSize.width,

26 target.contentSize.height);

27

28 if (CGRectIntersectsRect(projectileRect, targetRect))

29 {

30 [targetsToDelete addObject:target];

31 }

32 }

33

34 for (CCSprite *target in targetsToDelete)

35 {

36

37 [_targets removeObject:target];

38 [self removeChild:target cleanup:YES];

39 }

40

41 if (targetsToDelete.count >0)

42 {

43 [projectilesToDelete addObject:projectile];

44 }

45 [targetsToDelete release];

46 }

47

48 for (CCSprite *projectile in projectilesToDelete)

49 {

50

51 [_projectiles removeObject:projectile];

52 [self removeChild:projectile cleanup:YES];

53 }

54 [projectilesToDelete release];

55}

好了,最后一件事,我們要把update()加入到schedule里讓它每幀都能被調(diào)用。

1// cpp with cocos2d-x

2this->schedule( schedule_selector(HelloWorld::update) );

1// objc with cocos2d-iphone

2[self schedule:@selector(update:)];

編譯并運(yùn)行項(xiàng)目,盡情地發(fā)射子彈吧,這時(shí):啊哈,怪物都一個(gè)接著一個(gè)地被干掉了。

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

2011-12-12 10:40:08

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

2012-04-17 12:38:46

cocos2d-x

2013-05-22 15:49:46

2012-04-17 10:06:08

cocos2d-x

2012-04-17 12:47:27

cocos2d-x

2012-04-17 10:59:31

cocos2d-x

2013-04-16 10:02:47

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

2014-07-31 16:57:30

2013-06-03 17:04:20

CocoStudioCocos2D-X添加CocoStudi

2013-12-03 10:58:50

Cocos2D-X磚塊地圖

2013-05-22 14:38:44

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

2012-04-17 12:58:44

Cocos2D-X

2012-02-19 20:10:23

Cocos2d-x fCocos2dWindows Pho

2013-11-13 16:31:32

Cocos2d-x

2012-05-09 10:09:57

Cocos2d-xAndroidiOS

2013-06-07 14:06:52

移動開發(fā)Android開發(fā)cocos2d-x

2012-04-17 13:12:00

2014-08-13 10:07:02

游戲引擎

2012-04-17 09:30:45

cocos2d-x創(chuàng)建

2014-08-14 10:06:14

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

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

主站蜘蛛池模板: 亚洲一区中文字幕在线观看 | av电影一区二区 | 久久久久久久久久久高潮一区二区 | 国产亚洲网站 | 久久久亚洲成人 | 国产高清视频在线 | 亚洲视频一区 | 欧美中文字幕 | 日韩乱码一二三 | 狠狠操av | 亚洲乱码国产乱码精品精98午夜 | 国产视频91在线 | 久草视频观看 | 久久久久久国产 | 羞羞的视频免费看 | 亚洲人成人一区二区在线观看 | 亚洲深夜福利 | 亚洲毛片在线 | 国产乱码高清区二区三区在线 | 日本不卡一区二区三区 | 美女福利网站 | 午夜影院黄 | 成人精品鲁一区一区二区 | 国产日韩中文字幕 | 国产在线精品一区二区三区 | 久久综合狠狠综合久久综合88 | 超碰av人人 | 亚洲一区二区三区久久 | 欧美成视频| 中文字幕亚洲免费 | 国产日韩一区二区三免费 | 欧美一区二区三区在线观看视频 | 久久草视频 | 亚洲第一福利视频 | 免费亚洲成人 | 欧美三级电影在线播放 | 久久精品色欧美aⅴ一区二区 | 自拍偷拍中文字幕 | 日日操视频 | 亚洲国产高清高潮精品美女 | 9久9久9久女女女九九九一九 |