Swift Sprite-Kit:如何在触摸屏幕时提高游戏分数?

Swift Sprite-Kit: How do I increase my game's score when screen is touched?

在我的游戏中,每次触摸都会将背景向下移动 50 像素,我希望在某个动画期间的每次触摸都能转化为 50 分。在我的代码中,我有两个背景节点,它们使我的背景向下滚动每次触摸都没有背景间隙。我在绿灯动画运行时更新分数时遇到问题,并在红灯动画期间触摸屏幕时让游戏结束。我已经创建了一个分数标签。提前谢谢你。

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    if bg.position.y + bg.size.height/2 < 50
    {
        let diff =  bg.position.y + bg.size.height/2 - 50
        bg.position.y = self.frame.height + bg.size.height/2 + diff
    }
    else
    {
        bg.position.y -= 50
    }
    if bg2.position.y + bg2.size.height/2 < 50
    {
        let diff =  bg2.position.y + bg2.size.height/2 - 50
        bg2.position.y = self.frame.height + bg2.size.height/2 + diff
    }
    else
    {
        bg2.position.y -= 50
    }
}

您可以为 greenLightIsShowing 和 redLightIsShowing 设置一个布尔值,并根据它们的显示时间将它们设置为 true 和 false。然后在布尔为真时处理触摸。

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {   

        for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        if greenLightIsShowing == true {

            if nodeAtPoint(location) == self.view {

            score = score + 50
            scoreLable.text = "\(score)"

        }
}else if redLightIsShowing == true {
          if nodeAtPoint(location) == self.view {
            //end the game
        }
}

试试这个

class GameScene: SKScene {

    var currentLight : SKSpriteNode!
    var greenTexture : SKTexture!
    var redTexture : SKTexture!
    var isGreenLightON : Bool = true
    var score : Int = 0

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        greenTexture = SKTexture(imageNamed: "green light.png")
        redTexture = SKTexture(imageNamed: "red light.png")

        currentLight = SKSpriteNode(texture: greenTexture)
        currentLight.position = CGPointMake(CGRectGetMidX(self.frame), 650)


        let changeToGreenLight : SKAction = SKAction.runBlock({ () -> Void in
            self.currentLight.texture =  self.greenTexture
            self.isGreenLightON = true
        })

        let changeToRedLight : SKAction = SKAction.runBlock({ () -> Void in
            self.currentLight.texture = self.redTexture
            self.isGreenLightON = false
        })

        let changingLights = SKAction.sequence([changeToGreenLight,SKAction.waitForDuration(1, withRange: 4),changeToRedLight,SKAction.waitForDuration(1, withRange: 4)])

        currentLight.runAction(SKAction.repeatActionForever(changingLights))

        self.addChild(currentLight)
    }


    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        if isGreenLightON
        {
            score += 50
        }
        else
        {
            score -= 10
        }
        println(score)

        scoreLabel.text = "\(score)"
    }



    override func update(currentTime: CFTimeInterval) {
       // Your Code
    }
}