0

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

编辑(添加源):

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
4

1 回答 1

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];

在哪里设置 yourDistance 和 yourDuration。

于 2013-09-30T06:23:32.167 回答