使用SpriteKit驾驶计算

时间:2013-09-29 22:11:10

标签: iphone ios objective-c ios7 sprite-kit

我有一个tilemap(Kobald Kit的KKTilemapNode)和中间的一个角色,现在我使用操纵杆来上下移动tilemap并计算速度,我的问题是如果我将角色左转,它会去仍然只是上下,因为我不知道如何计算一个角度下的新位置。我试图移植一些Flash示例,但我发现的那些没有tile贴图:(

编辑(添加来源):

rotationStep = (_velocity.x / 20); // Rotate the car by

if (fabs(_speed) > 0.3) {
    _carRotation -= (rotationStep * (_speed / maxSpeed)); // Rotation angle if speed is at least 0.3
}

CGFloat speedX = ((_carRotation * (M_PI / 180)) * _speed); // Calculation stolen from the flash game and probably changed over tooo much
CGFloat speedY = ((_carRotation * (M_PI / 180)) * _speed);

CGFloat rotationValue = (degreesToRadians(_carRotation)); // Getting radians for the rotation

if (_velocity.x != 0 || _velocity.y != 0) {
    _car.zRotation = rotationValue; // Setting correct rotation
}

NSLog(@"Speed X: %f - Y: %f", speedX, speedY); // Getting probably very incorrect values

[_track setPosition:CGPointMake(5, (_track.position.y - _speed))]; // And moving the tile map just up and down in the end based on a speed I have calculated earlier

1 个答案:

答案 0 :(得分:1)

计算角度的一种方法:

float angle = atan2f (touchY - joystickY, touchX - joystickX);

其中touchY和touchX是玩家触摸操纵杆的坐标 和joystickY和joystickX是操纵杆的坐标。使用SKAction将节点移动该角度(您可能不想使用操作但仍可以使用数学计算目标坐标):

SKAction *moveWhatever = [SKAction moveByX: yourDistance*cosf(angle) y:yourDistance*sinf(angle) duration:yourDuration];
[node runAction: moveWhatever];

你设置你的距离和你的日期。