移动我的SpriteNode Swift SpriteKit

时间:2015-10-19 10:28:22

标签: ios swift sprite-kit swift2

对不起我的英语我是法国人。 大家好。

我目前正在使用Swift编写一个小游戏,我想知道如何移动我的角色在开始时位于中间,并且在使用该功能触摸时移动移动他跟随我的手指但不是瞬间,这是它自己的速度可以说。如果是这样的话,你也可以告诉我当我在箭头屏幕的另一侧拖动手指改变方向时,如何让我的角色(例如箭头)朝向我伸出手指的相同方向:)

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

这是我的代码:

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

            let actionBouger = SKAction.moveTo(CGPoint(x: location.x, y: location.y), duration: 2.5)
            Vaisseau.runAction(actionBouger)

            let dx = location.x - Vaisseau.position.x
            let dy = location.y - Vaisseau.position.y
            var angleInRadians = atan2(dy, dx) - CGFloat(M_PI_2)

            if(angleInRadians < 0){
                angleInRadians = angleInRadians + 2 * CGFloat(M_PI)
            }

            Vaisseau.zRotation = angleInRadians

            let actionAngle = SKAction.rotateToAngle(angleInRadians, duration: 0)
            Vaisseau.runAction(actionAngle)
        }
    }
相关问题