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

时间:2015-01-13 06:21:56

标签: ios swift touch sprite-kit

在我的游戏中,每次触摸都会将背景向下移动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
    }
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

试试这个

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