How can I countdown before I resume a SKScene? - ios

I have a paused SKScene. When the user asks to do so, I would like to resume the scene. However, I would like to give the user a couple of seconds to prepare before the game begins. To do so, when the user asks to resume the game, I would like to first countdown from 3, and then resume the scene.
I currently have a SKLabel to indicate the count. When the user clicks on resume, I use an NSTimer to countdown from 3, updating the contents of the label every second, and resuming the game when the count is up.
However, because the game is paused, the SKLabel doesn't update every second; it only updates once at the very end, once the game is resumed. I am looking for a way around this.

Use a common variable in your GameScene that indicates if the game is paused, for example isGamePaused. In your update: method, you will have:
if(!isGamePaused){
//Do all game logic
}
You can use isGamePaused to pause and unpause the game. Now, let's make the countdown. I would make a subclass of SKNode, add the SKLabel there and set a delegate so we know when the CountDown finished. For example, in CountDown.h:
#protocol SKCountDownDelegate <NSObject>
-(void)countDown:(id)countDown didFinishCounting:(BOOL) didFinishCounting;
#end
#interface SKCountDown : SKNode
#property (assign, atomic) id<SKCountDownDelegate> delegate;
-(instancetype)initCountDownWithSeconds:(int)seconds andColor:(UIColor *) color andFontName:(NSString *)fontName;
#end
And in CountDown.m:
#import "SKCountDown.h"
#interface SKCountDown()
#property int secondsToGo;
#property SKLabelNode *numberLabel;
#property NSTimer *countTimer;
#end
#implementation SKCountDown
-(instancetype)initCountDownWithSeconds:(int)seconds andColor:(UIColor *)color andFontName:(NSString *)fontName{
if (self = [super init]) {
self.numberLabel = [[SKLabelNode alloc] initWithFontNamed:fontName];
[self.numberLabel setFontColor:color];
[self.numberLabel setFontSize:110];
[self.numberLabel setText:#"3"];
self.secondsToGo = seconds;
[self addChild:self.numberLabel];
self.countTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(count) userInfo:nil repeats:YES];
}
return self;
}
-(void)count{
if (self.secondsToGo > 1) {
self.secondsToGo -= 1;
self.numberLabel.text = [NSString stringWithFormat:#"%i", self.secondsToGo];
}else{
[self.countTimer invalidate];
self.countTimer = nil;
self.numberLabel.text = #"GO!";
[self performSelector:#selector(finish) withObject:nil afterDelay:1];
}
}
-(void)finish{
[self removeFromParent];
[self.delegate countDown:self didFinishCounting:YES];
}
#end
So, anywhere you want to add this CountDown, you can do the following:
-(void)startCountDown{
self.countDown = [[SKCountDown alloc] initCountDownWithSeconds:3 andColor:self.countdownFontColor andFontName:self.countdownFontName];
self.countDown.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
self.countDown.delegate = self;
self.countDown.zPosition = 20;
[self addChild:self.countDown];
}
-(void)countDown:(id)countDown didFinishCounting:(BOOL)didFinishCounting{
isGamePaused = NO;
}
This is an example of a way to do it. Hope it helps!

Pausing an entire SKScene during gameplay seems a bit too blunt for me, because you typically want to update something in the node hierarchy.
A simple solution would be to make a distinction between game node and UI nodes. You can simply add two nodes (game and ui) to the scene and whenever the scene would call [self addChild] choose which node to add them to.
Later, you can pause the game node and still be able to update the UI.
SKScene
- SKNode "game" // <-- pause this one
- SKSpriteNode "goodGuy"
- SKSpriteNode "badGuy"
- SKSpriteNode "mehGuy"
- ...
- SKNode "ui"
- SKLabelNode "countdownLabel"
- SKSpriteNode "resumeButton"
- SKSpriteNode "pauseButton"
- ...
Do take note that the paused property only determines if actions are processed on the node and its descendants. If you are performing physics this will not suffice.
Pausing physics can be done by pausing the SKView but that is definitely not what you're looking for. You can always try setting the speed of the physicsWorld to 0 (I haven't tested this, though).

I prefer to simply pause sections of the update method instead of pausing the whole scene. Create a BOOL property:
#property (nonatomic) BOOL runUpdateMethod;
Structure your update method to determine what pieces get paused:
-(void)update {
if(runUpdateMethod) {
// code to be paused
}
// code not to be paused
}
You can use a SKAction block to run a countdown and resume regular updates:
-(void)countdown {
__block SKLabelNode *labelNode0;
SKAction *wait0 = [SKAction waitForDuration:1.0];
SKAction *block0 = [SKAction runBlock:^{
labelNode0 = [SKLabelNode labelNodeWithFontNamed:#"Arial"];
labelNode0.text = #"5";
labelNode0.fontSize = 100;
labelNode0.fontColor = [SKColor redColor];
labelNode0.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
labelNode0.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
labelNode0.position = CGPointMake(self.screenWidth/2, self.screenHeight/2);
labelNode0.zPosition = 955;
labelNode0.alpha = 0.4;
[self addChild:labelNode0];
}];
SKAction *block1 = [SKAction runBlock:^{
labelNode0.text = #"4";
}];
SKAction *block2 = [SKAction runBlock:^{
labelNode0.text = #"3";
}];
SKAction *block3 = [SKAction runBlock:^{
labelNode0.text = #"2";
}];
SKAction *block4 = [SKAction runBlock:^{
labelNode0.text = #"1";
}];
SKAction *block5 = [SKAction runBlock:^{
[labelNode0 removeFromParent];
self.runUpdateMethod = YES;
}];
[self runAction:[SKAction sequence:#[block0, wait0, block1, wait0, block2, wait0, block3, wait0, block4, wait0, block5]]];
}

Instead of pausing the game, I set SKScene's speed property to 0, along with setting SKPhysicsWorld's speed property to 0. I also had to modify my update function slightly by only executing certain portions of the code if the game isn't paused.
This created the same effect as the entire game being paused, while also allowing me to update the content of labels.

Related

SceneKit overlay SKScene has no children when receiving a notification?

This seems like a huge glitch in my opinion.
I have an SKScene overlaying a SceneKit scene. In my app, a method is called to update progress on a "progress bar" (really just an SKShapeNode with a really low height). This method is called updateSaveDataNotification and takes an NSNotification. I called [self childNodeWithName:] to access a child node. The problem is, when it's called, self has no children. Even though it really has three children.
I'm changing my code so they're variables in the implementation so I can animate their scale that way, but why would this behavior even exist? Why would self have zero children in this instance?
EDIT: the progress bar is simply an SKShapeNode. Here's the code I used:
#implementation {
SKShapeNode *progressBar;
float progress;
}
…
-(void)load {
[[NSNotificationCenter defaultCenter] addObserver:self name:#"anyName" selector:#selector(rcvNotification:) object:nil];
progress = 0.0;
…
progressBar = [SKShapeNode shapeNodeWithRectOfSize:CGSizeMake(self.size.width * 0.9, 2)
progressBar.xScale = 0;
progressBar.alpha = 0;
progressBar.fillColor = [UIColor whiteColor];
progressBar.strokeColor = [UIColor whiteColor];
progressBar.position = CGPointMake(self.size.width / 2, self.size.height * 0.1);
progressBar.name = #"progressBar";
…
[self addChild:progressBar]
[progressBar runAction:[SKAction fadeAlphaTo:1 duration:1]];
}
// The notification has one KVP in its user info dictionary. This is "someKey."
// "someKey"'s float value is added to "progress."
-(void)rcvNotification:(NSNotification*)notification {
progress += [[notification.userData valueForKey:#"someKey"] floatValue];
// [self updateUI];
// note: if I call updateUI like that, self will have zero children regardless of whether or not it actually has more.
// a remedy:
[self runAction:[SKAction waitForDuration:0] completion:^{
[self updateUI];
}];
}
-(void)updateUI {
NSLog(#"count of children: %lu", self.children.count);
[progressBar runAction:[SKAction scaleXTo:progress duration:0.5] completion:
// code to run when finished animating change
// also logic for reaching 100% progress.
}];
}
My predicament was, for a solid hour, not being able to re-scale that progress bar. I figured out self temporarily had 0 children, so I made it run a 0-second animation. I want to figure out why this odd behavior occurs.

How do I limit the touch input?

I am building a Sprite Kit game where the player shoots a particle whenever the screen is pressed. How can I limit the touch inputs (let's say 2 recognized touch inputs per second) so that the player can't just quickly tap the screen to get unlimited shots?
Another solution:
Create a BOOL (I prefer to work with properties myself, so):
#property (nonatomic, assign) BOOL touchEnabled;
Set it to YES in the scene's init. Then it is fairly simple from thereon:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.touchEnabled){
self.touchEnabled = NO;
[self shootParticle];
[self performSelector:#selector(enableTouch) withObject:nil afterDelay:2.0];
}
...
- (void)shootParticle{
// whatever...
}
- (void)enableTouch{
self.touchEnabled = YES;
}
One of many possibilities:
#interface YourSceneName (){
int _amountBullets; //increase every time you shot and just shoot when _fireStop = NO
BOOL _fireStop; // init as NO at start
BOOL _needStartTime; // init as YES at start
CFTimeInterval _startTime;
}
#end
-(void)update:(CFTimeInterval)currentTime {
//set starttime
if(_needStartTime){
_startTime = currentTime;
_needStartTime = NO;
}
//timeinterval if 2 seconds, renew everything
if(currentTime - _startTime > 2){
_startTime = currentTime;
_amountBullets = 0
_fireStop = NO;
}
//set firestop to yes, method should be executed
if(_amountBullets = 2){
_fireStop = YES;
}
}
I am new in SpriteKit but this should work. I bet there are better possibilities. Also i haven´t test the code. It shell show you the logic of how you could do it, hope I could help.
Here is a great Tutorial for working with time in SpriteKit.

CCNode remove not working Cocos2d

For some reason my remove function on my CCNode is not working after the animation is run.
I am adding a CCNode on click, running a animation then running the remove function.
// loads the Coin
CCNode* coin = [CCBReader load:#"heros/coin"];
coin.name =#"coin";
// position
coin.position = ccpAdd(_touchNode.position, ccp(0, 0));
//Add to Parent
[_touchNode addChild:coin z:-1];
id action1 = [CCActionMoveTo actionWithDuration:0.7f position:ccpAdd(_touchNode.position, ccp(0, 200))];
id action2 = [CCActionMoveTo actionWithDuration:0.7f position:ccpAdd(_touchNode.position, ccp(0, 100))];
CCActionCallFunc *callAfterMoving = [CCActionCallFunc actionWithTarget:self selector:#selector(cleanupSprite:)];
[coin runAction: [CCActionSequence actions:action1, action2, callAfterMoving, nil]];
using the following delete function produces a crash error with This node does not contain the specified child
- (void) cleanupSprite:(CCNode*)inSprite
{
// call your destroy particles here
// remove the sprite
[self removeChild:inSprite cleanup:YES];
}
Using the following delete also doesn't work
- (void) cleanupSprite:(CCNode*)inSprite
{
// call your destroy particles here
// remove the sprite
[self removeChildByName:#"coin" cleanup:YES];
}
self does not contain coin, _touchNode does. Do this in your callback :
[_touchNode removeChildByName:#"coin" cleanup:YES];

Stopping an SKAction

I have a sprite kit game with different scenes: the main menu ("MainMenuScene") and the game scene ("MyScene"). While the user is playing the game, I have an endless playing background music. But when the player wants to stop the game and return to main menu, the background music keeps playing. What should I do to make it stop? I have tried [self removeAllActions] but it didn't work.
MyScene:
#implementation MyScene
{
SKAction *_backgroundMusic;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.5 blue:0.3 alpha:1.0];
}
//Here I make the endless background music
_backgroundMusic = [SKAction playSoundFileNamed:#"Background 2.m4a" waitForCompletion:YES];
SKAction * backgroundMusicRepeat = [SKAction repeatActionForever:_backgroundMusic];
[self runAction:backgroundMusicRepeat];
return self;
}
- (void)selectNodeForTouch:(CGPoint)touchLocation
{
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
if ([_MainMenuButton isEqual:touchedNode]) {
SKScene *mainMenuScene = [[MainMenuScene alloc]initWithSize:self.size];
[self.view presentScene:mainMenuScene];
//Here is where the music should stop, when the player presses the 'return to main menu' button
}
}
I would not recommend using SKAction to play background music. Instead use AVAudioPlayer.
To use the AVAudioPlayer:
Add the AVFoundation to your project.
#import <AVFoundation/AVFoundation.h> into your .m file.
Add AVAudioPlayer *_backgroundMusicPlayer; in #implementation
Use this code snippet to run your audio:
- (void)playBackgroundMusic:(NSString *)filename
{
NSError *error;
NSURL *backgroundMusicURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
_backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
_backgroundMusicPlayer.numberOfLoops = -1;
_backgroundMusicPlayer.volume = 0.8;
_backgroundMusicPlayer.delegate = self;
[_backgroundMusicPlayer prepareToPlay];
[_backgroundMusicPlayer play];
}
Also read up on the AVAudioPlayer Class Reference so you know what all the properties do such as setting volume, number of loops, etc...
try this to play the music:
[self runAction:backgroundMusicRepeat withKey:#"bgmusic"];
and this to stop:
[self removeActionForKey:#"bgmusic"];
update
SKAction * backgroundMusicRepeat = [SKAction playSoundFileNamed:#"Background 2.m4a" waitForCompletion:YES];
backgroundMusicRepeat = [SKAction repeatActionForever:backgroundMusicRepeat];
[self runAction:backgroundMusicRepeat];
I have run those code in my own project and seems work.
but not your way, it only stop when I quit the view, I don't even need removeActionForKey . [self removeActionForKey:#"bgmusic"]; won't work in the scene.
So if you want stop sound while switch between diferent scenes of same view, I suggest you using AVAudioPlayer.
I also found some other questions in stackoverflow has the same problem as you, like this:
how to pause the sound when use SpriteKit
and this:
Spritekit stopping sound
they both go for the AVAudioPlayer.
as one of the comment in those links said: you should use the playSoundFileNamed method for playing sound effects... something short 1 or 2 seconds, like explosion sound --don't use it for background sound.

Spritekit animation load time

I have a simple game that I've been working on, a side scrolling space adventure where you save cats. A very simple game, but I'm running across an issue with. My game starts up normally, showing the main menu scene. Hit play, and it transititons to the game scene without issue, however, I'm finding that the first time I press start to begin the game, I get a bit of loading lag. This lag only appears the very first time you load the game.
Upon hitting start, the following method is called:
-(void)nonplayerSprites:(StartReason)endReason{
if (endReason == kStartGame){
// [self playMusic:#"Electrodoodle.mp3"];
[self spawnPlayer];
[self setupUI];
[self runAction:[SKAction repeatActionForever: [SKAction sequence:#[[SKAction
performSelector:#selector(enemySpawn) onTarget:self],
[SKAction waitForDuration:2.0]]]]];
[self runAction:[SKAction repeatActionForever: [SKAction sequence:#[[SKAction
performSelector:#selector(spawnCat) onTarget:self],
[SKAction waitForDuration:1.5]]]]];
[self runAction:[SKAction repeatActionForever: [SKAction sequence:#[[SKAction
performSelector:#selector(powerUpSpawnCheck) onTarget:self],[SKAction
waitForDuration:2.0]]]]];
} else if (endReason == kEndGame){
[self removeAllActions];
} else if (endReason == kRestartGame){
MainGame * myScene =
[[MainGame alloc] initWithSize:self.size];
SKTransition *reveal =
[SKTransition fadeWithDuration:0.5];
[self.view presentScene: myScene transition: reveal];
}
}
I have four sprite nodes that it calls, a UFO, a cat, the player rocket and a randomized power up. Two SKLabelNodes for player score and life total are called via the spawnUI method, and Additionally, music will play. Although I currently have it disabled.
The enemy and friendly nodes are all called using a similar method:
-(void)enemySpawn
{
int enemyChoice = [self getRandomNumberBetween:0 to:1];
if (enemyChoice == 0){
_enemy = [SKSpriteNode spriteNodeWithImageNamed:#"enemy"];
[_enemy setScale:1.0];
}else if (enemyChoice == 1){
_enemy = [SKSpriteNode spriteNodeWithImageNamed:#"enemy_alt"];
[_enemy setScale:0.50];
}
_enemy.anchorPoint = CGPointZero;
_enemy.zPosition = 2.0;
_enemy.name = #"rocket";
_enemy.position = CGPointMake(self.size.width + _enemy.size.width, [self
getRandomNumberBetween:0 to:250]);
SKAction *enemyMove = [SKAction sequence:#[[SKAction moveToX:-40 duration:[self
getRandomNumberBetween:2 to: 5]],
[SKAction removeFromParent]]];
[_worldNode addChild:_enemy];
[_enemy runAction:enemyMove];
}
I feel like I know what the issue is, the game data is being loaded into ram for the first time, but how do I avoid the pause while this loads? I wonder if I'm doing something wrong, because it doesn't seem like I'm loading enough to cause a noticable 1 to 3 second pause upon hitting the start button. The second time through, say after the player dies and restarts, the pause is gone and everything plays out smoothly. I apologize if I'm leaving out any important information, I'm pretty new at SpriteKit.
Anyone have some thoughts?
Thanks in advance.
I had the same problem.
It seems that when you use [SKSpriteNode spriteNodeWithImageNamed:#"enemy"] or *[SKSpriteNode spriteNodeWithImageNamed:#"enemy_alt"]* or any other texture loading method, texture is not stored in buffers for quick access so first call of those methods is giving you lags.
The solution is next :
Add properties for your game objects textures
#property(nonatomic,strong) SKTexture *enemyTexture;
.
.
.
add method to your scene subclass:
-(void)preloadTextures
{
self.enemyTexture = [SKTexture textureWithImageNamed:#"enemy"];
.
.
.
.
[SKTexture preloadTextures:#[self.enemyTexture,...] withCompletionHandler:^{
NSLog(#"Textures preloaded");
}];
}
Now call that method before presenting
MainGame * myScene =
[[MainGame alloc] initWithSize:self.size];
SKTransition *reveal =
[SKTransition fadeWithDuration:0.5];
[myScene preloadTextures];
[self.view presentScene: myScene transition: reveal];
Hope it will do the trick for you too.

Resources