我该如何修理这个炮塔以使其平稳移动

时间:2014-04-10 08:51:13

标签: ios sprite-kit

好吧,伙计们我已经成功制作了一个精灵套装游戏,我有一个可以射击糖果的炮塔,但我遇到的问题是,当我用手指指向一个位置时,炮塔不能平稳移动。它只是跳到指向该位置。

所以这是我的代码:

- (void) rotateSprite:(SKSpriteNode *)sprite toFace:(CGPoint)velocity rotateRadiansPerSec:(CGFloat)rotateRadiansPerSec {
float targetAngle = CGPointToAngle(velocity);
float shortest = ScalarShortestAngleBetween(sprite.zRotation, targetAngle);
float amtToRotate = rotateRadiansPerSec * _dt;
if (ABS(shortest) < amtToRotate) {
    amtToRotate = ABS(shortest);
}
sprite.zRotation += ScalarSign(shortest) * amtToRotate;
}

 - (void) movePlayerToward:(CGPoint)location {
_lastTouchLocation = location;
CGPoint offset = CGPointSubtract(location, _Player.position);
CGPoint direction = CGPointNormalize(offset);
_velocity = CGPointMultiplyScalar(direction, PLAYER_MOVE_POINTS_PER_SEC);

}

- (void) update:(NSTimeInterval)currentTime {
if (_lastUpdateTime) {
    _dt = currentTime - _lastUpdateTime;
}
else {
    _dt = 0;
}
_lastUpdateTime = currentTime;
CGPoint offset = CGPointSubtract(_lastTouchLocation, _Player.position);
float distance = CGPointLength(offset);
if (distance < PLAYER_MOVE_POINTS_PER_SEC * _dt) {
    _velocity = CGPointZero;
}
else
{
    [self rotateSprite:_Player toFace:_velocity rotateRadiansPerSec:PLAYER_ROTATE_RADIANS_PER_SEC];
}
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
[self movePlayerToward:touchLocation];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
[self movePlayerToward:touchLocation];
}

1 个答案:

答案 0 :(得分:1)

我假设您正在使用Zombie Conga代码,该代码使用static const来设置固定的移动和旋转速度值。

在您的代码中,您有float amtToRotate = rotateRadiansPerSec * _dt;

确保正确设置rotateRadiansPerSec值,因为这可以控制旋转速度。

在Conga代码中,值为:

static const float ZOMBIE_ROTATE_RADIANS_PER_SEC = 4 * M_PI;