Pregunta

I'm trying to create a replay button so the user can simply replay instead of going back to the level selection menu in a game I'm creating. The game is a sprite-kit game for iOS 7. The problem I'm having is resetting the integer. For some reason it stays at 0. All the rest of the scene contents replace themselves in the initWithSize method but even though I declare the integers value of 5 in the initWithSize method, it doesn't reset the value, instead it stays at 0.

After the gameOverNode for my game appears, there is a button that says "Replay", which I've set up to load the scene, this is the code:

if ([node.name isEqualToString:@"reTry"]) {
    level2 *repeat = [[level2 alloc] initWithSize:self.size];
    [self.view presentScene:repeat transition:[SKTransition fadeWithColor:[SKColor whiteColor] duration:0.5]];
}

And as stated, everything in the scene resets (i.e. positions of SKSpriteNodes, etc), except for the value of the integer, which stays at 4.

Why is this, and how can I reset the value of the integer?

Is there a way to clear the scene before reloading it?

This is the initWithSize method:

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

        movesRemaining = 4;
¿Fue útil?

Solución

What you have declared is a class object. You need to declare it as an instance variable instead.

Instead of

#import <SKScene/SKScene.h>
.
.

int movesRemaining;

You need to declare it like so:

@interface level2 : SKScene
{  
    int movesRemaining;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top