0

I have a game with a menu scene, a play scene, and a game over scene. When I am on one of them is there a way to not run the others in the background. For example on my play scene when you make contact with something it switches to the game over scene. When I'm on the menu scene the play scene runs and when it makes contact it switches to the game over scene.

So My Question is: Can I make it so that the scene won't run in the background? Or a way to delay it until I press the play button on the menu?

Here is the code for the first scene:

    #import "WEMenuScene.h"
    #import "WEMyScene.h"

    @implementation WEMenuScene

    -(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
    /* Setup your scene here */
    self.scaleMode = SKSceneScaleModeAspectFill;
    SKSpriteNode* background = [SKSpriteNode spriteNodeWithImageNamed:@"landscape"];
    background.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
    background.zPosition = 1000;
    [self addChild:background];
    [self addChild:[self playButton]];




}
return self;
}


-(SKSpriteNode *) playButton {

SKSpriteNode* play = [SKSpriteNode spriteNodeWithImageNamed:@"Play"];
play.position = CGPointMake(CGRectGetMidX(self.frame), 300);
play.zPosition = 1200;
play.name = @"playButton";

return play;



 }


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode* node = [self nodeAtPoint:location];


if ([node.name isEqualToString:@"playButton"]) {
    SKScene* playScene = [[WEMyScene alloc] initWithSize:self.size];
    SKTransition* transitionPlay = [SKTransition doorsOpenVerticalWithDuration:0.5];
    [self.view presentScene:playScene transition:transitionPlay];
}

}


@end

Here is the code for the second scene:

    #import "WEMyScene.h"
    #import "WECapturedScene.h"


    @implementation WEMyScene



    -(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
    self.scaleMode = SKSceneScaleModeAspectFill;
    [self performSelector:@selector(logs) withObject:nil afterDelay:3.0];
    [self performSelector:@selector(moveBackground) withObject:nil afterDelay:0.0];
    [self addChild:[self createCharacter]];
    [self setUpActions];
}
return self;
}

 -(SKSpriteNode *) createCharacter {

SKSpriteNode* holly = [SKSpriteNode spriteNodeWithImageNamed:@"holly1"];
holly.position = CGPointMake(CGRectGetMidX(self.frame), 185);
holly.name = @"holly";
holly.zPosition = 40;

return holly;

}

-(void) logs {

CGPoint startPoint = CGPointMake(480, 175);

SKSpriteNode* logs = [SKSpriteNode spriteNodeWithImageNamed:@"log"];
logs.position = CGPointMake(startPoint.x, startPoint.y);
logs.name = @"logs";
logs.zPosition = 40;

[self addChild:logs];




float spawnLog = arc4random_uniform(3)+ 1.4;
[self performSelector:@selector(logs) withObject:nil afterDelay:spawnLog];




}

-(void) setUpActions {


SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"Holly"];

SKTexture *movetex1 = [atlas textureNamed:@"holly1"];
SKTexture *movetex2 = [atlas textureNamed:@"holly2"];
SKTexture *movetex3 = [atlas textureNamed:@"holly3"];
SKTexture *movetex4 = [atlas textureNamed:@"holly4"];
SKTexture *movetex5 = [atlas textureNamed:@"holly5"];
SKTexture *movetex6 = [atlas textureNamed:@"holly6"];
SKTexture *movetex7 = [atlas textureNamed:@"holly7"];

NSArray *atlasTexture = @[movetex1, movetex2, movetex3, movetex4, movetex5, movetex6, movetex7];

SKAction* atlasAnimation =[SKAction repeatActionForever:[SKAction animateWithTextures:atlasTexture timePerFrame:0.08]];
hollyMovement = [SKAction sequence:@[atlasAnimation]];

    SKSpriteNode* holly = (SKSpriteNode*)[self childNodeWithName:@"holly"];
    holly.zPosition = 40;
    [holly runAction:hollyMovement];

SKAction* moveUp = [SKAction moveByX:0 y:90 duration:0.50];
SKAction* wait = [SKAction moveByX:0 y:0 duration:0.4];
SKAction* moveDown = [SKAction moveByX:0 y:-90 duration:0.4];
SKAction* done = [SKAction performSelector:@selector(jumpDone) onTarget:self];
hollyUp = [SKAction sequence:@[moveUp, wait, moveDown, done]];



}

-(void) jumpDone {

isJumping = NO;


}


-(void) moveBackground {

CGPoint startPoint = CGPointMake(480, 230);

SKSpriteNode* landscape = [SKSpriteNode spriteNodeWithImageNamed:@"landscape"];
landscape.position = CGPointMake(startPoint.x, startPoint.y);
landscape.name = @"landscape";
landscape.zPosition = 1;
[self addChild:landscape];

float spawnbackground = 0.7;
[self performSelector:@selector(moveBackground) withObject:nil afterDelay:spawnbackground];



}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */

if (isJumping == NO) {
    isJumping = YES;
    SKSpriteNode* holly = (SKSpriteNode*)[self childNodeWithName:@"holly"];
    [holly runAction:hollyUp];
}

}

-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */

SKNode* holly = [self childNodeWithName:@"holly"];

[self enumerateChildNodesWithName:@"landscape" usingBlock:^(SKNode *node, BOOL *stop) {
    if (node.position.x < 0 || node.position.y < 0) {
        [node removeFromParent];
    }else {

        node.position = CGPointMake(node.position.x - 10, node.position.y);
    }
}];

[self enumerateChildNodesWithName:@"logs" usingBlock:^(SKNode *node, BOOL *stop) {
    if (node.position.x < 0 || node.position.y < 0) {
        [node removeFromParent];

    }else {

        node.position = CGPointMake(node.position.x - 10, node.position.y);
    }

    if ([holly intersectsNode:node]) {
        SKScene *capturedScene = [[WECapturedScene alloc] initWithSize:self.size];
        SKTransition* transition = [SKTransition doorsOpenVerticalWithDuration:0.5];
        [self.view presentScene:capturedScene transition:transition];
    }
}];

[self enumerateChildNodesWithName:@"dogCatcher" usingBlock:^(SKNode *node, BOOL *stop) {

        node.position = CGPointMake(node.position.x + 10, node.position.y);
}];

}

Here is the view controller:

#import "WEViewController.h"
#import "WEMyScene.h"
#import "WEMenuScene.h"

@implementation WEViewController

- (void)viewDidLoad
{
[super viewDidLoad];

// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;

// Create and configure the scene.
SKScene * scene = [WEMenuScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;

// Present the scene.
[skView presentScene:scene];
}

- (BOOL)shouldAutorotate
{
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
    return UIInterfaceOrientationMaskAll;
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

@end








@end
user3762780
  • 45
  • 1
  • 6
  • Do you actually experience the problem where one scene keeps running in the background? Because since you have to call presentScene and only one scene can be presented at a time, there can't be scenes running in the background. Unless you were to update a background scene's code from another scene's update method, for instance. Which would probably indicate a programming bug, which in turn isn't debuggable without seeing the code. – CodeSmile Jun 28 '14 at 14:37
  • What's happening is I can see part of the scene running and the obstacles come and hit the character and it automatically changes the scene to the game over scene. – user3762780 Jun 28 '14 at 15:47
  • 1
    Hmmmm does the view controller's viewWillLayoutSubviews check if skScene.view is nil before presenting the scene? If not that would explain the seemingly random scene change. – CodeSmile Jun 28 '14 at 17:04
  • I just posted the view controlller – user3762780 Jun 28 '14 at 17:27
  • How do I add the viewWillLayoutSubviews – user3762780 Jun 28 '14 at 22:28

0 Answers0