0

I'm writing the game in cocos2d (from Pablo Ruiz's book). Right now I have to create pause screen, and, according to the book, I have to create new function in AppDelegate.m (and in .h file) :

+(AppDelegate *) get {

return (AppDelegate *) [[UIApplication sharedApplication] delegate];
}

I'm getting errors : Expected a type; expected expression; Missing '[' at start of message send expression; Use of undeclared identifier 'AppDelegate'.

In another file, called GameScene.m, I created those functions:

-(void)resume
{
if(![AppDelegate get].paused)
{
    return;
}
[AppDelegate get].paused = NO;
[self onEnter];
}

-(void)onExit
{
if(![AppDelegate get].paused)
{
    [AppDelegate get].paused = YES;
    [super onExit];
}
}

-(void)onEnter
{
if(![AppDelegate get].paused)
{
    [super onEnter];
}
}

And I'm getting another set of errors : Use of undeclared identifier 'AppDelegate', four times.

Can someone explain me how to get rid of those errors?

  • Do you `#import` your AppDelegate properly in your GameScene file? – de. Mar 24 '13 at 15:25
  • 1
    Tip: if you learn from a book, be sure to use the EXACT same version of the software described/used in the book. If you get this issue resolved, there'll be dozens of similar ones waiting for you. – CodeSmile Mar 24 '13 at 19:30

2 Answers2

1

Make sure you have added the following to your GameScene.h:

@class AppDelegate;

This will let the compiler know that the class AppDelegate exists. And in your GameScene.m:

#import "AppDelegate.h"

This lets you access AppDelegate members and methods.

de.
  • 7,068
  • 3
  • 40
  • 69
  • Thanks, it solved a few issues, but there are more. First, I got errors : Receiver 'AppDelegate' is a forward class and corresponding @interface may not exist; Property 'paused' cannot be found in forward class object 'AppDelegate'. Those two errors are repeated 5 times each). And there is one more error, concerning AppDelegate.m - Expected a type. – Artur Bartczak Mar 24 '13 at 16:21
  • That sounds as if you are trying to access members of your `AppDelegate` in a .m file that does not #import AppDelegate. Try to add the #import in the header file as well. This is not a good style and you should try to avoid it. Familiarize yourself with the concept of forward declaration. See this link: http://stackoverflow.com/a/5191507/921573 – de. Mar 24 '13 at 21:22
  • Hello. I have imported everything in every place, and still nothing. Still I recieve errors that I mentioned at the beggining. What to do? – Artur Bartczak Mar 29 '13 at 14:02
  • Ok, I'm starting to guess now...maybe you have forgotten to add some library or framework to you build phase? (Select your project file from the navigator, select your target, select build phases, check target dependencies / link binary with libraries) – de. Mar 29 '13 at 14:43
  • I checked everything. I also tried to rename every AppDelegate to AppController, but it doesn't work either – Artur Bartczak Mar 29 '13 at 16:08
1

Cocos2d 2.0? Then Use AppController.

#import "AppDelegate.h"

AppController *app = (AppController*)[UIApplication sharedApplication].delegate;
Guru
  • 21,652
  • 10
  • 63
  • 102