Cocos2D旋转炮塔到指定角度(三)

来源:互联网 发布:充值软件 编辑:程序博客网 时间:2024/04/28 01:57

到目前为止都很美好!

但是却有一点奇怪,因为炮塔一下子跳转到指定位置去射击,并不是平滑的跟随触摸去转动到指定位置.你可以修复这个问题,但是这需要略微一点的重构(refactoring).

首先打开HelloWorldLayer.h,在你的类中添加下面一个实例变量:

CCSprite *_nextProjectile;

然后回到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 = [self convertTouchToNodeSpace:touch];    // Set up initial location of projectile    CGSize winSize = [[CCDirector sharedDirector] winSize];    _nextProjectile = [[CCSprite spriteWithFile:@"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);    [_player runAction:     [CCSequence actions:      [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],      [CCCallBlock actionWithBlock:^{         // OK to add now - rotation is finished!         [self addChild:_nextProjectile];         [_projectiles addObject:_nextProjectile];         // Release         [_nextProjectile release];         _nextProjectile = nil;     }],      nil]];    // Move projectile to actual endpoint    [_nextProjectile runAction:     [CCSequence actions:      [CCMoveTo actionWithDuration:realMoveDuration position:realDest],      [CCCallBlockN actionWithBlock:^(CCNode *node) {         [_projectiles removeObject:node];         [node removeFromParentAndCleanup:YES];    }],      nil]];    _nextProjectile.tag = 2;    [[SimpleAudioEngine sharedEngine] playEffect:@"pew-pew-lei.caf"];}

看起来代码不少,但是实际你并没有修改太多,大多数都仅仅是很小的重构,以下是代码做出的改变:

  • 在方法开始处如果_nextProjectile不为nil则直接退出,意思是你正在处理射击操作
  • 之前你使用的是一个名为projectile的局部变量,然后立即把它添加到场景中.在新的版本中你创建了一个实例变量_nextProjectile,但是没有立即添加到场景中.
  • 你定义了炮塔旋转的速度为180 / 0.5,意味着半秒钟旋转180度.
  • 去计算旋转特定的角度需要多长时间
  • 然后开始一个顺序action去旋转炮塔到正确的角度,然后调用一个block去将导弹添加到场景中.

偶鸟,试一试吧!编译运行项目,然后现在炮塔应该旋转的平滑多了 ;)

原文地址:Rotating Turrets: How To Make A Simple iPhone Game with Cocos2D 2.X Part 2

2 0
原创粉丝点击