Pausing some actions in the Sprite bundle

I recently started developing my first iPhone app, and it will be a 2D game. Everything is going well so far, and the project already includes more than a thousand lines of code.

However, I just entered into a small problem. As with many games, you need to pause it. My first attempt was to simply use this in code where a pause should occur.

self.view.paused = true; 

It worked fine until I needed animations so that after someone pressed the pause button. Imagine a new node sprite shifted from the bottom of the screen, which includes some details about the current account and so on. Using the code above, of course, all animations will be stopped.

My new idea was to simply stop all nodes that needed to be stopped separately. I got this working using this piece of code.

 [self enumerateChildNodesWithName:@"name" usingBlock:^(SKNode* node, BOOL* stop) { node.paused = true; node.speed = 0.0; }]; 

I tried a couple of runs and thought that I finally found a solution when I entered into another problem. Some of the nodes of the sprite games that need to be stopped are created periodically after a certain time. The following code is responsible for this behavior.

 SKAction* createPeriodically = [SKAction repeatActionForever:[SKAction sequence:@[[SKAction performSelector:@selector(create) onTarget:self],[SKAction waitForDuration:15.0]]]]; [self runAction:createPeriodically]; 

As you can see, I use waitForDuration to delay the creation of sprite nodes. Now, when I pause all the nodes, the time still continues, and thus waitForDuration ends after the set time and a node sprite is created, although all the others are paused and not moved.

Hope you guys were able to understand my problem. I am looking for a way to temporarily suspend the mechanism for creating sprite nodes, while still having the ability to create a new sprite node and perform actions for the new one. Any help is appreciated. Thanks.

+6
source share
1 answer

If you set paused to node, this pauses all actions for the node and its children. That way, you can pause all actions throughout the scene (including createPeriodically ) by setting the paused property of the scene, but then you will return to the same script as the pause view.

But you can take advantage of the node and its child to organize your scene. Instead of all the nodes making up your game being direct children of the scene, create a separate node to contain all of them. Instead of triggering actions that affect your game content around the world (e.g. createPeriodically ) on the stage, run them on this separate node. Now, when you want to pause the game, set paused on this node - all actions of the game content will be suspended, but there is still room to start actions on the stage itself or add other, not suspended nodes as direct children of your scene.

+2
source

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


All Articles