Ok, i never did something useful for this community so i would like to try to remedy^^'
I'm also in trouble learning Cocoa so i decided that my first experiment will be entirely done within the Sparrow framework... i'll have time to add cocoa's native features in a second release if the first one will have some kind of success.
This is a way to add menus using only Sparrow's syntax:
#import "Game.h"
@implementation Game
-(id)initWithWidth:(float)width height:(float)height {
// we start showing the mainMenu
// - we also set a container for our game's elements
if (self = [super initWithWidth:width height:height]) {
mContents = [SPSprite sprite];
mContents = [[SPSprite alloc] init];
[self addChild:mContents];
[self mainMenu];
}
return self;
}
-(void)mainMenu{
// add the main menu to the stage
// - we add a background image and a button to start
mainMenuImage = [SPImage imageWithContentsOfFile:@"mainMenuBkg.png"];
mainMenuScreen = [SPSprite sprite];
[mainMenuScreen addChild:mainMenuImage];
[mContents addChild:mainMenuScreen];
startButton = [SPButton buttonWithUpState:[SPTexture textureWithContentsOfFile:@"buttonStart.png"]];
[mainMenuScreen addChild:startButton];
[startButton addEventListener:@selector(onStartButtonTriggered:) atObject:self
forType:SP_EVENT_TYPE_TRIGGERED];
}
-(void)onStartButtonTriggered:(SPEvent*)event{
// make the game start
[self start];
}
-(void)start{
// begin a new game
// - here we set all our game's variables and start the main game cycle
[mContents removeAllChildren];
hero = [SPImage imageWithContentsOfFile:@"hero.png"];
[mContents addChild:hero];
hero.x = 0;
[self addEventListener:@selector(onEnterFrame:) atObject:self
forType:SP_EVENT_TYPE_ENTER_FRAME];
}
-(void)onEnterFrame:(SPEnterFrameEvent *)event{
// game main cycle
// - moves hero, enemies etc here
hero.x += 1;
if (hero.x > 320) {
[self gameOver];
}
}
-(void)gameOver{
// add the GameOver screen
// - we remove the onEnterFrame event and show a background with a Reset button
[self removeEventListener:@selector(onEnterFrame:) atObject:self
forType:SP_EVENT_TYPE_ENTER_FRAME];
gameOverImage = [SPImage imageWithContentsOfFile:@"gameOverBkg.png"];
gameOverScreen = [SPSprite sprite];
[gameOverScreen addChild:gameOverImage];
[mContents addChild:gameOverScreen];
resetButton = [SPButton buttonWithUpState:[SPTexture textureWithContentsOfFile:@"back2menu.png"]];
[resetButton addEventListener:@selector(onResetButtonTriggered:) atObject:self
forType:SP_EVENT_TYPE_TRIGGERED];
[gameOverScreen addChild:resetButton];
}
-(void)onResetButtonTriggered:(SPEvent*)event{
// we go back to the main menu
[self mainMenu];
}
@end
I did not double checked what i wrote but it's very similar to how my game actually works so i hope that this could be a decent way to postpone the problem when we'll be more experienced.
If not please make me know!^^'
Edit: corrected some typos and mistakes but take it just as an aexample and not as a fully working code