使用虚拟操纵杆将SKSpriteNode移动到半圆形中

时间:2017-05-28 21:26:35

标签: ios swift sprite-kit skspritenode joystick

我正在制作一个包含虚拟操纵杆的游戏。我的操纵杆由两个SKSpriteNode组成,一个用于棒,另一个用于基板。 (见下图)

Virutal Joystick

显然我的操纵杆只能按照面板上的绿色半圆移动。

我的问题是我试图让它成功,这是我现在的代码:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        let location = touch.location(in: self)

        guard isTracking else {
            return
        }

        let maxDistantion = panel.radius
        let realDistantion = sqrt(pow(location.x, 2) + pow(location.y, 2))
        let needPosition = realDistantion <= maxDistantion ? CGPoint(x: location.x, y: location.y) : CGPoint(x: location.x / realDistantion * maxDistantion, y: location.y / realDistantion * maxDistantion)

        if (needPosition.y > 0) {
            stick.position = needPosition
        } else {
            stick.position = CGPoint(x: needPosition.x, y: 0)
        }

        data = AnalogJoystickData(velocity: needPosition, angular: -atan2(needPosition.x, needPosition.y))
    }
}

有了这段代码,我可以进入半圆圈,但是如果有人可以帮我解决我糟糕的数学问题,我只会把我的棍子移到绿线上

谢谢

0 个答案:

没有答案