在类似俄罗斯方 block 的游戏中,我尝试使用 Action 向下移动形状,然后当调用下一个形状时,它将阻止前一个形状移动,这样它们就不会同时移动。我曾尝试在有关此问题以及 SKNode 文档中提出的许多其他问题中使用 removeAllActions
和 removeActionForKey
但它似乎不起作用。我在我的代码上测试了 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)
}
}
}
我注释掉了其他 removeAllActions
和 removeActionForKey
只是为了显示我最初的内容。此外,MoveBlocks 的第一位就在那里,因此当生成第一个形状时,它不会尝试停止任何操作,因为没有任何操作。然后是第二次、第三次等等。
我有一种感觉,这与我一开始实际创建操作的方式有关,但我不太确定。
最佳答案
稍微不同的想法,你可以将它们构建为物理体,然后在与其他 block 接触时删除它们的 Action 以“卡住”它们吗?只是一个想法。
关于swift - 使用 swift 停止 SpriteKit 中的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338801/