PDA

View Full Version : Problems with Obj-C


Blorx2
2005.04.27, 08:48 PM
I'm in the middle of learning Obj-C through the FlipSquare tutorial because I've learned C++ and Lua already...I only have 3 problems/errors. :( The problems are in italics and this is the Game.h file, can anyone help me?

/* Game */

#import <Cocoa/Cocoa.h>
@class GameView, GameController;
enum { STATE_TITLE, STATE_PLAYING, STATE_GAME_OVER }
@interface Game : NSObject //parse error befor 'interface'

{
IBOutlet GameController *gameController;
IBOutlet GameView *gameView;
int state;
} //parse error before token '{'

- (void)setState: (int)newState; //fatal error metod definition not in class context (sorry, needed the space so that it wasn't a frown here on the forums)
- (int)state;
@end

kelvin
2005.04.27, 10:43 PM
1) missing semi-colon after the enum.
2) the second error is symptomatic of the first.

Blorx2
2005.04.28, 07:25 AM
ok, thanks :D

Blorx2
2005.04.28, 07:53 AM
one last problem:

/* Game */

#import <Cocoa/Cocoa.h>
@class GameView, GameController;
enum; { STATE_TITLE, STATE_PLAYING, STATE_GAME_OVER }
@interface Game : NSObject

{
IBOutlet GameController *gameController;
IBOutlet GameView *gameView;
int state;
}

- (void)setState: (int)newState; //parse error before semi-colon
- (int)state;
@end

nabobnick
2005.04.28, 08:17 AM
When he said after enum he meant this:

enum { STATE_TITLE, STATE_PLAYING, STATE_GAME_OVER }; // At the end

Blorx2
2005.04.28, 03:28 PM
oh...oops, sorry

Blorx2
2005.04.28, 04:00 PM
Sorry but: http://www.imagehosting.us/imagehosting/showimg.jpg/?id=410638
And the code:
#import "GameView.h"
#import "Game.h"
#import "GameView.h"

@implementation GameView

- (id)initWithFrame: (NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil) {
// Add initialization code here
}
return self;
}

- (void)drawRect: (NSRect)rect
{
}

switch([game state])
{
case STATE_TITLE:
[self displayTitle:rect];
break;

case STATE_PLAYING:
[self displayPlaying:rect];
break;

case STATE_GAME_OVER:
[self displayGameOver:rect];
break;

glFinish();
[[self openGLContext] flushBuffer];

- (void)displayTitle: (NSRect)rect { }

- (void)displayPlaying: (NSRect)rect {
glClearColor(0,0,1.0f,0);
glClear(GL_COLOR_BUFFER_BIT);
}

- (void)displayGameOver: (NSRect)rect { }
}

@end

Fenris
2005.04.28, 04:05 PM
First error: the drawing code isnt' inside a method/function. I suppose you don't want both a { and a } after the switch, only one makes sense. Second error, you need the OpenGL headers included: #import <OpenGL/gl.h> at the top of your file. I think the other two errors are caused by these two. :) Good luck.

Blorx2
2005.04.28, 04:08 PM
I suppose you don't want both a { and a } after the switch, only one makes sense.
Thanks and which one?

Fenris
2005.04.28, 04:10 PM
Sorry, the {. That starts a function that you're closing a bit of ways down. The way you have it now starts a function, and ends it. The code that follows it is dangling freely.

Blorx2
2005.04.28, 04:13 PM
So, what should I edit? Maybe you could fix the code by pasting it and editing as needed? Adding the OpenGL header does this, btw. wait, wrong link...

Blorx2
2005.04.28, 04:18 PM
Ok, here are the errors http://www.imagehosting.us/imagehosting/showimg.jpg/?id=410653

PowerMacX
2005.04.28, 09:36 PM
Try this:
#import "GameView.h"
#import "Game.h"
#import "GameView.h"
#import <OpenGL/gl.h>

@implementation GameView

- (id)initWithFrame: (NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil)
{
// Add initialization code here
}
return self;
}

- (void)drawRect: (NSRect)rect
{
switch([game state])
{
case STATE_TITLE:
[self displayTitle:rect];
break;

case STATE_PLAYING:
[self displayPlaying:rect];
break;

case STATE_GAME_OVER:
[self displayGameOver:rect];
break;
}

glFinish();
[[self openGLContext] flushBuffer];
}

- (void)displayTitle: (NSRect)rect
{
}

- (void)displayPlaying: (NSRect)rect
{
glClearColor(0,0,1.0f,0);
glClear(GL_COLOR_BUFFER_BIT);
}

- (void)displayGameOver: (NSRect)rect
{
}

@end

Blorx2
2005.04.29, 07:31 AM
It built but, the window didn't open nd it said this:
[Session started at 2005-04-29 07:30:38 -0400.]
ZeroLink: unknown symbol '_glClearColor'

Executable “FlipSquare” has exited due to signal 6 (SIGABRT).

kodex
2005.04.29, 09:00 AM
make sure to include the OpenGL headers and frameworks.

Blorx2
2005.04.29, 02:55 PM
Which ones?

kodex
2005.04.29, 03:01 PM
Well for started add the openGL framework then include openGL/opengl.h (i think thats the name)

Blorx2
2005.04.29, 04:38 PM
I did that one...

Blorx2
2005.04.30, 11:25 AM
The window doesn't open and I get this warning, /Users/matthew/FlipSquare/GameView.m:35: warning: `GameView' may not respond to `-openGLContext'

Fenris
2005.04.30, 11:27 AM
Is GameView a subclass of NSOpenGLView? If not, you can set that through either IB or programmatically. How did you create the GameView class?

Blorx2
2005.04.30, 11:36 AM
through IB, how would I do that programmatically?