iDevGames Forums
Working with Flipsquare - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Community Zone (/forum-4.html)
+--- Forum: Assembly Room (/forum-13.html)
+--- Thread: Working with Flipsquare (/thread-2591.html)

Pages: 1 2


Working with Flipsquare - Megamac04 - Jun 10, 2008 06:00 PM

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
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
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.


Working with Flipsquare - OneSadCookie - Jun 10, 2008 06:05 PM

You didn't say what the error is...


Working with Flipsquare - Megamac04 - Jun 10, 2008 06:53 PM

OneSadCookie Wrote:You didn't say what the error is...
sorry I forgot.
it says that there are two syntax errors before 'Switch'


Working with Flipsquare - FlamingHairball - Jun 10, 2008 07:21 PM

I think we'll need to see your .h files. But I may be wrong.


Working with Flipsquare - OneSadCookie - Jun 10, 2008 07:25 PM

The switch statement needs to be in a method, probably the displayTitle method.


Working with Flipsquare - Megamac04 - Jun 10, 2008 07:26 PM

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;
@end

GameController.h
Code:
#import <Cocoa/Cocoa.h>
@class Game, GameView;
@interface GameController : NSObject {
    IBOutlet Game *game;
    IBOutlet GameView *gameView;
}

@end

Game.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;
@end



Working with Flipsquare - Megamac04 - Jun 10, 2008 07:30 PM

OneSadCookie 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"


Working with Flipsquare - OneSadCookie - Jun 10, 2008 08:33 PM

the whole block switch (.....) { ......... } needs to go in the method


Working with Flipsquare - Megamac04 - Jun 10, 2008 08:48 PM

OneSadCookie Wrote:the whole block switch (.....) { ......... } needs to go in the method
whoa it worked. I know I may be asking much, but could you explain the logic behind this.
Thank you.


Working with Flipsquare - backslash - Jun 11, 2008 06:05 AM

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.


Working with Flipsquare - Megamac04 - Jun 11, 2008 08:29 AM

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?
Code:
- (void)drawrect: (NSRect)rect
thank you.


Working with Flipsquare - BeyondCloister - Jun 11, 2008 12:50 PM

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.


Working with Flipsquare - backslash - Jun 11, 2008 01:25 PM

@ 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.


Working with Flipsquare - OneSadCookie - Jun 11, 2008 01:29 PM

You need a capital R in drawRect:


Working with Flipsquare - Megamac04 - Jun 11, 2008 02:37 PM

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...