【问题标题】:Driving calculations using SpriteKit使用 SpriteKit 驱动计算
【发布时间】:2013-09-29 22:11:09
【问题描述】:

我有一个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

【问题讨论】:

  • 请贴一些代码给我们看看。
  • 希望你能从代码中看到我是多么的绝望......正在进行中,我的数学技能很糟糕:(

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


【解决方案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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多