Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2-dimensional arrays in Objective-C?

I'm working on a basic iPhone game that requires a single-screen tilemap. Nothing difficult there. I come from a C background, so my current solution looks a bit like this:

typedef struct _Tile {
    NSString *type;
} Tile;

@interface Map {
    Tile mapData[MAP_TILE_MAX_X][MAP_TILE_MAX_Y];
}

This works fine, but I'm wondering if there's a slightly more 'correct' way to handle things via Objective-C. Here's how I see the situation going if I were to adopt an Objective-C approach: I'd create a base Tile class to hold basic tile properties, which I could then subclass for specific tile types (@interface Water : Tile {}, for example). This would allow me to also have Tile-specific logic. For instance: the Tile class could have a 'think' method which would execute any necessary logic. In my Water subclass, this may involve creating a ripple effect if the player were submerged.

My questions then:

  1. Is it acceptable to use C structs in this situation? If not, am I on the right track with regards to my Obj-C approach?
  2. If I were to create a base Tile class and use subclasses for specific Tile types, how would I dynamically instantiate each Tile subclass (given that I have an NSString containing the type 'water', I would need to instantiate the Water class).
like image 914
ndg Avatar asked Nov 25 '22 18:11

ndg


1 Answers

The second part of your question is easier to address, so I will approach that first.

Dynamically instantiating an object of an arbitrary class at runtime can be done using NSClassFromString()

Assuming your WaterTile is a subclass of UIView (which seems to make sense since it is likely to to be drawn on the screen)

UIView *newTile = [[NSClassFromString([NSString stringWithFormat:@"%@TileView", [@"water" capitalizedString]]) alloc] init];

Now for the first part.

Given that the tile is something that you want to draw on screen you will benefit from all of the OO goodness by inheriting from UIView which will respond to touch events and have methods needed for positioning and drawing. This is the major advantage over using a struct for your tiles.

Chances are, the abstract Tile class that you are thinking of won't really be needed as most of the properties and methods provided by UIView which leads me to think that you may want to define a Tile @protocol instead.

@protocol TileViewDrawing 
- (void)drawThinking;
@end

@interface WaterTileView : UIView <TileViewDrawing>
@end

@implementation WaterTileView
-(void)drawThinking
{
    // Code to show rippling effect
}
@end

To create a 2D arrays in your Map define an NSArray (columns) of NSArrays (row)

like image 63
falconcreek Avatar answered Jan 02 '23 11:01

falconcreek