Question

I'm trying to make this SKScene appear in the middle of the scene at the end of the game, so that the previous scene is still visible in the background (similar to that of the ending of flappy bird). This is an image of how I want it to appear:

enter image description here

So far this is the code I've made:

in the game scene:

-(void)dieFrom:(SKNode*)killingDebris {
    _touched = YES;

    if (_touched == YES) {
        beatLevel = YES;
        NSLog(@"touched");

        [self runAction:[SKAction sequence:@[
            [SKAction runBlock:^{
            [_goodSquare removeFromParent];
        }],
            [SKAction runBlock:^{

            level2Achieved = [[NSUserDefaults standardUserDefaults] integerForKey:@"newLevelAchieved"];
            if (beatLevel == YES) {
                level2Achieved = 2;
            }
            [[NSUserDefaults standardUserDefaults] setInteger:level2Achieved forKey:@"newLevelAchieved"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [self removeAllChildren];
        }],
            [SKAction waitForDuration:1.5],
            [SKAction runBlock:^{

            [self endGame];

        }],
                                                   ]]];

    }
}

-(void)endGame {

    [self removeAllActions];

    gameOverScene *gameOverNode = [[gameOverScene alloc] initWithScore:self.score];
    gameOverNode.gameScene = self;
    gameOverNode.position = CGPointMake(self.scene.size.width/2, -150);
    [self addChild:gameOverNode];

    [gameOverNode runAction:[SKAction moveToY:self.scene.size.height/2 duration:0.6]];
}

In the game over's header file:

@class testScene;

@interface gameOverScene : SKScene

@property (weak, nonatomic) testScene *gameScene;

-(instancetype)initWithScore:(NSInteger)score;

@end

In the game over's implementation:

-(instancetype)initWithScore:(NSInteger)score {

    if (self = [super init]) {
        self.userInteractionEnabled = YES;
        self.zPosition = 5.0;

        SKSpriteNode *bg = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(280, 300)];
        bg.alpha = 0.6;
        [self addChild:bg];
    }
    return self;
}

BUT this is what I'm getting when I test it on the simulator: enter image description here

With the following error messages:

2014-05-11 15:22:05.502 test2[2825:60b] SKScene: Setting the zPosition of a SKScene has no effect.
2014-05-11 15:22:05.503 test2[2825:60b] SKScene: Setting the position of a SKScene has no effect.
2014-05-11 15:22:05.504 test2[2825:60b] SKScene: Animating the position of a SKScene has no effect.

Can anyone tell me where I've gone wrong, and how I can fix it? Thanks. Please let me know if any extra info is needed.

Était-ce utile?

La solution

Can anyone tell me where I've gone wrong, and how I can fix it?

From the Overview section of the SKScene reference page:

A scene is the root node in a tree of Sprite Kit nodes (SKNode).

Since a tree can have only one root, any SKNode graph has just one scene.

To fix the problem, either use an SKNode (that's not a SKScene, obviously) to contain your end of game display. You can then add that node to your existing scene.

Autres conseils

Imagine Scene as separate part of your application. Scenes differ by their function, for example you can have a main menu scene, a high scores scene, a gameplay scene, a credits scene.
Each of them is very different from the other. Scene is complete entity, it holds everything that you may want from it. You do not nest scenes.

What you want to do can be accomplished with SKNode or SKSpriteNode. Just create new node and add it to your scene. Move it to some far away coordinates (like 10000, 10000).

When you want to show it just change the coordinates to your needed ones.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top