基于度数移动精灵

时间:2018-02-01 03:19:26

标签: swift xcode sprite-kit swift4

我已经能够检测到度数,但我不确定如何在360度内移动精灵。 drawing 我不希望精灵只能在某些部分移动,如上图所示,而是让它能够移动一整圈。 代码:

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

        if (ball.frame.contains(location)) {

            stickActive = true
        }else {

            stickActive = false

        }

    }

}

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

        if (stickActive == true) {


            var v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
            let angle = atan2(v.dy, v.dx)
            var deg = angle * CGFloat(180 / M_PI)
            print(deg + 180)

            let lenght:CGFloat = base.frame.size.height / 2 - 20
            let xDist: CGFloat = sin(angle - 1.57079633) * lenght
            let yDist: CGFloat = cos(angle - 1.57079633) * lenght
            ball.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)

            if (base.frame.contains(location)) {

                ball.position = location
            }else {

                ball.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)

            }

        }

    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if (stickActive == true) {


        let move: SKAction = SKAction.move(to: base.position, duration: 0.2)
        move.timingMode = .easeOut

        ball.run(move)


    }
}

1 个答案:

答案 0 :(得分:1)

这很简单,虽然我花了很长时间才弄清楚如何去做。

TouchesBegan方法:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        if isTracking == false && DPad.contains(location) {
            isTracking = true
        }
    }
}

TouchesMoved方法:

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

            v = CGVector(dx: location.x - DPad.position.x, dy: location.y - DPad.position.y)
            let angle = atan2(v.dy, v.dx)
            let deg = angle * CGFloat(180 / Double.pi)

            let Length:CGFloat = DPad.frame.size.height / 2
            let xDist: CGFloat = sin(angle - 1.57079633) * Length
            let yDist: CGFloat = cos(angle - 1.57079633) * Length

            xJoystickDelta = location.x - DPad.position.x
            yJoystickDelta = location.y - DPad.position.y

            if DPad.contains(location) {
                thumbNode.position = location
            } else {
                thumbNode.position = CGPoint(x: DPad.position.x - xDist, y: DPad.position.y + yDist)
            }
        }
    }
}

TouchesEnded方法:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    isTracking = false
    thumbNode.run(SKAction.move(to: DPad.position, duration: 0.01))
    xJoystickDelta = 0
    yJoystickDelta = 0
}

update(_ currentTime:)方法:

    if v.dx > abs(v.dy) {
        yourPlayer.texture = SKTexture(imageNamed: "rightTexture")
    } else if v.dx < -abs(v.dy) {
        player.texture = SKTexture(imageNamed: "leftTexture")
    } else if v.dy < 0 {
        yourPlayer.texture = SKTexture(imageNamed: "frontTexture")
    } else if v.dy > 0 {
        yourPlayer.texture = SKTexture(imageNamed: "backTexture")
    }
    //This code moves your character where-ever you want it too
    let xScale = CGFloat(4) //adjust to your preference. Higher means slower, lower means faster
    let yScale = CGFloat(4) //adjust to your preference. Higher means slower, lower means faster

    let xAdd = xScale * self.xJoystickDelta
    let yAdd = yScale * self.yJoystickDelta

    yourPlayerNode.position.x += xAdd
    yourPlayerNode.position.y += yAdd

这些事情需要超出您的didMove(toView:)方法:

var xJoystickDelta:CGFloat = 0
var yJoystickDelta:CGFloat = 0
var v = CGVector()
var isTracking:Bool = false

var DPad = SKSpriteNode()
var thumbNode = SKSpriteNode()

<强> -Explanation -

touchesBegan方法中,if语句正在测试您是否未控制thumbNode以及您的触摸是否在DPad节点内。然后它开始跟踪。

touchesMoved一次isTracking == true,它开始计算必要的数学,然后调整所需的各种事物。 (它很复杂,最重要的是它有效。)

touchesEnded方法中,它正在测试您何时将手指从屏幕上抬起,然后重置所有内容以供下次使用。

update(_ current:)方法中,代码计算CGVector的角度,然后在各种情况下设置纹理(或任何你想做的事情)。然后它计算在DPad内的thumbNode的位置,并在场景中移动你的玩家(或你需要移动的任何东西)。调整xScaleyScale浮点数可以减慢运动速度,降低速度可以增加你想要移动的任何颜色的运动。

- 除了必要的东西 -

您需要在DPad方法中设置thumbNodedidMove(toView:)

thumbNode.size = CGSize(width: 50, height: 50)
DPad.size = CGSize(width: 150, height: 150)
DPad.position = CGPoint(x: 0, y: 0)
thumbNode.position = DPad.position
DPad.zPosition = 3
thumbNode.zPosition = 4
DPad.texture = SKTexture(imageNamed: "yourBaseTexture")
thumbNode.texture = SKTexture(imageNamed: "yourStickTexture")

self.addChild(thumbNode)
self.addChild(DPad) 

您只需将DPad.position移动到您想要的任何位置即可。 thumbNode将随之移动。此外,如果您有任何问题,请务必问我,以便我可以帮助您。