ios - 为什么在spritekit中按下按钮时节点的速度越来越快?

标签 ios swift

正如标题所示,每次我点击按钮节点时,项目节点都会变得更快。如何才能保持速度一致? 这是该函数的代码。

let ball: Ball = Ball(textureName: "ball.png")
func attackButton() {

    ball.removeFromParent()

    ball.position = CGPointMake(100, hero.position.y)
    ball.startMoving()
    hero.throwBall() // for just movement of his arm
    playThrowSound()
    addChild(ball)

}

func startMoving() {
    let moveLeft = SKAction.moveByX(200, y: 0, duration: 3)
    ball.runAction(SKAction.repeatActionForever(moveLeft))

    let rotating = SKAction.rotateByAngle(CGFloat(M_PI), duration: 6)
    ball.runAction(SKAction.repeatActionForever(rotating))
}

此外,虽然这段代码可以工作,但不是我想要的方式。产生一个球后,删除该球并产生下一个球。我知道这是因为 removeFromParent(),但我无法找出其他方法。我希望曾经产生的球只有在它离开屏幕时才被删除。

因此,如果您为我提供这两个问题的解决方案,我真的很感激。

最佳答案

您能为您的 Ball-Class 添加代码吗?我猜您的 Ball-Class 是应用程序崩溃的问题。在函数 attackButton 中,您将删除ball 并将其添加到场景中。但我认为在你的 Ball-Class 中你添加了一个 SKNode 但你也没有在那里删除它。因此,如果您解决了 Ball-Class 中的问题,下面的代码应该可以工作(您没有发布代码 - 所以您必须自己检查)。

let ball: Ball = Ball(textureName: "ball.png")

func attackButton() {
    ball.removeFromParent()
    ball.position = CGPointMake(100, hero.position.y)
    hero.throwBall() // for just movement of his arm
    playThrowSound()
    addChild(ball)
    ball.startMoving()
}

func startMoving() {
    ball.removeAllActions()
    let moveLeft = SKAction.moveByX(200, y: 0, duration: 3)
    ball.runAction(SKAction.repeatActionForever(moveLeft))

    let rotating = SKAction.rotateByAngle(CGFloat(M_PI), duration: 6)
    ball.runAction(SKAction.repeatActionForever(rotating))
}

关于ios - 为什么在spritekit中按下按钮时节点的速度越来越快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37131917/

相关文章:

ios - 这个unicode加密会失败吗?

ios - 我如何从某人点击的已创建单元格的 firebase 中提取 key

ios - 在 Xcode/Swift 中导出 TableView 数据

ios - presentViewController:animated:NO 简要展示了 iOS 8 中的呈现 Controller 。备选方案?

ios - 基于字符串数组 (Swift) 在 UITableView 中的 viewdidload 上选择单元格

c++ - 使用 libsodium、randombytes 错误为 iOS 构建 zeromq

swift - 这个闭包 ()->() 在 swift 中能做什么?

ios - 如何在 Firestore 中构建搜索列表?

ios - Swift:将 [[[CGFloat]]] 合并到 [[CGFloat]] 与额外数据的正确方法是什么

ios - 如何在 iOS 中保存用户复选标记选择