SKAction遵循UIBezierPath函数无法正常工作SpriteKit - Swift

时间:2016-05-17 16:02:30

标签: swift sprite-kit uibezierpath

在我的应用程序中,我有一个Sprite绕着另一个精灵盘旋。我希望这个精灵在按住屏幕时加速。我已经实现了加速功能,但我似乎无法弄清楚为什么精灵在点击屏幕时重新回到某个位置并且在松开之后。我有一种感觉,我知道为什么,但我试图解决它,我似乎无法接近解决方案。代码如下。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let dx = Slider.accessibilityActivationPoint.x
    let dy = Slider.accessibilityActivationPoint.y

    let rad = atan2(dx, dy)

    let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

    let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 400)
    //let rotate = SKAction.rotateByAngle(75, duration: 100)

    Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {


    let dx = Slider.accessibilityActivationPoint.x
    let dy = Slider.accessibilityActivationPoint.y

    let rad = atan2(dy, dx)

    let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: 9, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

    let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 150)


    Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
}

func moveClockWise(){

    let dx = Slider.position.x / 2
    let dy = Slider.position.y / 2

    let rad = atan2(dy, dx)


    let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

    let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 150)
    //let rotate = SKAction.rotateByAngle(75, duration: 100)

    Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
    //Slider.runAction(SKAction.repeatActionForever(rotate).reversedAction())
}

1 个答案:

答案 0 :(得分:0)

旁注:你不应该在变量上使用大写。大写字母为类名称保留..

例如:

let slider = Slider()

这是几乎所有编程语言都使用的命名约定:)

无论如何..尝试做

import SpriteKit

class GameScene: SKScene {

    let Slider = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(10, 10))

    override func didMoveToView(view: SKView) {
        Slider.position = CGPointMake(size.width/2, size.height/2)
        addChild(Slider)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


        let dx = Slider.accessibilityActivationPoint.x
        let dy = Slider.accessibilityActivationPoint.y

        let rad = atan2(dx, dy)

        let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

        let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 400)
        //let rotate = SKAction.rotateByAngle(75, duration: 100)

        if !Slider.hasActions() {
            Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
        } else {
            Slider.runAction(SKAction.speedTo(1, duration: 0))
        }



    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        Slider.runAction(SKAction.speedTo(0.4, duration: 0))

    }

    func moveClockWise(){

        let dx = Slider.position.x / 2
        let dy = Slider.position.y / 2

        let rad = atan2(dy, dx)


        let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

        let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 150)
        //let rotate = SKAction.rotateByAngle(75, duration: 100)

        Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
        //Slider.runAction(SKAction.repeatActionForever(rotate).reversedAction())

    }
}