swift - 使用 swift 停止 SpriteKit 中的 Action

标签 swift sprite-kit skaction sknode

在类似俄罗斯方 block 的游戏中,我尝试使用 Action 向下移动形状,然后当调用下一个形状时,它将阻止前一个形状移动,这样它们就不会同时移动。我曾尝试在有关此问题以及 SKNode 文档中提出的许多其他问题中使用 removeAllActionsremoveActionForKey 但它似乎不起作用。我在我的代码上测试了 actionForKey ,即使其他形状仍在移动,它似乎也没有执行任何正在执行的操作。

我首先在 didMoveToView 内部调用我的 doMethod 函数,该函数生成形状,然后开始按如下方式移动它。每当我按下按钮时就会调用此函数,以便我可以控制何时创建每个新形状。

func doMethod(){
    if(true){
        if(number_of_blocks != 1){
            current_min = current_max + 1
        }
        // calculate points for shape
        var box_d = createBox(self.size.width/2, yinit: 7*self.size.height/8)
        // using box_d, make a SKNode with each calculated value
        makeBox(box_d)
        // move the blocks down
        MoveBlocks()
        number_of_blocks++
    }
}

那么我的MoveBlocks函数如下

func MoveBlocks(){
    if(number_of_blocks > 1){
        let newnum = number_of_blocks - 1
        let KeyName = "MoveBlocks_"+String(newnum)
        let ActName = actionForKey(KeyName) // returns nil
        // removeAllActions()
        // removeActionForKey(KeyName)
    }


    var Sprite = childNodeWithName("BOX_"+String(current_min))
    var y_min = (Sprite?.position.y)
    // calc y_min to stop when gets to bottom - not currently implemented correctly
    for i in current_min...(current_max-1){
        var CurrentSprite = childNodeWithName("BOX_"+String(i))
        if(CurrentSprite?.position.y < y_min){
            y_min = (CurrentSprite?.position.y)
        }
    }
    if(y_min > CGFloat(BOX_WIDTH/2)){
        for i in current_min...current_max {
            var CurrentSprite = childNodeWithName("BOX_"+String(i))
            //var current_pos = sprite_self?.position.y
            //sprite_self?.position.y = (sprite_self?.position.y)! - CGFloat(BOX_WIDTH/2)
            let moveAction = SKAction.moveBy(CGVector(dx: 0,dy: -60), duration: 0.001)
            let pauseAction = SKAction.waitForDuration(0.999)
            let runSequence = SKAction.sequence([moveAction, pauseAction])
            var KeyName = "MoveBlocks_"+String(number_of_blocks)
            CurrentSprite?.runAction(SKAction.repeatActionForever(runSequence), withKey: KeyName)
        }
    }
}

我注释掉了其他 removeAllActionsremoveActionForKey 只是为了显示我最初的内容。此外,MoveBlocks 的第一位就在那里,因此当生成第一个形状时,它不会尝试停止任何操作,因为没有任何操作。然后是第二次、第三次等等。

我有一种感觉,这与我一开始实际创建操作的方式有关,但我不太确定。

最佳答案

稍微不同的想法,你可以将它们构建为物理体,然后在与其他 block 接触时删除它们的 Action 以“卡住”它们吗?只是一个想法。

关于swift - 使用 swift 停止 SpriteKit 中的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338801/

相关文章:

ios - 自定义过渡

ios - swift: UITextField 没有名为 text 的成员

ios - Sprite-Kit:如何可视化 SKFieldNode

ios - SpriteKit 无法加载以前使用过的资源

swift - 如何让 SKAction 在随机时间后重复自身

swift - 我的图像 addSubview 添加了比预期更大的 View

swift - 记录alamofire上传图片请求和响应

ios - 在滚动背景上使用什么 SKPhysicsBody 来限制底部和顶部?

swift - 小, "bouncy"在用 SKAction 移动的另一个形状插入 SKShapeNode 时滞后

ios - SpriteKit 中的动画路径绘制