I want to Pause and Unpause a Scene in SpriteKit, with 2 Buttons on the same position.
While the Scene is running, I want to show the 'Pause' Button.
While the Scene is paused, I want to hide the 'Pause' Button and show the 'Play' Button.
In SpriteKit you can use self.scene.view.paused
which is defined in SpriteKit.
My Code:
@implementation MyScene {
SKSpriteNode *PauseButton;
SKSpriteNode *PlayButton;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
[self Pause];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode * Node = [self nodeAtPoint:location];
if([Node.name isEqualToString:@"PauseButton"]){
self.scene.view.paused = YES;
[PauseButton removeFromParent];
[self Resume];
}
if([Node.name isEqualToString:@"PlayButton"]){
self.scene.view.paused = NO;
[PlayButton removeFromParent];
[self Pause];
}
}
-(void)Pause{
PauseButton = [SKSpriteNode spriteNodeWithImageNamed:@"Pause.png"];
PauseButton.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 1.04);
PauseButton.zPosition = 3;
PauseButton.size = CGSizeMake(40, 40);
PauseButton.name = @"PauseButton";
[self addChild:PauseButton];
}
-(void)Resume{
PlayButton = [SKSpriteNode spriteNodeWithImageNamed:@"Play.png"];
PlayButton.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 1.04);
PlayButton.zPosition = 3;
PlayButton.size = CGSizeMake(60, 60);
PlayButton.name = @"PlayButton";
[self addChild:PlayButton];
}
It pauses the Scene, but the there is still the Pause Button, and if I touch the Pause Button again, the Scene resumes. So now only the Images won't change. How can I fix this?