XCode,SpriteKit:如何通过触摸触发两个动作?

时间:2016-04-17 23:40:42

标签: sprite-kit swift2

我的简单游戏理念坚持一个规模增长,达到一定大小的节点,然后回到原来的大小,然后循环。我有它,所以一旦我触摸屏幕,这会触发。我想要它,所以在我第一次触摸它并触摸它一秒钟后,即使它处于增大或减小的中间,我希望它切换到它正在做的相反动作。例如,如果它的尺寸越来越大,我会在它减少之前点击,它会减少...无论如何......这是我的代码。我需要做什么?干杯!

import SpriteKit

类GameScene:SKScene,SKPhysicsContactDelegate {

let player = SKSpriteNode(color: .blackColor(), size: CGSize(width: 50, height: 50))

override func didMoveToView(view: SKView) {

    backgroundColor = UIColor.whiteColor()

    self.player.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    addChild(self.player)

}

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

    //SCALE//

    let scaleUp = SKAction.scaleTo(6.0, duration: 1)
    let scaleDown = SKAction.scaleTo(1.0, duration: 1)

    let scaleUpGroup = SKAction.group([scaleUp])
    let scaleDownGroup = SKAction.group([scaleDown])

    let scaleUpAndDownSequence = SKAction.sequence([scaleUpGroup, scaleDownGroup])

    let loop = SKAction.repeatActionForever(scaleUpAndDownSequence)

    self.player.runAction(loop, withKey: "scalingUpAndDown")

}

}

1 个答案:

答案 0 :(得分:0)

可能的解决方案是使用布尔值作为一种切换开关。创建一个名为“isReversed”的var,然后创建一个切换顺序的序列。

let scaleDownAndUp = SKAction.sequence([scaleDownGroup, scaleUpGroup])
let reverseLoop = SKAction.repeatActionForever(scaleDownAndUp)

var isReversed = false

 if !isReversed {
    self.player.removeActionForKey("scalingDownAndUp")
    self.player.runAction(loop, withKey: "scalingUpAndDown")
    isReversed = true
} else {
    self.player.removeActionForKey("scalingUpAndDown")
    self.player.runAction(reverseLoop, withKey: "scalingDownAndUp")
    isReversed = false
}

当您点击屏幕,执行正确的操作然后将isReversed切换到其备用状态时,将检查isReversed。

我没有对此进行测试,但是如果有些东西略微偏离,那么一般的想法是合理的,应该很容易实现。