Reputation: 49
I've always had issues with classes, and I'm not sure if this is possible.
I'm trying to create a class with an identifiable name. I realize that that isn't clear, but my overall goal is to create a grid-like game and each square in the grid would be a member in the class.
So for example, I would have a class called square and say in my code
square(16,47).isdead = true;
Basically, I want to know if it is possible to create a class where I can differentiate between at least a hundred different squares.
Also, not sure if this matters, but I am using sprite kit.
Upvotes: 0
Views: 61
Reputation: 2576
Several ways to do what you want, I would prefer the below method.
C style two dimensional array:
Square * squares[10][10];
And you encapsulate that in a class called SquareManager with a method:
-(Square*) aSquareManager squareX:(short)x Y:(short)y;
If you specifically want the access pattern described in your question you can use (credit goes to arturgrigor):
#define square(x, y) [aSquareManager squareX:x Y:y]
Then you can access all of your squares this way:
if ([aSquareManager squareX:16 Y:47].isdead==true) [self showSkullForSquare:[aSquareManager squareX:16 Y:47]];
A different approach would be that a square has x and y properties:
Square.h
@property short y;
@property short x;
And then you put them all into an array, and when you need a square you search through the array.
for(Square * aSquare in squares) {
if(aSquare.x==anXValue && aSquare.y==anYValue) {
return aSquare;
}
}
Functions like these are much quicker than you think.
Upvotes: 1