第四章——如何发射子弹
2013-01-02 11:43
357 查看
http://cn.cocos2d-x.org/resource/show?nid=66
现在,我们想让英雄能发射子弹来消灭敌人,添加以下的代码,来设置图层支持触摸。
1// cpp with cocos2d-x
2this->setIsTouchEnabled(true);
1// objc with cocos2d-iphone
2self.isTouchEnabled = YES;
这样我们就能接受到touch event的了。
在HelloWorldScene.h里声明回调函数“void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);”,并在HelloWorldScene.cpp实现这个函数。
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}
好了,编译并运行,触摸屏幕(使用的是模拟器?点击屏幕!),享受一下效果。
PS: 为了和Object-C代码保持一致,这里可能会有一些“float”到“int”隐式转换导致的警告,请忽略它们。
Win32
iPhone
Android
沃Phone
http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_4_-_How_to_Fire_some_Bullets
现在,我们想让英雄能发射子弹来消灭敌人,添加以下的代码,来设置图层支持触摸。
1// cpp with cocos2d-x
2this->setIsTouchEnabled(true);
1// objc with cocos2d-iphone
2self.isTouchEnabled = YES;
这样我们就能接受到touch event的了。
在HelloWorldScene.h里声明回调函数“void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);”,并在HelloWorldScene.cpp实现这个函数。
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}
好了,编译并运行,触摸屏幕(使用的是模拟器?点击屏幕!),享受一下效果。
PS: 为了和Object-C代码保持一致,这里可能会有一些“float”到“int”隐式转换导致的警告,请忽略它们。
Win32
iPhone
Android
沃Phone
http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_4_-_How_to_Fire_some_Bullets
相关文章推荐
- cocos2d-x SimpleGame(3)如何发射子弹
- Cocos2d-x《雷电大战》(3)-子弹无限发射
- 发射子弹 发射射线判断方法
- 第四章 Spring.Net 如何管理您的类___自定义对象行为
- web安全之SQL注入---第四章 如何进行SQL注入攻击
- 《Team Geek》 阅读笔记之 第四章 如何处理有害的人
- 第四章 Spring.Net 如何管理您的类___统一资源访问接口
- 第四章 Spring.Net 如何管理您的类___让对象了解自己的容器
- cocos2d-x入门(4)-英雄发射子弹和碰撞检测
- swing 飞机大战 三 自己飞机的移动和发射子弹
- 第四章 Spring.Net 如何管理您的类___对象、对象工厂和应用程序上下文
- 实现子弹, 可以指定发射位置, 发发射角度, 有方向, 可以反弹
- 【学习笔记】《卓有成效的管理者》 第四章 如何发挥人的长处(未完......)
- 第四章 Spring.Net 如何管理您的类___对象、对象工厂和应用程序上下文
- Java案例:装载与发射子弹问题
- 第四章 Spring.Net 如何管理您的类___对象的生命周期链
- Unity3d 5.x 学习笔记(3)—— 发射预制体子弹(Instantiate),点射,扫射,五连发效果
- 第四章 Spring.Net 如何管理您的类___IObjectPostProcessor接口
- 如何实现一个HTML5 RPG游戏引擎——第四章,实现情景对话
- 第四章 Spring.Net 如何管理您的类___让对象了解自己的容器