自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

cocos2d-x如何發(fā)射子彈

移動開發(fā) iOS Android 游戲開發(fā)
現(xiàn)在,我們想讓英雄能發(fā)射子彈來消滅敵人,添加以下的代碼,來設(shè)置圖層支持觸摸。

先上代碼

1// cpp with cocos2d-x

2this->setIsTouchEnabled(true);

1// objc with cocos2d-iphone

2self.isTouchEnabled = YES;

這樣我們就能接受到touch event的了。

在HelloWorldScene.h里聲明回調(diào)函數(shù)“void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);”,并在HelloWorldScene.cpp實(shí)現(xiàn)這個函數(shù)。

1// cpp with cocos2d-x

2void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)

3{

4 // Choose one of the touches to work with

5 CCTouch* touch = (CCTouch*)( touches->anyObject() );

6 CCPoint location = touch->locationInView(touch->view());

7 location = CCDirector::sharedDirector()->convertToGL(location);

8

9 // Set up initial location of projectile

10 CCSize winSize = CCDirector::sharedDirector()->getWinSize();

11 CCSprite *projectile = CCSprite::spriteWithFile("Projectile.png",

12 CCRectMake(0, 0, 20, 20));

13 projectile->setPosition( ccp(20, winSize.height/2) );

14

15 // Determinie offset of location to projectile

16 int offX = location.x - projectile->getPosition().x;

17 int offY = location.y - projectile->getPosition().y;

18

19 // Bail out if we are shooting down or backwards

20 if (offX <= 0) return;

21

22 // Ok to add now - we've double checked position

23 this->addChild(projectile);

24

25 // Determine where we wish to shoot the projectile to

26 int realX = winSize.width

27 + (projectile->getContentSize().width/2);

28 float ratio = (float)offY / (float)offX;

29 int realY = (realX * ratio) + projectile->getPosition().y;

30 CCPoint realDest = ccp(realX, realY);

31

32 // Determine the length of how far we're shooting

33 int offRealX = realX - projectile->getPosition().x;

34 int offRealY = realY - projectile->getPosition().y;

35 float length = sqrtf((offRealX * offRealX)

36 + (offRealY*offRealY));

37 float velocity = 480/1; // 480pixels/1sec

38 float realMoveDuration = length/velocity;

39

40 // Move projectile to actual endpoint

41 projectile->runAction( CCSequence::actions(

42 CCMoveTo::actionWithDuration(realMoveDuration, realDest),

43 CCCallFuncN::actionWithTarget(this,

44

45 callfuncN_selector(HelloWorld::spriteMoveFinished)),

46 NULL) );

47}

1// objc with cocos2d-iphone

2- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent* event)

3{

4 // Choose one of the touches to work with

5 UITouch *touch = [touches anyObject];

6 CGPoint location = [touch locationInView:[touch view]];

7 location = [[CCDirector sharedDirector] convertToGL:location];

8

9 // Set up initial location of projectile

10 CGSize winSize = [[CCDirector sharedDirector] winSize];

11 CCSprite *projectile = [CCSprite spriteWithFile:@"Projectile.png"

12 rect:CGRectMake(0, 0, 20, 20)];

13 projectile.position = ccp(20, winSize.height/2);

14

15 // Determine offset of location to projectile

16 int offX = location.x - projectile.position.x;

17 int offY = location.y - projectile.position.y;

18

19 // Bail out if we are shooting down or backwards

20 if (offX <= 0) return;

21

22 // Ok to add now - we've double checked position

23 [self addChild:projectile];

24

25 // Determine where we wish to shoot the projectile to

26 int realX = winSize.width + (projectile.contentSize.width/2);

27 float ratio = (float) offY / (float) offX;

28 int realY = (realX * ratio) + projectile.position.y;

29 CGPoint realDest = ccp(realX, realY);

30

31 // Determine the length of how far we're shooting

32 int offRealX = realX - projectile.position.x;

33 int offRealY = realY - projectile.position.y;

34 float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));

35 float velocity = 480/1; // 480pixels/1sec

36 float realMoveDuration = length/velocity;

37

38 // Move projectile to actual endpoint

39 [projectile runAction:[CCSequence actions:

40 [CCMoveTo actionWithDuration:realMoveDuration position:realDest],

41 [CCCallFuncN actionWithTarget:self

42

43 selector:@selector(spriteMoveFinished:)],

44 nil]];

45}

好了,編譯并運(yùn)行,觸摸屏幕(使用的是模擬器?點(diǎn)擊屏幕!),享受一下效果。

PS: 為了和Object-C代碼保持一致,這里可能會有一些“float”到“int”隱式轉(zhuǎn)換導(dǎo)致的警告,請忽略它們。

Win32

iPhone

Android

沃Phone

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

2011-12-12 10:40:08

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

2012-04-17 12:44:38

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ā)

2013-05-22 14:38:44

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

2013-12-03 10:58:50

Cocos2D-X磚塊地圖

2012-04-17 12:58:44

Cocos2D-X

2013-06-03 17:04:20

CocoStudioCocos2D-X添加CocoStudi

2014-07-31 16:57:30

2012-02-19 20:10:23

Cocos2d-x fCocos2dWindows Pho

2014-08-13 10:07:02

游戲引擎

2012-04-17 13:12:00

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 09:30:45

cocos2d-x創(chuàng)建

2014-08-14 10:06:14

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

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