First (pathetic) attempt at dual-weilding classes

Member
Posts: 102
Joined: 2005.01
Post: #1
So.. i tryed to get 2 classes to work together.... it gets stray \240 and \302
it says that the class 'Troll' wont resond to any of the methods that are in it and it SPAT IN MY FACE!!!!

code:

myView.h
Code:
#import <Cocoa/Cocoa.h>

enum {HERO, BOX, WALL, GRASS, NONE, BLOCK, TROLL};
enum {UP, DOWN, LEFT, RIGHT};
enum {PAD};
@interface gameView : NSView
{
    NSImage *goal;
    NSImage *wall;
    NSImage *grass;
    NSImage *hero;
    NSImage *block;
    NSImage *troll;
    int enviro[20][20];
    int objects[20][20];
    int padPos[20][20];
    
    NSRect boardSquares[20][20];
    NSRect modal;
    int xpoh, ypoh;
    int numOfPads;
    int numOfTrolls;
}
- (IBAction)open:(id)sender;
- (void)keyDown:(NSEvent *)event;
- (void)move:(int)dir;
- (BOOL)canMoveX:(int)x Y:(int)y;
- (BOOL)canPushBlockX:(int)x Y:(int)y dir:(int)dir;
- (void)gameOver;
- (void)dead;
@end


myView.m
Code:
#import "gameView.h"
#import "Troll.h"
#define o NSCompositeSourceOver
@implementation gameView

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

- (void)awakeFromNib
{
    wall = [[NSImage imageNamed:@"wall.png"] retain];
    grass = [[NSImage imageNamed:@"grass.png"] retain];
    hero = [[NSImage imageNamed:@"hero.png"] retain];
    block = [[NSImage imageNamed:@"basket.png"] retain];
    goal = [[NSImage imageNamed:@"goal.png"] retain];
    troll = [[NSImage imageNamed:@"troll.png"] retain];
    int x, y;
    //cover with grass
    for (x = 0; x < 20; x++)    {
        for (y = 0; y < 20; y++)    {
            objects[x][y] = NONE;
            enviro[x][y] = GRASS;
            padPos[x][y] = NONE;
            boardSquares[x][y] = NSMakeRect(x*16, y*16, 16, 16);
        }
    }
    modal = NSMakeRect(0, 0, 16, 16);
    //add walls
    int a, b, c, d;
    for (a = 0; a < 20; a++)    {
        enviro[0][a] = WALL;
        objects[0][a] = WALL;
    }
    for (b = 0; b < 20; b++)    {
        enviro[b][19] = WALL;
        objects[b][19] = WALL;
    }
    for (c = 0; c < 20; c++)    {
        enviro[c][0] = WALL;
        objects[c][0] = WALL;
    }
    for (d = 0; d < 20; d++)    {
        enviro[19][d] = WALL;
        objects[19][d] = WALL;
    }
    //level specific
    objects[5][5] = TROLL;
    objects[7][7] = BLOCK;    
    objects[7][8] = BLOCK;
    objects[11][2] = HERO;
    xpoh = 11;
    ypoh = 2;
}

- (void)drawRect:(NSRect)rect
{
    int x, y;
    for (x = 0; x < 20; x++)    {
        for (y = 0; y < 20; y++)    {
            switch (enviro[x][y])    {
                case GRASS:
                    [grass drawInRect:boardSquares[x][y] fromRect:modal operation:o fraction:1];
                    break;
                case WALL:
                    [wall drawInRect:boardSquares[x][y] fromRect:modal operation:o fraction:1];
                    break;
            }
            switch (padPos[x][y])    {
                case PAD:
                    [goal drawInRect:boardSquares[x][y] fromRect:modal operation:o fraction:1];
                    break;
            }
            switch (objects[x][y])    {
                case HERO:
                    [hero drawInRect:boardSquares[x][y] fromRect:modal operation:o fraction:1];
                    break;
                case BLOCK:
                    [block drawInRect:boardSquares[x][y] fromRect:modal operation:o fraction:1];
                    break;
                case TROLL:
                    [troll drawInRect:boardSquares[x][y] fromRect:modal operation:o fraction:1];
                    break;
            }
        }
    }
}

- (BOOL)acceptsFirstResponder
{
    return YES;
}

- (void)keyDown:(NSEvent *)theEvent
{
    NSString *key = [theEvent charactersIgnoringModifiers];
    switch ([key characterAtIndex:0])    {
        case NSUpArrowFunctionKey:
            [self move:UP];
            break;
        case NSDownArrowFunctionKey:
            [self move:DOWN];
            break;
        case NSLeftArrowFunctionKey:
            [self move:LEFT];
            break;
        case NSRightArrowFunctionKey:
            [self move:RIGHT];
            break;
    }
}

- (void)move:(int)dir
{
    switch (dir)    {
        case UP:
            if ([self canMoveX:xpoh Y:ypoh+1] == YES)    {
                objects[xpoh][ypoh] = NONE;
                objects[xpoh][ypoh+1] = HERO;
                ypoh += 1;
            }
            else if ([self canPushBlockX:xpoh Y:ypoh+1 dir:UP] == YES)    {
                //move block
                objects[xpoh][ypoh+1] = NONE;
                objects[xpoh][ypoh+2] = BLOCK;
                //move hero
                objects[xpoh][ypoh] = NONE;
                objects[xpoh][ypoh+1] = HERO;
                ypoh += 1;
            }
            break;
        case DOWN:
            if ([self canMoveX:xpoh Y:ypoh-1] == YES)    {
                objects[xpoh][ypoh] = NONE;
                objects[xpoh][ypoh-1] = HERO;
                ypoh -= 1;
            }
            else if ([self canPushBlockX:xpoh Y:ypoh-1 dir:DOWN] == YES)    {
                //move block
                objects[xpoh][ypoh-1] = NONE;
                objects[xpoh][ypoh-2] = BLOCK;
                //move hero
                objects[xpoh][ypoh] = NONE;
                objects[xpoh][ypoh-1] = HERO;
                ypoh -= 1;
            }
            break;
        case RIGHT:
            if ([self canMoveX:xpoh+1 Y:ypoh] == YES)    {
                objects[xpoh][ypoh] = NONE;
                objects[xpoh+1][ypoh] = HERO;
                xpoh += 1;
            }
            else if ([self canPushBlockX:xpoh+1 Y:ypoh dir:RIGHT] == YES)    {
                //move block
                objects[xpoh+1][ypoh] = NONE;
                objects[xpoh+2][ypoh] = BLOCK;
                //move hero
                objects[xpoh][ypoh] = NONE;
                objects[xpoh+1][ypoh] = HERO;
                xpoh += 1;
            }
            break;
        case LEFT:
            if ([self canMoveX:xpoh-1 Y:ypoh] == YES)    {
                objects[xpoh][ypoh] = NONE;
                objects[xpoh-1][ypoh] = HERO;
                xpoh -= 1;
            }
            else if ([self canPushBlockX:xpoh-1 Y:ypoh dir:LEFT] == YES)    {
                //move block
                objects[xpoh-1][ypoh] = NONE;
                objects[xpoh-2][ypoh] = BLOCK;
                //move hero
                objects[xpoh][ypoh] = NONE;
                objects[xpoh-1][ypoh] = HERO;
                xpoh -= 1;
            }
            break;
    }
    [self setNeedsDisplay:YES];
    [self gameOver];
}

- (BOOL)canMoveX:(int)x Y:(int)y
{
    BOOL returnValue = NO;
    if (objects[x][y] == NONE)    {
        returnValue = YES;
    }
    return returnValue;
}

- (BOOL)canPushBlockX:(int)x Y:(int)y dir:(int)dir
{
    BOOL returnValue = NO;
    if (objects[x][y] == BLOCK)    {
        if (dir == UP && objects[x][y+1] == NONE || dir == UP && objects[x][y+1] == TROLL)    {
            returnValue = YES;
        }
        if (dir == DOWN && objects[x][y-1] == NONE ||dir == DOWN && objects[x][y-1] == TROLL)    {
            returnValue = YES;
        }
        if (dir == LEFT && objects[x-1][y] == NONE ||dir == LEFT && objects[x-1][y] == TROLL)    {
            returnValue = YES;
        }
        if (dir == RIGHT && objects[x+1][y] == NONE ||dir == RIGHT && objects[x+1][y] == TROLL)    {
            returnValue = YES;
        }
    }
    return returnValue;
}

- (IBAction)open:(id)sender
{
    [Troll resetTrolls];
    numOfTrolls = 0;
    numOfPads = 0;
    NSString *lvl1 = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lvl3" ofType:@"txt"]];
    int a, b;
    int index = 0;
    for (a = 20 ; a > 0 ; a--)    {
        for (b = 0 ; b < 20 ; b++)    {
            switch ([lvl1 characterAtIndex:index])    {
                case '#':
                    enviro[b][a] = WALL;
                    objects[b][a] = WALL;
                    break;
                case '@':
                    objects[b][a] = HERO;
                    xpoh = b;
                    ypoh = a;
                    break;
                case '+':
                    enviro[b][a] = GRASS;
                    objects[b][a] = NONE;
                    break;
                case '$':
                    objects[b][a] = BLOCK;
                    break;
                case '&':
                    enviro[b][a] = GRASS;
                    objects[b][a] = NONE;
                    padPos[b][a] = PAD;
                    numOfPads++;
                    break;
                case '*':
                    objects[b][a] = TROLL;
                    enviro[b][a] = GRASS;
                    [Troll newTrollAtIndex:numOfTrolls X:b Y:a];
                    numOfTrolls++;
                    break;
            }
            index++;    
        }
        index++;
    }
    [self setNeedsDisplay:YES];
    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(moveTrolls:)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)gameOver
{
    int numOfTotalPads = numOfPads;
    int x, y;
    for (x = 0; x < 20; x++)    {
        for (y = 0; y < 20; y++)    {
            if (padPos[x][y] == PAD && objects[x][y] == BLOCK)    {
                numOfPads--;
                if (numOfPads == 0)    {
                    NSRunAlertPanel(@"Victory", @"You Win!", @"OK", nil, nil);
                }
            }
        }
    }
    numOfPads = numOfTotalPads;
}

- (void)moveTrolls:(NSTimer *)timer
{
    int i;
    for (i = 0; i < numOfTrolls; i++)    {
        int x = [Troll xOfTroll:i];
        int y = [Troll yOfTroll:i];
        if (x < xpoh)    {
            objects[x][y] = NONE;
            objects[x+1][y] = TROLL;
            [Troll resetTroll:i AtX:x+1 Y:y];
        }
        else if (x > xpoh)    {
            objects[x][y] = NONE;
            objects[x-1][y] = TROLL;
            [Troll resetTroll:i AtX:x-1 Y:y];
        }
        else if (y < ypoh)    {
            objects[x][y] = NONE;
            objects[x][y+1] = TROLL;
            [Troll resetTroll:i AtX:x Y:y+1];
        }
        else if (y < ypoh)    {
            objects[x][y] = NONE;
            objects[x][y-1] = TROLL;
            [Troll resetTroll:i AtX:x Y:y-1];
        }
    }
    [self dead];
    [self setNeedsDisplay:YES];
}

- (void)dead
{
    int x, y;
    int i = 0;
    for (x = 0; x < 20; x++)    {
        for (y = 0; y < 20; y++)    {
            if (objects[x][y] == TROLL && xpoh == x && ypoh == y)    {i++;}
        }
    }
    if    (i > 0)    {
        xpoh = ypoh = nil;
    }
}
@end


Troll.h
Code:
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

@interface Troll : NSObject
{
    int x, y;
    NSMutableArray* xpot;
    NSMutableArray* ypot;
}
- (int)xOfTroll:(int)i;
- (int)yOfTroll:(int)i;
- (void)newTrollAtIndex:(int)i X:(int)x Y:(int)y;
- (void)resetTroll:(int)i AtX:(int)x Y:(int)y;
- (void)resetTrolls;
@end


Troll.m
Code:
#import "Troll.h"
@implementation Troll:

- (id)init
{
    self = [super init];
    [[xpot initWithCapacity:0] reatain];
    [[ypot initWithCapacity:0] reatain];
}

- (int)xOfTroll:(int)i;
{
    return [[xpot objectAtIndex:i] intValue];
}

- (int)yOfTroll:(int)i
{
    return [[xpot objectAtIndex:i] intValue];
}

- (void)newTrollAtIndex:(int)i X:(int)x Y:(int):y
{
    NSNumber *xn = [NSNumber numberWithInt:x];
    NSNumber *yn = [NSNumber numberWithInt:y];
    [xpot insertObject:xn AtIndex:i];
    [xpot insertObject:yn AtIndex:i];
    [xn release];
    [yn release];
}

- (void)resetTrolls
{
    [xpot release];
    [xpot release];
    [[xpot initWithCapacity:0] retain];
    [[ypot initWithCapacity:0] retain];
}

- (void)resetTroll:(int)i AtX:(int)x Y:(int)y
{
    NSNumber *xn = [NSNumber numberWithInt:x];
    NSNumber *yn = [NSNumber numberWithInt:y];
    
    [xpot replaceObjectAtIndex:i withObject:xn];
    [ypot replaceObjectAtIndex:i withObject:yn];
    
    [xn release];
    [yn release];
}

- (void)dealloc
{
    [xpot dealloc];
    [ypot dealloc];
    [super dealloc];
}
@end


most of the stray \... errors are at the begining of Troll.m and there is a parse error before the - in - (id)init {}

how many things did i mess up!?!?


edit: thats odd.... theres little random * in the code pasted here... although that isnt accually in my code.... i bet those are my \302 and \240

edit edit: got rid of those now there are two errors..... both in Troll.m
1) parse error token before '-' (on the line with - (id)init)
2) fatal error: method definition not in class context (on line with - (int)xOfTroll: (int)i; )

soo yea.... whats with that??!!

edit edit edit: for no apperent reason it just added warnings everywhere in myView.m saying that Troll wont respond to the methods that are in Troll.... im scared
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #2
type this into the terminal: defaults write com.apple.xcode NSTextShowsControlCharacters 'YES'

It's not magic, it's Ruby.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #3
Did you by chance copy code off a web page and paste it in? That's where the \xxx characters usually come from.

Coin Wrote:edit edit: got rid of those now there are two errors..... both in Troll.m
1) parse error token before '-' (on the line with - (id)init)
2) fatal error: method definition not in class context (on line with - (int)xOfTroll: (int)i; )

soo yea.... whats with that??!!

Look closely at the end of those lines.

Wade
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #4
Let's talk about preprocessor defines. You defined "o" to be replaced by NSCompositeSourceOver. Now a quick find a replace will show you what your file looks like to the compiler.

Code:
@implementatiNSCompositeSourceOvern gameView

- (vNSCompositeSourceOverid)awakeFrNSCompositeSourceOvermNib
{
    wall = [[NSImage imageNamed:@"wall.png"] retain];
    grass = [[NSImage imageNamed:@"grass.png"] retain];
    herNSCompositeSourceOver = [[NSImage imageNamed:@"herNSCompositeSourceOver.png"] retain];
    blNSCompositeSourceOverck = [[NSImage imageNamed:@"basket.png"] retain];
    gNSCompositeSourceOveral = [[NSImage imageNamed:@"gNSCompositeSourceOveral.png"] retain];
    trNSCompositeSourceOverll = [[NSImage imageNamed:@"trNSCompositeSourceOverll.png"] retain];
    int x, y;
    //cNSCompositeSourceOverver with grass
    fNSCompositeSourceOverr (x = 0; x < 20; x++)    {
        fNSCompositeSourceOverr (y = 0; y < 20; y++)    {
            NSCompositeSourceOverbjects[x][y] = NNSCompositeSourceOverNE;
            envirNSCompositeSourceOver[x][y] = GRASS;
            padPNSCompositeSourceOvers[x][y] = NNSCompositeSourceOverNE;
            bNSCompositeSourceOverardSquares[x][y] = NSMakeRect(x*16, y*16, 16, 16);
        }
    }
    mNSCompositeSourceOverdal = NSMakeRect(0, 0, 16, 16);
    //add walls
    int a, b, c, d;
    fNSCompositeSourceOverr (a = 0; a < 20; a++)    {
        envirNSCompositeSourceOver[0][a] = WALL;
        NSCompositeSourceOverbjects[0][a] = WALL;
    }
    fNSCompositeSourceOverr (b = 0; b < 20; b++)    {
        envirNSCompositeSourceOver[b][19] = WALL;
        NSCompositeSourceOverbjects[b][19] = WALL;
    }
    fNSCompositeSourceOverr (c = 0; c < 20; c++)    {
        envirNSCompositeSourceOver[c][0] = WALL;
        NSCompositeSourceOverbjects[c][0] = WALL;
    }
    fNSCompositeSourceOverr (d = 0; d < 20; d++)    {
        envirNSCompositeSourceOver[19][d] = WALL;
        NSCompositeSourceOverbjects[19][d] = WALL;
    }
    //level specific
    NSCompositeSourceOverbjects[5][5] = TRNSCompositeSourceOverLL;
    NSCompositeSourceOverbjects[7][7] = BLNSCompositeSourceOverCK;    
    NSCompositeSourceOverbjects[7][8] = BLNSCompositeSourceOverCK;
    NSCompositeSourceOverbjects[11][2] = HERNSCompositeSourceOver;
    xpNSCompositeSourceOverh = 11;
    ypNSCompositeSourceOverh = 2;
}

Now, just guess why it's pissing at you. Smile So you need to use those defines in such a manner as the string isn't one you'll normally use. Maybe NSCSO would be a better choice. You aren't likely to have that in some stray place in that file or files which include that file. So fix that and see if it helps any of your problems.
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #5
i got a stray /23 when i tried to save by control-x control-s. Blush

It's not magic, it's Ruby.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #6
iefan Wrote:Let's talk about preprocessor defines. You defined "o" to be replaced by NSCompositeSourceOver. Now a quick find a replace will show you what your file looks like to the compiler.

While this particular #define doesn't seem like such a great idea to me, it doesn't actually cause any problems. The preprocessor replaces tokens, not substrings.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #7
wadesworld Wrote:Did you by chance copy code off a web page and paste it in? That's where the \xxx characters usually come from.

no.

I did the termianl thing.. im guessing that ill allow me to see weird characters?

I chaned the o thing

All the errors/warnings are exactly the same

New Code:

The only difference is the NSCSO

errors:

parse error before token '-'
on line:
Code:
- (id)init


fatal error: method definition not in class definition
on line:
Code:
- (int)xOfTroll:(int)i;



warnings:

there are 8 warnings

one on every line in myView.m that had a [Troll bla!]; in it had a warning that says
Troll may not respond '+theMethodBeingCalled'
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #8
what's the code? give us an example of how you call it. You may have to say a [[Troll alloc] bla!]; instead.

It's not magic, it's Ruby.
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #9
It's one of the things which I have never really considered. I know it works when I need it to. Now I know.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #10
i used

[[Troll alloc] resetTrolls];

for all my calls to Troll and it got rid of all the warnings

now there are just two errors in Troll.m

here is troll.m and .h

.h
Code:
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

@interface Troll : NSObject
{
    int x, y;
    NSMutableArray* xpot;
    NSMutableArray* ypot;
}
- (int)xOfTroll:(int)i;
- (int)yOfTroll:(int)i;
- (void)newTrollAtIndex:(int)i X:(int)x Y:(int)y;
- (void)resetTroll:(int)i AtX:(int)x Y:(int)y;
- (void)resetTrolls;
@end


.m
Code:
#import "Troll.h"
@implementation Troll:

- (id)init
{
    self = [super init];
    [[xpot initWithCapacity:0] reatain];
    [[ypot initWithCapacity:0] reatain];
}

- (int)xOfTroll:(int)i;
{
    return [[xpot objectAtIndex:i] intValue];
}

- (int)yOfTroll:(int)i
{
    return [[xpot objectAtIndex:i] intValue];
}


- (void)newTrollAtIndex:(int)i X:(int)x Y:(int):y
{
    NSNumber *xn = [NSNumber numberWithInt:x];
    NSNumber *yn = [NSNumber numberWithInt:y];
    [xpot insertObject:xn AtIndex:i];
    [xpot insertObject:yn AtIndex:i];
    [xn release];
    [yn release];
}

- (void)resetTrolls
{
    [xpot release];
    [xpot release];
    [[xpot initWithCapacity:0] retain];
    [[ypot initWithCapacity:0] retain];
}

- (void)resetTroll:(int)i AtX:(int)x Y:(int)y
{
    NSNumber *xn = [NSNumber numberWithInt:x];
    NSNumber *yn = [NSNumber numberWithInt:y];
    
    [xpot replaceObjectAtIndex:i withObject:xn];
    [ypot replaceObjectAtIndex:i withObject:yn];
    
    [xn release];
    [yn release];
}

- (void)dealloc
{
    [xpot dealloc];
    [ypot dealloc];
    [super dealloc];
}
@end


there errors are the same as before....
a fatal error saying that the first method isnt defined, and a parse before the - in -(id)init
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #11
Its because I had a : after the class name on the first line

After i fixed that i spelled retain reatain

Now it has no errors or warnings

But it crashes... here is the good-sorta code

Troll.h
Code:
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

@interface Troll : NSObject
{
    int x, y;
    NSMutableArray* xpot;
    NSMutableArray* ypot;
}
- (int)xOfTroll:(int)i;
- (int)yOfTroll:(int)i;
- (void)newTrollAtIndex:(int)i X:(int)x Y:(int)y;
- (void)resetTroll:(int)i AtX:(int)x Y:(int)y;
- (void)resetTrolls;
@end


Troll.m
Code:
#import "Troll.h"
@implementation Troll

- (id)init
{
    self = [super init];
    [[xpot initWithCapacity:0] retain];
    [[ypot initWithCapacity:0] retain];
}

- (int)xOfTroll:(int)i;
{
    NSRunAlertPanel(@"Troll", @"xoftroll", @"OK", nil, nil);
    return [[xpot objectAtIndex:i] intValue];
}

- (int)yOfTroll:(int)i
{
    NSRunAlertPanel(@"Troll", @"yoftroll", @"OK", nil, nil);
    return [[xpot objectAtIndex:i] intValue];
}


- (void)newTrollAtIndex:(int)i X:(int)xa Y:(int)ya
{
    NSRunAlertPanel(@"Troll", @"newTrollAtIndex:%d x:%d y:%d", @"OK", nil, nil, i, xa, ya);
    NSNumber *xn = [NSNumber numberWithInt:xa];
    NSNumber *yn = [NSNumber numberWithInt:ya];
    [xpot addObject:xn];
    [xpot addObject:yn];
    [xn release];
    [yn release];
}

- (void)resetTrolls
{
    NSRunAlertPanel(@"Troll", @"ResetTrolls", @"OK", nil, nil);
    [xpot release];
    [xpot release];
    [[xpot initWithCapacity:0] retain];
    [[ypot initWithCapacity:0] retain];
}

- (void)resetTroll:(int)i AtX:(int)xa Y:(int)ya
{
    NSRunAlertPanel(@"Troll", @"resetTrollAtIndex:%d AtX:%d Y:%d", @"OK", nil, nil, i, xa, ya);
    NSNumber *xn = [NSNumber numberWithInt:xa];
    NSNumber *yn = [NSNumber numberWithInt:ya];
    
    [xpot replaceObjectAtIndex:i withObject:xn];
    [ypot replaceObjectAtIndex:i withObject:yn];
    
    [xn release];
    [yn release];
}

- (void)dealloc
{
    [xpot dealloc];
    [ypot dealloc];
    [super dealloc];
}
@end


myView.h
Code:
#import <Cocoa/Cocoa.h>

enum {HERO, BOX, WALL, GRASS, NONE, BLOCK, TROLL};
enum {UP, DOWN, LEFT, RIGHT};
enum {PAD};
@interface gameView : NSView
{
    NSImage *goal;
    NSImage *wall;
    NSImage *grass;
    NSImage *hero;
    NSImage *block;
    NSImage *troll;
    int enviro[20][20];
    int objects[20][20];
    int padPos[20][20];
    
    NSRect boardSquares[20][20];
    NSRect modal;
    int xpoh, ypoh;
    int numOfPads;
    int numOfTrolls;
}
- (IBAction)open:(id)sender;
- (void)keyDown:(NSEvent *)event;
- (void)move:(int)dir;
- (BOOL)canMoveX:(int)x Y:(int)y;
- (BOOL)canPushBlockX:(int)x Y:(int)y dir:(int)dir;
- (void)gameOver;
- (void)dead;
- (void)moveTrolls:(NSTimer *)timer;
@end


myView.h
Code:
#import "gameView.h"
#import "Troll.h"
#define NSCSO NSCompositeSourceOver
@implementation gameView

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

- (void)awakeFromNib
{
    wall = [[NSImage imageNamed:@"wall.png"] retain];
    grass = [[NSImage imageNamed:@"grass.png"] retain];
    hero = [[NSImage imageNamed:@"hero.png"] retain];
    block = [[NSImage imageNamed:@"basket.png"] retain];
    goal = [[NSImage imageNamed:@"goal.png"] retain];
    troll = [[NSImage imageNamed:@"troll.png"] retain];
    int x, y;
    //cover with grass
    for (x = 0; x < 20; x++)    {
        for (y = 0; y < 20; y++)    {
            objects[x][y] = NONE;
            enviro[x][y] = GRASS;
            padPos[x][y] = NONE;
            boardSquares[x][y] = NSMakeRect(x*16, y*16, 16, 16);
        }
    }
    modal = NSMakeRect(0, 0, 16, 16);
    //add walls
    int a, b, c, d;
    for (a = 0; a < 20; a++)    {
        enviro[0][a] = WALL;
        objects[0][a] = WALL;
    }
    for (b = 0; b < 20; b++)    {
        enviro[b][19] = WALL;
        objects[b][19] = WALL;
    }
    for (c = 0; c < 20; c++)    {
        enviro[c][0] = WALL;
        objects[c][0] = WALL;
    }
    for (d = 0; d < 20; d++)    {
        enviro[19][d] = WALL;
        objects[19][d] = WALL;
    }
    //level specific
    objects[5][5] = TROLL;
    objects[7][7] = BLOCK;    
    objects[7][8] = BLOCK;
    objects[11][2] = HERO;
    xpoh = 11;
    ypoh = 2;
}

- (void)drawRect:(NSRect)rect
{
    int x, y;
    for (x = 0; x < 20; x++)    {
        for (y = 0; y < 20; y++)    {
            switch (enviro[x][y])    {
                case GRASS:
                    [grass drawInRect:boardSquares[x][y] fromRect:modal operation:NSCSO fraction:1];
                    break;
                case WALL:
                    [wall drawInRect:boardSquares[x][y] fromRect:modal operation:NSCSO fraction:1];
                    break;
            }
            switch (padPos[x][y])    {
                case PAD:
                    [goal drawInRect:boardSquares[x][y] fromRect:modal operation:NSCSO fraction:1];
                    break;
            }
            switch (objects[x][y])    {
                case HERO:
                    [hero drawInRect:boardSquares[x][y] fromRect:modal operation:NSCSO fraction:1];
                    break;
                case BLOCK:
                    [block drawInRect:boardSquares[x][y] fromRect:modal operation:NSCSO fraction:1];
                    break;
                case TROLL:
                    [troll drawInRect:boardSquares[x][y] fromRect:modal operation:NSCSO fraction:1];
                    break;
            }
        }
    }
}

- (BOOL)acceptsFirstResponder
{
    return YES;
}

- (void)keyDown:(NSEvent *)theEvent
{
    NSString *key = [theEvent charactersIgnoringModifiers];
    switch ([key characterAtIndex:0])    {
        case NSUpArrowFunctionKey:
            [self move:UP];
            break;
        case NSDownArrowFunctionKey:
            [self move:DOWN];
            break;
        case NSLeftArrowFunctionKey:
            [self move:LEFT];
            break;
        case NSRightArrowFunctionKey:
            [self move:RIGHT];
            break;
    }
}

- (void)move:(int)dir
{
    switch (dir)    {
        case UP:
            if ([self canMoveX:xpoh Y:ypoh+1] == YES)    {
                objects[xpoh][ypoh] = NONE;
                objects[xpoh][ypoh+1] = HERO;
                ypoh += 1;
            }
            else if ([self canPushBlockX:xpoh Y:ypoh+1 dir:UP] == YES)    {
                //move block
                objects[xpoh][ypoh+1] = NONE;
                objects[xpoh][ypoh+2] = BLOCK;
                //move hero
                objects[xpoh][ypoh] = NONE;
                objects[xpoh][ypoh+1] = HERO;
                ypoh += 1;
            }
            break;
        case DOWN:
            if ([self canMoveX:xpoh Y:ypoh-1] == YES)    {
                objects[xpoh][ypoh] = NONE;
                objects[xpoh][ypoh-1] = HERO;
                ypoh -= 1;
            }
            else if ([self canPushBlockX:xpoh Y:ypoh-1 dir:DOWN] == YES)    {
                //move block
                objects[xpoh][ypoh-1] = NONE;
                objects[xpoh][ypoh-2] = BLOCK;
                //move hero
                objects[xpoh][ypoh] = NONE;
                objects[xpoh][ypoh-1] = HERO;
                ypoh -= 1;
            }
            break;
        case RIGHT:
            if ([self canMoveX:xpoh+1 Y:ypoh] == YES)    {
                objects[xpoh][ypoh] = NONE;
                objects[xpoh+1][ypoh] = HERO;
                xpoh += 1;
            }
            else if ([self canPushBlockX:xpoh+1 Y:ypoh dir:RIGHT] == YES)    {
                //move block
                objects[xpoh+1][ypoh] = NONE;
                objects[xpoh+2][ypoh] = BLOCK;
                //move hero
                objects[xpoh][ypoh] = NONE;
                objects[xpoh+1][ypoh] = HERO;
                xpoh += 1;
            }
            break;
        case LEFT:
            if ([self canMoveX:xpoh-1 Y:ypoh] == YES)    {
                objects[xpoh][ypoh] = NONE;
                objects[xpoh-1][ypoh] = HERO;
                xpoh -= 1;
            }
            else if ([self canPushBlockX:xpoh-1 Y:ypoh dir:LEFT] == YES)    {
                //move block
                objects[xpoh-1][ypoh] = NONE;
                objects[xpoh-2][ypoh] = BLOCK;
                //move hero
                objects[xpoh][ypoh] = NONE;
                objects[xpoh-1][ypoh] = HERO;
                xpoh -= 1;
            }
            break;
    }
    [self setNeedsDisplay:YES];
    [self gameOver];
}

- (BOOL)canMoveX:(int)x Y:(int)y
{
    BOOL returnValue = NO;
    if (objects[x][y] == NONE)    {
        returnValue = YES;
    }
    return returnValue;
}

- (BOOL)canPushBlockX:(int)x Y:(int)y dir:(int)dir
{
    BOOL returnValue = NO;
    if (objects[x][y] == BLOCK)    {
        if (dir == UP && objects[x][y+1] == NONE || dir == UP && objects[x][y+1] == TROLL)    {
            returnValue = YES;
        }
        if (dir == DOWN && objects[x][y-1] == NONE ||dir == DOWN && objects[x][y-1] == TROLL)    {
            returnValue = YES;
        }
        if (dir == LEFT && objects[x-1][y] == NONE ||dir == LEFT && objects[x-1][y] == TROLL)    {
            returnValue = YES;
        }
        if (dir == RIGHT && objects[x+1][y] == NONE ||dir == RIGHT && objects[x+1][y] == TROLL)    {
            returnValue = YES;
        }
    }
    return returnValue;
}

- (IBAction)open:(id)sender
{
    [[Troll alloc] resetTrolls];
    numOfTrolls = 0;
    numOfPads = 0;
    NSString *lvl1 = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lvl3" ofType:@"txt"]];
    int a, b;
    int index = 0;
    for (a = 20 ; a > 0 ; a--)    {
        for (b = 0 ; b < 20 ; b++)    {
            switch ([lvl1 characterAtIndex:index])    {
                case '#':
                    enviro[b][a] = WALL;
                    objects[b][a] = WALL;
                    break;
                case '@':
                    objects[b][a] = HERO;
                    xpoh = b;
                    ypoh = a;
                    break;
                case '+':
                    enviro[b][a] = GRASS;
                    objects[b][a] = NONE;
                    break;
                case '$':
                    objects[b][a] = BLOCK;
                    break;
                case '&':
                    enviro[b][a] = GRASS;
                    objects[b][a] = NONE;
                    padPos[b][a] = PAD;
                    numOfPads++;
                    break;
                case '*':
                    objects[b][a] = TROLL;
                    enviro[b][a] = GRASS;
                    [[Troll alloc]newTrollAtIndex:numOfTrolls X:b Y:a];
                    numOfTrolls++;
                    break;
            }
            index++;    
        }
        index++;
    }
    [self setNeedsDisplay:YES];
    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(moveTrolls:)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)gameOver
{
    int numOfTotalPads = numOfPads;
    int x, y;
    for (x = 0; x < 20; x++)    {
        for (y = 0; y < 20; y++)    {
            if (padPos[x][y] == PAD && objects[x][y] == BLOCK)    {
                numOfPads--;
                if (numOfPads == 0)    {
                    NSRunAlertPanel(@"Victory", @"You Win!", @"OK", nil, nil);
                }
            }
        }
    }
    numOfPads = numOfTotalPads;
}

- (void)moveTrolls:(NSTimer *)timer
{
    int i;
    for (i = 0; i < numOfTrolls; i++)    {
        int x = [[Troll alloc] xOfTroll:i];
        int y = [[Troll alloc] yOfTroll:i];
        if (x < xpoh)    {
            objects[x][y] = NONE;
            objects[x+1][y] = TROLL;
            [[Troll alloc] resetTroll:i AtX:x+1 Y:y];
        }
        else if (x > xpoh)    {
            objects[x][y] = NONE;
            objects[x-1][y] = TROLL;
            [[Troll alloc] resetTroll:i AtX:x-1 Y:y];
        }
        else if (y < ypoh)    {
            objects[x][y] = NONE;
            objects[x][y+1] = TROLL;
            [[Troll alloc] resetTroll:i AtX:x Y:y+1];
        }
        else if (y < ypoh)    {
            objects[x][y] = NONE;
            objects[x][y-1] = TROLL;
            [[Troll alloc] resetTroll:i AtX:x Y:y-1];
        }
    }
    [self dead];
    [self setNeedsDisplay:YES];
}

- (void)dead
{
    int x, y;
    int i = 0;
    for (x = 0; x < 20; x++)    {
        for (y = 0; y < 20; y++)    {
            if (objects[x][y] == TROLL && xpoh == x && ypoh == y)    {i++;}
        }
    }
    if    (i > 0)    {
        xpoh = ypoh = nil;
    }
}
@end


With this code when I load a level with 2 trolls in it this is what happens :
1) resetTrolls
2) newTrollAtIndex:i X:x Y:y
3) newTrollAtIndex:i X:x Y:y
4) crash!

Debugger didnt give me anything but machine code
when I put a display dialog in -open right after the timer it runs
but if I stick it on the first line of -moveTrolls: it crashes before showing....

apperently nothing is wrong
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #12
Coin Wrote:i used

[[Troll alloc] resetTrolls];

for all my calls to Troll and it got rid of all the warnings

LOL LOL Good one! No, seriously that is NOT what you want!
Look in the docs for the meaning of class & instance methods.

Basically, resetTrolls (declared with a -) is an instance method, and if you call [Troll resetTrolls] it doesn't work because that would mean you want to call a class method (+resetTrolls).
When you do [Troll alloc], you create an instance, so if you call [[Troll alloc] resetTrolls] you are calling the instance method BUT every time you call [Troll alloc] you are actually creating a new different instance (and leaking memory).

Coin Wrote:there errors are the same as before....
a fatal error saying that the first method isnt defined, and a parse before the - in -(id)init

Read that error closely: "before the - in -(id)init"...
Delete the : in @implementation Troll:
...also, reatain != retain
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #13
Good, you caught the : and the reatain before I finished my post. Read the first part though Smile
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #14
i changed all the - (SomeMethod) to + (someMethod) in Troll.h and Troll.m exept -(id)init

now there are 10 warnings in Troll.m saying i cant use xpot or ypot (x position of troll, y position of troll)

so how do i fix that ... AND stuff
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #15
Well, those are instance variables, so the class methods can't access them. Perhaps what you want is just one shared Troll instance? The easier way for you would be to simply add Troll *myTroll; to your GameView class, alloc and init it once ( myTroll = [[Troll alloc] init]; ) and then simply use [myTroll doSomething]; elsewhere.

The alternative would be a "Singleton" with a sharedInstance method to access a single global instance, but the first alternative would do OK.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Dual Kinect Cameras and iPiSoft basic mrbones 1 3,579 Nov 23, 2011 03:39 AM
Last Post: vincentroberts
  C++ Interlocking Classes merrill541 1 2,679 Jan 25, 2009 08:43 PM
Last Post: akb825
  Noob: Accessing Structures from Cocoa Classes MikeC 15 7,438 Oct 19, 2007 02:42 PM
Last Post: MikeC
  Trouble With Template Classes in C++ Nick 4 3,257 Nov 21, 2006 10:25 AM
Last Post: DoG
  Trouble with template classes ermitgilsukaru 2 2,719 Aug 11, 2006 02:00 PM
Last Post: ermitgilsukaru