0
votes

At the time of this post, I am using the latest versions of XCode and SpriteBuilder.

I have 2 games, one of which is built using purely Objective-C on XCode and another which uses Objective-C on XCode as well as SpriteBuilder. The non-SpriteBuilder game has a tap to play screen whose background looks the same as the in-game background. The second you tap the screen, you go straight into the game.

The Sprite Builder game is the one I am trying to implement a Start screen on. I read a few tutorials, http://www.reigndesign.com/blog/creating-a-simple-menu-with-scene-transition-in-cocos2d/, but it didn't help me. I don't want to transition from one screen to another using different background images.

This is the start screen that my non-SpriteBuilder game uses:

-(void)NewGame{
Bottom1.hidden = YES;
Bottom2.hidden = YES;
Bottom3.hidden = YES;
Bottom4.hidden = YES;
Bottom5.hidden = YES;
Bottom6.hidden = YES;
Bottom7.hidden = YES;
Top1.hidden = YES;
Top2.hidden = YES;
Top3.hidden = YES;
Top4.hidden = YES;
Top5.hidden = YES;
Top6.hidden = YES;
Top7.hidden = YES;
Obstacle.hidden = YES;
Obstacle2.hidden = YES;

Intro1.hidden = NO;
Intro2.hidden = NO;
Intro3.hidden = NO;

Heli.hidden = NO;
Heli.center = CGPointMake(31, 74);
Heli.image = [UIImage imageNamed:@"HeliUp.png"];

Start = YES;
ScoreNumber = 0;
Score.text = [NSString stringWithFormat:@"Score: 0"];
Intro3.text = [NSString stringWithFormat:@"High Score: %i", HighScore];

All obstacles are hidden except the background and the intro labels. You also see your high score and the tap to play button.

When you tap the screen, this is the next code:

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

if (Start == YES) {

    Intro1.hidden = YES;
    Intro2.hidden = YES;
    Intro3.hidden = YES;

    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(HeliMove) userInfo:nil repeats:YES];



    Scorer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(Scoring) userInfo:nil repeats:YES];

    Start = NO;

    Bottom1.hidden = NO;
    Bottom2.hidden = NO;
    Bottom3.hidden = NO;
    Bottom4.hidden = NO;
    Bottom5.hidden = NO;
    Bottom6.hidden = NO;
    Bottom7.hidden = NO;
    Top1.hidden = NO;
    Top2.hidden = NO;
    Top3.hidden = NO;
    Top4.hidden = NO;
    Top5.hidden = NO;
    Top6.hidden = NO;
    Top7.hidden = NO;
    Obstacle.hidden = NO;
    Obstacle2.hidden = NO;


    RandomPosition = arc4random() %75;
    RandomPosition = RandomPosition + 110;
    Obstacle.center = CGPointMake(570,RandomPosition);

    RandomPosition = arc4random() %75;
    RandomPosition = RandomPosition + 110;
    Obstacle2.center = CGPointMake(855,RandomPosition);

    RandomPosition = arc4random() %55;
    Top1.center = CGPointMake(560, RandomPosition);
    RandomPosition = RandomPosition +265;
    Bottom1.center = CGPointMake(560, RandomPosition);

    RandomPosition = arc4random() %55;
    Top2.center = CGPointMake(640, RandomPosition);
    RandomPosition = RandomPosition +265;
    Bottom2.center = CGPointMake(640, RandomPosition);

    RandomPosition = arc4random() %55;
    Top3.center = CGPointMake(720, RandomPosition);
    RandomPosition = RandomPosition +265;
    Bottom3.center = CGPointMake(720, RandomPosition);

    RandomPosition = arc4random() %55;
    Top4.center = CGPointMake(800, RandomPosition);
    RandomPosition = RandomPosition +265;
    Bottom4.center = CGPointMake(800, RandomPosition);

    RandomPosition = arc4random() %55;
    Top5.center = CGPointMake(880, RandomPosition);
    RandomPosition = RandomPosition +265;
    Bottom5.center = CGPointMake(880, RandomPosition);

    RandomPosition = arc4random() %55;
    Top6.center = CGPointMake(960, RandomPosition);
    RandomPosition = RandomPosition +265;
    Bottom6.center = CGPointMake(960, RandomPosition);

    RandomPosition = arc4random() %55;
    Top7.center = CGPointMake(1040, RandomPosition);
    RandomPosition = RandomPosition +265;
    Bottom7.center = CGPointMake(1040, RandomPosition);


}

Y = -7;
Heli.image = [UIImage imageNamed:@"HeliUp.png"];

}

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

Y = 7;
Heli.image = [UIImage imageNamed:@"HeliDown.png"];


}

In basic terms, all obstacles are loaded and are generated in random positions, the object moves with u tap the screen and drops if you don't tap the screen. The intro labels are hidden.

Here's the code for my SpriteBuilder game:

- (void)didLoadFromCCB {
self.userInteractionEnabled = TRUE;
_grounds = @[_ground1, _ground2];
for (CCNode *ground in _grounds) {
    // set collision txpe
    ground.physicsBody.collisionType = @"level";
    ground.zOrder = DrawingOrderGround;
_scrollSpeed = 80.f;
}
// set this class as delegate
_physicsNode.collisionDelegate = self;
// set collision txpe
_hero.physicsBody.collisionType = @"hero";
_hero.zOrder = DrawingOrdeHero;
_obstacles = [NSMutableArray array];
[self spawnNewObstacle];
[self spawnNewObstacle];
[self spawnNewObstacle];
}
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (!_gameOver) {
    [_hero.physicsBody applyImpulse:ccp(0, 400.f)];
    [_hero.physicsBody applyAngularImpulse:10000.f];
    _sinceTouch = 0.f;
}
}    

Really short code. Basically, after the logo screen disappears, the game starts right away without a start screen. When the game ends, it restarts and you immediately start playing again.

Trying to figure out how I can add another void statement to the SpriteBuilder game so that a person goes from tapping the screen then to playing the game instead of immediately going into playing the game.

I can add the viewDidLoad statement to the SpriteBuilder game but I wonder if it will conflict with the didLoadfromCCB statement. I need to have the start screen transition into the game screen using the same background. Only difference is, the start screen will hide the obstacles and show the play now labels, then the labels will hide and obstacles become visible in the game screen.

2

2 Answers

1
votes

I would suggest doing it following the spritebuilder tutorial. This will probably require you to rejig your code. The following tutorial also helps you with linking spritebuilder to backend code, and also how to transition between scenes.

I'm not 100% sure why you included so much detail in your question, I'm confused with what is game logic, and what is other logic... So If I'm off the mark, let me know and I'll update my answer.

https://www.makegameswith.us/tutorials/getting-started-with-spritebuilder/menus/

0
votes

Smells a bit like Flappy Bird to me.

Either way I would implement it with a gameState variable and a layer with the menu in.

When you open the game (viewDidLoad) set the gameState to 0 e.g. Menu

then in the touch method I would have a switch like the following:

switch(gameState){
    case 0: //Menu
        menuLayer.hidden = YES;
        gameState = 1 //IN_GAME
        break;
    case 1: //In game
        //Do your game tap code
        break;
    case 2: //Game Over
        gameOverLayer.hidden = NO;
        break;
}

Or you could duplicate the CCb and remove the game code and load the game cCB on touch, but I think the former is the best option.