Pause Menu Game States

General discussion about iOS development - a great place to ask questions about iOS or help out others. For discussion of the official tutorials, see the Official Tutorials forum.

Pause Menu Game States

Postby Abigail » Thu Aug 01, 2013 5:33 am

Hi,
In my cocos2d game I had it so there was a pause menu and when you pressed pause the game actions would freeze until you press resume. So far it just starts over. I passed a variable named path to take record of the actions in the game scene. It says it is unused variable as I declare it in init : NSString *path = p;. And implementation: NSString *path;

This is for saving the data in hello world:
Code: Select all
– (void) saveData
{
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

//here add elements to data file and write data to file
int value = 5;

[data setObject:[NSNumber numberWithInt:value] forKey:@”value”];

[data writeToFile: path atomically:YES];
[data release];
}

Code: Select all
- (void) readData
{
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

//load from savedStock example int value
int value;
value = [[savedStock objectForKey:@"value"] intValue];

[savedStock release];
}

- (void) createProperty
{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
path = [documentsDirectory stringByAppendingPathComponent:@"Pause Menu Property List.plist"]; //3

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@”data” ofType:@”plist”]; //5

[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}
NSLog(@”PLIST INITIALIZED”);

}

- (void) PauseGame: (id) sender
{
[self saveData];
[self pauseSchedulerAndActions];

// save the layer

[[CCDirector sharedDirector] replaceScene:[PauseLayer node]];
}


It is passed to the pause menu .h:

Code: Select all
// Give id path a identifier
-(id)initWithPath:(BOOL)path withPath:(int)p;

// returns a CCScene that contains the PauseLayer.h as the only child
+(CCScene *) sceneWithPath:(BOOL)path withPath:(int)p;


Pause Menu .m:
Code: Select all
-(id)initWithPath:(BOOL)path withPath:(int)p;
{
if( (self=[super init]))
{

NSString *path = p;.
}
}
Abigail
Uber Haxx0r
 
Posts: 67
Joined: Wed May 22, 2013 1:21 am
Has thanked: 3 times
Been thanked: 0 time

Re: Pause Menu Game States

Postby marciokoko » Tue Aug 06, 2013 1:36 am

Ok you need to send the path variable into your saveData method or make it a property for it to be used in that method.

So either add a property for path variable, or change your method to saveData:(NSString*)path and when you call it, end your path variable along.
--------------
"Stay Hungry, Stay Foolish!"
Mars
santiapps.com
@santiapps
MBA-2011-iPhone5s-iPad3
User avatar
marciokoko
Team Member
Forum Subject Matter Expert
 
Posts: 2363
Joined: Wed Jun 08, 2011 9:37 pm
Has thanked: 4 times
Been thanked: 333 times

Re: Pause Menu Game States

Postby Abigail » Tue Aug 06, 2013 5:25 am

I changed it to be -(id)saveData:(NSString*)path withPath:(int)p; for the init method and the .h file but it still says that path is an used variable. So than what should I do from there?
Abigail
Uber Haxx0r
 
Posts: 67
Joined: Wed May 22, 2013 1:21 am
Has thanked: 3 times
Been thanked: 0 time

Re: Pause Menu Game States

Postby marciokoko » Tue Aug 06, 2013 2:56 pm

You need to actually pass the variable from one method to another.

path is created in createProperty method only, correct?

It would be best to just make a property out of it. Change your saveData method back to what it was and just add a property to your class,

Code: Select all
@property (nonatomic, strong) NSString *path;

and then access path wherever you need.
--------------
"Stay Hungry, Stay Foolish!"
Mars
santiapps.com
@santiapps
MBA-2011-iPhone5s-iPad3
User avatar
marciokoko
Team Member
Forum Subject Matter Expert
 
Posts: 2363
Joined: Wed Jun 08, 2011 9:37 pm
Has thanked: 4 times
Been thanked: 333 times

Re: Pause Menu Game States

Postby Abigail » Sat Aug 10, 2013 12:22 am

I have added the: // Adds a property to save data in a class @property (nonatomic, strong) NSString *path; in the .h file and still is saying the same thing: Unused Variable Path.It also says for @implementation HelloWorldLayer that: Autosynthesized property 'path' will use synthesized instance variable '_path', not existing instance variable 'path'. What should I do instead if properties don't work?
Abigail
Uber Haxx0r
 
Posts: 67
Joined: Wed May 22, 2013 1:21 am
Has thanked: 3 times
Been thanked: 0 time

Re: Pause Menu Game States

Postby marciokoko » Sun Aug 11, 2013 3:31 am

Could you please post your complete code. Its kinda hard to figure out whats going on.

What I can tell you is that if you define NSString *path = p; in your init method, that makes path a local variable, its scope is the method and thus cannot be used elsewhere in another method. So if in some other method you try to do something with path, it wont work. If you make it into a property then you have to set it and access it as self.path. Otherwise you can make it an instance variable as well.
--------------
"Stay Hungry, Stay Foolish!"
Mars
santiapps.com
@santiapps
MBA-2011-iPhone5s-iPad3
User avatar
marciokoko
Team Member
Forum Subject Matter Expert
 
Posts: 2363
Joined: Wed Jun 08, 2011 9:37 pm
Has thanked: 4 times
Been thanked: 333 times

Re: Pause Menu Game States

Postby Abigail » Mon Aug 19, 2013 5:54 pm

Here is the full code for the pause menu:
Hello World.h

Code: Select all
// Give id path a identifier
-(id)initWithPath:(BOOL)path withPath:(int)p;

// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) sceneWithPath:(BOOL)path withPath:(int)p;


Hello World.m

Code: Select all
-(id)initWithPath:(BOOL)path withPath:(int)p;
{
    // always call "super" init
    // Apple recommends to re-assign "self" with "super" return value
    if ((self = [super initWithColor:ccc4(255,255,255,255)])) {
       
        [self setTouchEnabled:YES];

        NSString *path = p;
       
        CGSize winSize = [CCDirector sharedDirector].winSize;
        CCSprite *SpeedNinja = [CCSprite spriteWithFile:@"SpeedNinjaFix.png"];
        // SpeedNinja.position = ccp(SpeedNinja.contentSize.width/2, winSize.height/2);
        SpeedNinja.position = ccp(80, 180);
        float tmpx = SpeedNinja.contentSize.width/2;
        NSLog(@"X %f", tmpx);
        float tmpy = winSize.height/2;
        NSLog(@"Y %f", tmpy);
        [self addChild:SpeedNinja z:2];
       
        [self schedule:@selector(gameLogic:) interval:1.0];
       
        [self setTouchEnabled:YES];
       
        _monsters = [[NSMutableArray alloc] init];
        _projectiles = [[NSMutableArray alloc] init];
       
        [self schedule:@selector(update:)];
       
        [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"Ninja Background Music.mp3"];
       
        // Set the score to zero.
        score = 0;
       
        // Set Monsters Killed to zero.
        MonstersKilled = 0;
       
        // Set the score to zero.
        Wave = 0;
       
        // Create and add the score label as a child.
        scoreLabel = [CCLabelTTF labelWithString:@"0" fontName:@"Arial" fontSize:20];
        scoreLabel.position = ccp(120, 300);
        [self addChild:scoreLabel z:1];
       
        // Create label to name the score.
        scoreNameLabel = [CCLabelTTF labelWithString:@"Score:" fontName:@"Arial" fontSize:20];
        scoreNameLabel.position = ccp(60, 300);
        [self addChild:scoreNameLabel z:7];
       
        // Create and add the Monsters Killed label as a child.
        MonstersKilledLabel = [CCLabelTTF labelWithString:@"0" fontName:@"Arial" fontSize:20];
        MonstersKilledLabel.position = ccp(290, 300);
        [self addChild:MonstersKilledLabel z:6];
       
        // Create label to name the Monsters Killed.
        MonstersKilledNameLabel = [CCLabelTTF labelWithString:@"NinjasKilled:" fontName:@"Arial" fontSize:20];
        MonstersKilledNameLabel.position = ccp(210, 300);
        [self addChild:MonstersKilledNameLabel z:8];
       
        // Create Label for the number of waves.
        MonstersWavesNameLabel = [CCLabelTTF labelWithString:@"Wave: " fontName:@"Arial" fontSize:20];
        MonstersWavesNameLabel.position = ccp(350, 300);
        [self addChild:MonstersWavesNameLabel z:21];
       
        // Create and add the wave label as a child.
        WaveLabel = [CCLabelTTF labelWithString:@"1" fontName:@"Arial" fontSize:20];
        WaveLabel.position = ccp(390, 300);
        [self addChild:WaveLabel z:22];
       
        CCLayer *HelloWorldLayer = [[CCLayer alloc] init];
        [self addChild:HelloWorldLayer];

        CCMenuItemImage *PauseButton = [CCMenuItemImage
                                        itemWithNormalImage:@"PauseButton.png"
                                        selectedImage:@"PauseButtonSelected .png"
                                        target:self
                                        selector:@selector(PauseGame:)];
        PauseButton.position = ccp(250, -150);
       
        CCMenu *Pause = [CCMenu menuWithItems:PauseButton, nil];
        [self addChild:Pause z:24];
        NSLog(@"Pause Button");
       
        CCSprite *background = [CCSprite spriteWithFile:@"Speed Ninja Background iPhone 5.bmp"];
        background.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:background];
       
        SpeedNinjaHealthBar = [[CCDrawNode alloc] init];
        SpeedNinjaHealthBar.contentSize = CGSizeMake(100, 0);
        [self addChild:SpeedNinjaHealthBar];
       
        SpeedNinja.position = ccp(SpeedNinja.position.x - HealthBarWidth/2.0f + 0.5f,
        SpeedNinja.position.y - SpeedNinja.contentSize.height/2.0f - 15.0f + 0.5f);
       
        SpeedNinjaHP = MaxHP;
        WaveLevel = 0;
    }
   
    [self createProperty];
   
    return self;
}

- (void) playBackgroundMusic: (NSString *) filePath
{
    loop: [[SimpleAudioEngine sharedEngine] playEffect:@"Ninja Background Music.mp3"];
}

- (void)drawHealthBar:(CCDrawNode *)node hp:(int)hp
{
    [node clear];
   
    CGPoint verts[4];
    verts[0] = ccp(0.0f, 0.0f);
    verts[1] = ccp(0.0f, HealthBarHeight - 1.0f);
    verts[2] = ccp(HealthBarWidth - 1.0f, HealthBarHeight - 1.0f);
    verts[3] = ccp(HealthBarWidth - 1.0f, 0.0f);
   
    ccColor4F clearColor = ccc4f(0.0f, 0.0f, 0.0f, 0.0f);
    ccColor4F fillColor = ccc4f(113.0f/255.0f, 202.0f/255.0f, 53.0f/255.0f, 1.0f);
    ccColor4F borderColor = ccc4f(35.0f/255.0f, 28.0f/255.0f, 40.0f/255.0f, 1.0f);
   
    [node drawPolyWithVerts:verts count:4 fillColor:clearColor borderWidth:1.0f borderColor:borderColor];
   
    verts[0].x += 0.5f;
    verts[0].y += 0.5f;
    verts[1].x += 0.5f;
    verts[1].y -= 0.5f;
    verts[2].x = (HealthBarWidth - 2.0f)*hp/MaxHP + 0.5f;
    verts[2].y -= 0.5f;
    verts[3].x = verts[2].x;
    verts[3].y += 0.5f;
   
    [node drawPolyWithVerts:verts count:4 fillColor:fillColor borderWidth:0.0f borderColor:clearColor];
}

- (void) saveData
{
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

    //here add elements to data file and write data to file
    int value = 5;

    [data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
   
    [data writeToFile: path atomically:YES];
    [data release];
}

- (void) readData
{
    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

    //load from savedStock example int value
    int value;
    value = [[savedStock objectForKey:@"value"] intValue];

    [savedStock release];
}

- (void) createProperty
{
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2
   path = [documentsDirectory stringByAppendingPathComponent:@"Pause Menu Property List.plist"]; //3
   
    NSFileManager *fileManager = [NSFileManager defaultManager];
   
    if (![fileManager fileExistsAtPath: path]) //4
    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; //5
       
        [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
    }
    NSLog(@"PLIST INITIALIZED");
   
}

- (void) PauseGame: (id) sender
{
    [self saveData];
    [self pauseSchedulerAndActions];
   
    // save the layer
   
   
    [[CCDirector sharedDirector] replaceScene:[PauseLayer node]];
}


Pause Menu.m

Code: Select all
+(CCScene *) sceneWithPath:(BOOL)path withPath:(int)p
{
    CCScene *scene = [CCScene node];
   
    PauseLayer *layer = [PauseLayer node];
   
    [scene addChild: layer];
   
    return scene;
}

-(id)initWithPath:(BOOL)path withPath:(int)p;
{
    if( (self=[super init]))
    {
        NSString *path = p;
       
        CGSize winsize = [CCDirector sharedDirector].winSize;
        PauseLabel = [CCLabelTTF labelWithString:@"Pause" fontName:@"Arial" fontSize:24];
        // position the label at the center top of your layer
        PauseLabel.position = ccp(winsize.width/2, winsize.height*0.90);
        [self addChild: PauseLabel];
       
        CCMenuItemImage *MainMenu = [CCMenuItemImage
                                     itemWithNormalImage:@"GoToMainMenuPause .png"
                                     selectedImage:@"GoToMainMenuButtonSelected.png"
                                     target:self selector:@selector(Main:)];
       
        CCMenuItemImage *BackToGame= [CCMenuItemImage
                                      itemWithNormalImage:@"BackToGamePause.png"
                                      selectedImage:@"BackToGameButtonSelected.png"
                                      target:self selector:@selector(BacktoGame:)];
       
        CCMenu *Pause = [CCMenu menuWithItems: BackToGame, MainMenu, nil];
        [self addChild: Pause];
       
        // position your pause menu in the center of your layer
        Pause.position = ccp(winsize.width/2,winsize.height/2);
        // align them vertically
        [Pause alignItemsVertically];
               

    }
    return self;
}

- (void) Main: (id) sender
{
    [[CCDirector sharedDirector] replaceScene:[MainMenuScene scene]];
}

- (void) BacktoGame: (id) sender
{
    [[CCDirector sharedDirector] replaceScene:[HelloWorldLayer node]];
   
}
Last edited by marciokoko on Tue Aug 20, 2013 2:12 am, edited 1 time in total.
Reason: code tags
Abigail
Uber Haxx0r
 
Posts: 67
Joined: Wed May 22, 2013 1:21 am
Has thanked: 3 times
Been thanked: 0 time

Re: Pause Menu Game States

Postby marciokoko » Tue Aug 20, 2013 2:20 am

Oh ok, so you have the path variable in HelloWorld and want to pass it to the PauseMenu Class, correct?

I assume you instantiate a PauseMenu object inside HelloWorld and then call it to be presented on screen, right?

So in HelloWorld just say:

PauseMenu *myPauseMenu = [[PauseMenu alloc] initWithPath:path];

And in your PauseMenu initWithPath menu you receive your path variable.
--------------
"Stay Hungry, Stay Foolish!"
Mars
santiapps.com
@santiapps
MBA-2011-iPhone5s-iPad3
User avatar
marciokoko
Team Member
Forum Subject Matter Expert
 
Posts: 2363
Joined: Wed Jun 08, 2011 9:37 pm
Has thanked: 4 times
Been thanked: 333 times

Re: Pause Menu Game States

Postby Abigail » Tue Aug 20, 2013 5:42 pm

Just to make sure I put it in the init method right? Also should I keep the NSString *path or delete it. So when I put in PauseMenu *myPauseMenu = [[PauseMenu alloc] initWithPath:path]; it says undeclared identifier myPauseMenu also should I verify that as I did with the NSString *path and what should I identify it as? Thanks so much! :D
Abigail
Uber Haxx0r
 
Posts: 67
Joined: Wed May 22, 2013 1:21 am
Has thanked: 3 times
Been thanked: 0 time

Re: Pause Menu Game States

Postby Abigail » Thu Aug 22, 2013 11:12 pm

Also when I run the level it gives me totally black screen. I think it is because I is pausing the game even before I press the pause button so do I need to fix and tell the pause menu when to pause the game? Thanks
Abigail
Uber Haxx0r
 
Posts: 67
Joined: Wed May 22, 2013 1:21 am
Has thanked: 3 times
Been thanked: 0 time

Next

Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 7 guests