Cocos2D 2.1开发简单iPhone游戏(2)
来源:互联网 发布:淘宝旺铺有什么用 编辑:程序博客网 时间:2024/06/06 15:50
一、原文出处
http://www.raywenderlich.com/25791/rotating-turrets-how-to-make-a-simple-iphone-game-with-cocos2d-2-x-part-2
二、旋转的炮台
1.开始
a.从resources for this tutorial下载新图片,将他们加到工程。
b.HelloWorldLayer.m
修改init函数
CCSprite *player = [CCSpritespriteWithFile:@"player2.png"];
修改ccTouchesEnded函数
CCSprite *projectile = [CCSpritespriteWithFile:@"projectile2.png"];
2.旋转射击
a.HelloWorldLayer.h
增加成员变量CCSprite *_player;保存炮台对象
b.HelloWorldLayer.m
修改init函数
_player = [CCSpritespriteWithFile:@"player2.png"];
_player.position =ccp(_player.contentSize.width/2,size.height/2);
[selfaddChild:_player];
修改ccTouchesEnded函数,增加代码
// Determine angle to face
float angleRadians = atanf((float)offRealY / (float)offRealX);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float cocosAngle = -1 * angleDegrees;
_player.rotation = cocosAngle;
此时是射击后炮台才做的旋转
3.旋转后射击,使画面更加平滑
a.HelloWorldLayer.h
增加成员变量CCSprite *_nextProjectile;
b.HelloWorldLayer.m
修改函数ccTouchesEnded函数
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (_nextProjectile !=nil) return;
// Choose one of the touches to work with
UITouch *touch = [touches anyObject];
CGPoint location = [selfconvertTouchToNodeSpace:touch];
// Set up initial location of projectile
CGSize winSize = [[CCDirectorsharedDirector] winSize];
_nextProjectile = [[CCSpritespriteWithFile:@"projectile2.png"]retain];
_nextProjectile.position =ccp(20, winSize.height/2);
// Determine offset of location to projectile
CGPoint offset = ccpSub(location,_nextProjectile.position);
// Bail out if you are shooting down or backwards
if (offset.x <=0) return;
// Determine where you wish to shoot the projectile to
int realX = winSize.width + (_nextProjectile.contentSize.width/2);
float ratio = (float) offset.y / (float) offset.x;
int realY = (realX * ratio) + _nextProjectile.position.y;
CGPoint realDest = ccp(realX, realY);
// Determine the length of how far you're shooting
int offRealX = realX - _nextProjectile.position.x;
int offRealY = realY - _nextProjectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1;// 480pixels/1sec
float realMoveDuration = length/velocity;
// Determine angle to face
float angleRadians = atanf((float)offRealY / (float)offRealX);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float cocosAngle = -1 * angleDegrees;
float rotateDegreesPerSecond =180 / 0.5; // Would take 0.5 seconds to rotate 180 degrees, or half a circle
float degreesDiff = _player.rotation - cocosAngle;
float rotateDuration = fabs(degreesDiff / rotateDegreesPerSecond);
[_playerrunAction:
[CCSequence actions:
[CCRotateToactionWithDuration:rotateDuration angle:cocosAngle],
[CCCallBlockactionWithBlock:^{
// OK to add now - rotation is finished!
[selfaddChild:_nextProjectile];
[_projectilesaddObject:_nextProjectile];
// Release
[_nextProjectilerelease];
_nextProjectile =nil;
}],
nil]];
// Move projectile to actual endpoint
[_nextProjectilerunAction:
[CCSequence actions:
[CCMoveToactionWithDuration:realMoveDuration position:realDest],
[CCCallBlockNactionWithBlock:^(CCNode *node) {
[_projectilesremoveObject:node];
[node removeFromParentAndCleanup:YES];
}],
nil]];
_nextProjectile.tag =2;
[[SimpleAudioEnginesharedEngine] playEffect:@"pew-pew-lei.caf"];
}
4.结束
- Cocos2D 2.1开发简单iPhone游戏(2)
- Cocos2D 2.1开发简单iPhone游戏(1)
- Cocos2D 2.1开发简单iPhone游戏(3)
- iPhone开发之游戏篇(2) --- cocos2d的Actions
- 如何用Cocos2d来开发简单的iPhone游戏教程
- (译)如何使用cocos2d开发一个简单的iphone游戏:旋转炮塔。(第二部分)
- (译)如何使用cocos2d开发一个简单的iphone游戏:旋转炮塔。(第二部分)
- (译)如何使用cocos2d开发一个简单的iphone游戏:旋转炮塔。(第二部分)
- 3 .如何使用cocos2d开发一个简单的iphone游戏:旋转炮塔(第二部分)
- 如何使用cocos2d开发一个简单的iphone游戏:旋转炮塔。(第二部分)
- 使用 Cocos2D 开发iPhone游戏
- 使用 Cocos2D 开发iPhone游戏
- 使用 Cocos2D 开发iPhone游戏
- 使用 Cocos2D 开发iPhone游戏
- 使用 Cocos2D 开发iPhone游戏
- iphone游戏开发之cocos2d
- iPhone之使用Cocos2d来开发简单的IPhone游戏教程
- 【iphone游戏开发】iPhone游戏开发之Cocos2d常用工具大全
- 题目1189:还是约瑟夫环
- 13-14寒假作业9
- 给VM VirtualBox的Linux虚拟机添加虚拟硬盘
- 【prim】poj 2485
- 【UNIX高级环境编程】3.信号
- Cocos2D 2.1开发简单iPhone游戏(2)
- Java学习之关于静态修饰符的一个小程序
- 关于evalb的使用
- 四个铁球放入圆柱形容器
- poj-2352-Stars-线段树
- python note
- sdjzu2121叠筐
- 只要有你就溫暖了
- 文本索引小结