【问题标题】:Bouncing label in Sprite KitSprite Kit 中的弹跳标签
【发布时间】:2016-08-11 13:04:13
【问题描述】:

我正在构建一个游戏,当触摸屏幕时(它在 touchesBegan 内),标签应该弹回屏幕(仅一次)。

我发现 this 扩展有助于“弹跳”,但我不知道当我触摸 restartButton 时会发生什么(此按钮工作正常)之前 3 秒(持续时间?!?!),myLabel 只会出现,没有任何动画。我做错什么了吗?

Ps:restartButton 有一个 removeAllActions()。所以即使这个动画在循环中(我猜不是),它也会停止,不是吗?

//myLabel
    myLabel.position = CGPoint(x: self.frame.width / 2, y: self.frame.height)
    myLabel.text = "0"

    self.addChild(myLabel)

    //bounces into screen (using Sprite-Kit Spring)
    self. myLabel.runAction(SKAction.moveTo(CGPoint(x: self.frame.width / 2, y: self.frame.height / 1.125), duration: 3.0, delay: 0.5, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0))

当restartButton被触摸时,它会调用这个函数:

func restartScene(){
        self.removeAllChildren()
        self.removeAllActions()
        gameStarted = false

        createScene()
    }

Ps:createScene() 是实际的游戏场景(位置、比例、物理等)。

【问题讨论】:

  • removeAllActions 仅删除场景中的动作,而不是其子项的动作。 RemoveAllChildren 不会删除操作。调用删除标签上的所有操作。

标签: ios swift sprite-kit


【解决方案1】:

在使用 Sprite Kit 时,请注意 SKScene 是 SKNode 的成员。调用removeAllActions 只会移除应用于屏幕的动作,调用removeAllChildren 只会移除子元素,而不是动作。

你需要给你所有的孩子打电话 removeAllActions 让他们停止移动。

如果您想为您的场景提供一种方法来删除所有孩子的动作,请在 SKNode 上设置一个扩展。

extension SKNode
{
    func removeAllActionsIncludingChildren()
    {
       self.removeAllActions()
       self.children.map { $0.removeAllActionsIncludingChildren()}
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多