Xcode SpriteKit - Remove sprites and terminate - repeatActionForever

I am new to Swift and SpritKit and have several problems with my game.

In my didMoveToView(view: SKView) { } section of my code, I invoke the statement below, which fills the monsters on the screen. In my func addMonster() { } Then the monsters move from the right side, to the left side of the screen. When they exit the screen to the opposite side, the sprite is deleted.

CODE A

  runAction(SKAction.repeatActionForever( SKAction.sequence([ SKAction.runBlock(addMonster), SKAction.waitForDuration(1.0),SKAction. ]) )) 

In the Mons † er add function, I call the following code, which moves Monster around the screen.

  let actualDuration = random(min: CGFloat(6.0), max: CGFloat(10.0)) let actionMove = SKAction.moveTo(CGPoint(x: -monster.size.width/2, y: actualY), duration: NSTimeInterval(actualDuration)) let actionMoveDone = SKAction.removeFromParent() monster.runAction(SKAction.sequence([actionMove, actionMoveDone])) 

All of the above code is working fine.

When the user killed the number of monsters X, I want all the other monsters on the screen to disappear and stop appearing.

My questions are: how do I: a) Stop CODE And from spawning monsters and b) how do I get the monsters that are on view, delete them?

Thanks,

Ryann

+5
source share
1 answer

When starting an action, use

 monster.runAction(SKAction.sequence([actionMove, actionMoveDone]), withKey: "actionA") 

then cancel it with

 monster.removeActionForKey("actionA") 
+10
source

Source: https://habr.com/ru/post/1206657/


All Articles