In a tetris-like game, I am trying to use actions to move the Shapes down and then when the next shape is called, it will stop the previous shape from moving, so that they arent both moving. I have tried to use removeAllActions
and removeActionForKey
in a lot of other questions asked on this and also in the SKNode Documentation but it doesnt seem to be working. I tested actionForKey
on my code and it appears it's not picking up any action that is being carried out even though the other shapes are still moving.
I first call my doMethod
function inside of the didMoveToView
which generates the shape and then starts to move it as follows. This is called whenever I press a button so I can control when each new shape is create.
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++
}
}
Then my MoveBlocks
function is as follows
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)
}
}
}
I have commented out the other removeAllActions
and removeActionForKey
just to show what I had originally. Also, the first bit of MoveBlocks
is just there so that when the first shape is generated, it doesnt try to stop any actions as there arent any. Then it will for the second, third and so on.
I have a feeling that it's something to do with how I'm actually creating the Action in the first place but I'm not too sure.