【问题标题】:Stop Action in SpriteKit with swift使用 swift 在 SpriteKit 中停止操作
【发布时间】:2016-07-20 05:24:53
【问题描述】:

在类似俄罗斯方块的游戏中,我尝试使用动作将形状向下移动,然后当调用下一个形状时,它将阻止前一个形状移动,这样它们就不会同时移动。我曾尝试在很多其他问题中使用removeAllActionsremoveActionForKey 以及在SKNode 文档中提出的问题,但它似乎不起作用。我在我的代码上测试了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 的第一位就在那里,因此当第一个形状生成时,它不会尝试停止任何动作,因为没有任何动作。然后它将为第二,第三等等。

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

【问题讨论】:

  • removeAllActions 仅从场景中移除,而不是您当前的精灵,您还需要在精灵上调用 removeactions。 CurrentSprite?.removeAllActions
  • 哦,是的,谢谢!我实现了一个 for 循环,就像在 MoveBlocks() 的末尾一样,并按照你所说的做了,它奏效了。谢谢!除了使用 swift 而不是 Objective-c 之外,您还有其他类似的教程吗?
  • 浏览那个网站,那里有很多
  • 非常感谢

标签: swift sprite-kit skaction sknode


【解决方案1】:

稍微不同的想法,你能否将它们构建为物理体,然后在与其他块接触时移除它们的动作以“冻结”它们?只是一个想法。

【讨论】:

  • 其实我就是这么想的!我可能会试一试,看看效果如何!
  • 你可以很好地控制身体:`func evalBorderContact(contact: SKPhysicsContact){ let conA = Int(contact.bodyA.categoryBitMask) let conB = Int(contact.bodyB.categoryBitMask) // case bottemEdge = 16 // case topEdge = 32 if conB == 16 { contact.bodyA.applyImpulse(CGVectorMake(0, +10)) } else if conA == 16 { contact.bodyB.applyImpulse(CGVectorMake(0, +10))'(只是一个 sn-p)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
相关资源
最近更新 更多