【问题标题】:When the screen is touched, how do I make the game over?当屏幕被触摸时,我如何让游戏结束?
【发布时间】:2015-01-11 09:06:53
【问题描述】:

我希望游戏结束屏幕,并且在特定动画期间触摸屏幕时游戏停止。

    let lightTexture = SKTexture(imageNamed: "green light.png")
    let lightTexture2 = SKTexture(imageNamed: "red light.png")

    let animateGreenLight = SKAction.sequence([SKAction.waitForDuration(2.0, withRange: 0.1),   SKAction.animateWithTextures([lightTexture, lightTexture2], timePerFrame: 3)])
    let changeGreenLight = SKAction.repeatActionForever(animateGreenLight)


    let animateRedLight = SKAction.sequence([SKAction.waitForDuration(2.0, withRange: 0.1), SKAction.animateWithTextures([lightTexture, lightTexture2], timePerFrame: 3)])
    let changeRedLight = SKAction.repeatActionForever(animateRedLight)


    let greenLight = SKSpriteNode(texture: lightTexture)
    greenLight.position = CGPointMake(CGRectGetMidX(self.frame), 650)
    greenLight.runAction(changeGreenLight)

    self.addChild(greenLight)

    let redLight = SKSpriteNode(texture: lightTexture2)
    redLight.position = CGPointMake(CGRectGetMidX(self.frame), 650)
    redLight.runAction(changeRedLight)

    self.addChild(redLight)

当红灯的动画出现在屏幕上时,我希望游戏结束。我是否必须做一个 if 语句,如果是,具体是为了什么?

提前谢谢你!

【问题讨论】:

    标签: ios animation swift touch sprite-kit


    【解决方案1】:

    您可以为自己创建另一个与红灯具有相同大小和位置的节点。此外,该节点能够处理触摸事件。然后,在动画运行之前添加该节点。这可以通过一系列动作来完成,例如:

    let addAction = SKAction.runBlock({ self.addChild(touchNode) })
    let animationAction = SKAction.repeatActionForever(animateRedLight)
    
    redLight.runAction(SKAction.sequence([ addAction, animationAction ]))
    

    更新

    由于您希望游戏在您触摸任何地方时结束,因此请更改代码,以便该块设置一个指示动画已执行的变量并实现检查该变量的touchesBegan,例如

    let addAction = SKAction.runBlock({ self.redLightAnimationRuns = true })
    
    [...]
    
    // In touchesBegan
    if touched
    {
        if redLightAnimationRuns
        {
            endGame()
        }
    }
    

    【讨论】:

    • var touchNode = SKSpriteNode() touchNode.size = redLight.size touchNode.position = CGPointMake(CGRectGetMidX(self.frame), 650) let addAction = SKAction.runBlock({ self.addChild(self. touchNode) }) let animationAction = SKAction.repeatActionForever(animateRedLight) redLight.runAction(SKAction.sequence([ addAction, animationAction ])) 这是我添加的,当我写 self.addChild(touchNode) 应用程序崩溃时。另外,我不认为 touchNode 的动画正在运行。
    • 我要如何做到,当屏幕被触摸到任何地方时,游戏就结束了。
    • 如果触摸会出错。 touched 是一个未解析的标识符。另外,“redLightAnimationRuns”不在我的游戏场景中,你是指我的 SKSpritenode 的 redLight 吗?
    • 当然,您必须将选择器重定向到正确的实例。因此,如果它在孩子中,请在孩子上调用选择器。但这是我无法意识到的......
    • "if touch" 给出错误,可能是错字。什么是正确的代码行?
    【解决方案2】:

    当屏幕上出现红灯时,使用 touchesBegan() 函数调用 GameOver() 函数(可以通过变量控制)。

    因此,当屏幕上出现红灯时,变量 redLightCurrent 设置为 true。在 TouchesBegan() 中,当 redLightCurrent 为 true 时,调用 gameOver() 函数,您可以在其中包含游戏结束时要执行的操作。这只会在屏幕上开始触摸时发生。

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    
      super.touchesBegan(touches, withEvent: event)
      let array = Array(touches)
      let touch = array[0] as UITouch
      let touchLocation = touch.locationInNode(self)
    
      if redLightCurrent {
           gameOver()
         }
      }
    

    此代码适用于新的 xCode 7 和 Swift 2.0

    【讨论】:

      猜你喜欢
      • 2015-08-29
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多