PDA

View Full Version : Tiles are weird


Coin
2005.01.30, 05:22 PM
im making a tile type thing to test some stuff and its terrible at working correctly

since the source code isnt all that long here it is

header file

#import <Cocoa/Cocoa.h>
enum {HERO, NONE};
@interface gameView : NSView
{
NSImage *heroImage;

int board[10][10];

NSRect clickArea[10][10];

int xpoh, ypoh;
}
- (void)mouseLocationX:(int)x y:(int)y;
@end


here is the main file

#import "gameView.h"

@implementation gameView

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

- (void)awakeFromNib
{
heroImage = [[NSImage imageNamed:@"hero.png"] retain];

int x, y = 0;
for (x = 0 ; x < 10 ; x++) {
for (y = 0 ; y < 10 ; y++) {
board[x][y] = NONE;
clickArea[x][y] = NSMakeRect(x*16,y*16,16,16);
[NSBezierPath strokeRect:clickArea[x][y]];
}
}
board[9][9] = HERO;
xpoh = ypoh = 9;
}
- (void)drawRect:(NSRect)rect
{
int w, k = 0;
for (w = 0 ; w < 10 ; w++) {
for (k = 0 ; k < 10 ; k++) {
[NSBezierPath strokeRect:clickArea[w][k]];
}
}
NSRect hero, modal;
hero.size = [heroImage size];
hero.origin = NSMakePoint(xpoh*16, ypoh*16);
modal.size = [heroImage size];
modal.origin = NSZeroPoint;
[heroImage drawInRect:hero fromRect:modal operation:NSCompositeSourceOver fraction:1];
}

- (void)mouseDown:(NSEvent *)event
{
NSPoint loc = [event locationInWindow];
int a, b = 0;
for (a = 0 ; a < 10 ; a++) {
for (b = 0 ; b < 10 ; b++) {
if (NSMouseInRect(loc, clickArea[a][b], NO) == YES) {
[self mouseLocationX:a y:b];
}
}
}
}
- (void)mouseLocationX:(int)x y:(int)y
{
NSRunAlertPanel(@"CLICKED!", @"x = %d y = %d", @"OK", nil, nil,x,y);
}
@end

what that does is when i click one of the little squares say the bottom left it comes up with x = 1 y = 1, instead of x = 0 y = 0 also the top and rightmost row and column dont do anything, i stuck the exact same alert panel call right before the MouseInRect call to see if it was starting at 1 1 or 0 0
turns out it runs through all the positions correctly starting at 0 0 and ending at 9 9
so what is wrong with MouseInRect? is it biased aginst the NSRect array?

And why am i an evangelist, im pretty sure i havnt tryed to convert any of you to christians.. or gone on any cursades latly

Taxxodium
2005.01.30, 06:16 PM
Your coordinates are wrong. Remember that in Cocoa (0, 0) is located in the bottom left corner, and not in top left corner.

With this said, try switching your x and y variables...

Coin
2005.01.30, 08:39 PM
........... it still starts at 11 and doesnt do that last row/column, and now the x axis is up and down and the y axis is left and right.