简体   繁体   中英

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

In my game, every touch moves the background down 50 pixels, I want every touch during a certain animation to translate into a score of 50. In my code, I have two background nodes that make my background scroll down with every touch without a gap in the background. I have a problem updating the score when the green light animation runs, and make the game end when the screen is touched during a red light animation. I have already created a score label. Thank you in advance.

 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
    }
}

You could set a bool for greenLightIsShowing and redLightIsShowing and set them to true and false depending on when they are showing. Then just handle the touches when the bools are true.

 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
        }
}

Try this

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM