Working with Flipsquare
I was working with the tutorial found in the resource topic.http://www.idevgames.com/forum/showthread.php?t=13951 and i began to do the Flipsquare tutorial. I did followed all the instructions and did all entered everything right but when I was about to run the program my last file GameView.m had a syntax error. Here it is
Im new at objective c and any other language for that matter. Im trying to get a hang of it but this has stopped me from continuing. I appreciate your help.
Code:
#import "GameView.h"
#import "Game.h"
#import "GameController.h"
@implementation GameView
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;
}
- (void)displayTitle: (NSRect)rect
{
}
- (void)displayPlaying: (NSRect)rect
{
[[NSColor blueColor] set];
NSRectFill([self bounds]);}
- (void)displayGameOver: (NSRect)rect
{
}
@end
You didn't say what the error is...
OneSadCookie Wrote:You didn't say what the error is...sorry I forgot.
it says that there are two syntax errors before 'Switch'
I think we'll need to see your .h files. But I may be wrong.
The switch statement needs to be in a method, probably the displayTitle method.
Hairball183 Wrote:I think we'll need to see your .h files. But I may be wrong.
ok here are the .h files.
GameView.h
Code:
#import <Cocoa/Cocoa.h>
@class Game, GameController;
@interface GameView : NSView {
IBOutlet Game *game;
IBOutlet GameController *gameController;
}
- (void)displayTitle:(NSRect)rect;
- (void)displayPlaying:(NSRect)rect;
- (void)displayGameOver:(NSRect)rect;
@endGameController.h
Code:
#import <Cocoa/Cocoa.h>
@class Game, GameView;
@interface GameController : NSObject {
IBOutlet Game *game;
IBOutlet GameView *gameView;
}
@endGame.h
Code:
#import <Cocoa/Cocoa.h>
enum { STATE_TITLE, STATE_PLAYING, STATE_GAME_OVER };
@class GameController, GameView;
@interface Game : NSObject {
IBOutlet GameController *gameController;
IBOutlet GameView *gameView;
int state; // current game state
}
- (void)setState:(int)newState;
- (int)state;
@endOneSadCookie Wrote:The switch statement needs to be in a method, probably the displayTitle method.
tried it but the syntax error gets passed over to the next statement. "syntax error before case STATE_TITLE"
the whole block switch (.....) { ......... } needs to go in the method
OneSadCookie Wrote:the whole block switch (.....) { ......... } needs to go in the methodwhoa it worked. I know I may be asking much, but could you explain the logic behind this.
Thank you.
When an object receives a message it finds the corresponding method and runs the code therein. All your code has to be inside a method for it to be run, so putting code outside a method is a syntax error (actually you can use C functions outside of a class declaration, but ignore that just now).
In the code sample in your original post you have a method which is not closed (-(void) displayPlaying: (NSRect)rect) which should be giving you errors. Also, if you put that switch statement inside the displayTitle method (as OSC suggested) it will try to call that method again from inside itself and end up stuck in a big self-referential loop until it crashes. I suspect that it was probably supposed to be in drawRect: which is inherited from the NSView class, as that is the method which is called when the view needs to draw itself. The switch statement will then call one of a number of drawing methods depending on the state of the game.
In the code sample in your original post you have a method which is not closed (-(void) displayPlaying: (NSRect)rect) which should be giving you errors. Also, if you put that switch statement inside the displayTitle method (as OSC suggested) it will try to call that method again from inside itself and end up stuck in a big self-referential loop until it crashes. I suspect that it was probably supposed to be in drawRect: which is inherited from the NSView class, as that is the method which is called when the view needs to draw itself. The switch statement will then call one of a number of drawing methods depending on the state of the game.
Thank you in the instructions it said something about the drawRect method but I did not know where to put it. Thank you for the info. oh and one last thing, the instructions say that after I build it there is supposed to be a blue rectangle but its just showing a grey screen. oh and sorry to ask but is this how you put the drawRect method?
thank you.
Code:
- (void)drawrect: (NSRect)rect
After seeing this thread I thought I'd dig out the code and have a play around with it.
However I do not seem to have it on any of my Macs at home - all I could find was the tutorial LaTeX files.
Hopefully I can find the source on one of my office Macs.
However I do not seem to have it on any of my Macs at home - all I could find was the tutorial LaTeX files.
Hopefully I can find the source on one of my office Macs.
@ Megamac04:
Yes, that is the the definition of the drawrect method. <edit>As pointed out below, it should be drawRect. Unfortunately I'm not case-sensitive.</edit> It looks like you've figured this out already, but these are all listed in the class reference files in Apple's documentation. Any method which is defined in your base class can be re-implemented in your own class by putting it in your implementation file. This replaces the base class' implementation (which you want here) but you can call the base class' implementation with by calling the same method on "super" if you would rather extend its functionality.
Yes, that is the the definition of the drawrect method. <edit>As pointed out below, it should be drawRect. Unfortunately I'm not case-sensitive.</edit> It looks like you've figured this out already, but these are all listed in the class reference files in Apple's documentation. Any method which is defined in your base class can be re-implemented in your own class by putting it in your implementation file. This replaces the base class' implementation (which you want here) but you can call the base class' implementation with by calling the same method on "super" if you would rather extend its functionality.
You need a capital R in drawRect:
Thank you, I changed the drawRect like you said and it worked. Oh and I know this is a bit off topic but do you know how to join the ADC membership. I tried but it wont allow me to join...

