views:

49

answers:

1

I am writing a game, which need a map, and I want to store the map. The first thing I can think of, is using a 2D-array. But the problem is what data should I store in the 2D-array. The player can tap different place to have different reaction. So, I am thinking store a 2D-array with objects, when player click some position, and I find it in the array, and use the object in that array to execute a cmd. But I have a concern that storing lots of object may use lots of memory. So, I am think storing char/int only. But it seems that not enough for me. I want to store the data like that:

{
Type:1
Color:Green
}

No matter what color is, if they are all type 1, the have same reactions in logic, but the visual effect is based on the color. So, it is not easy to store using a prue char/int data, unless I make something like this:

 1-5 --> all type 1. 1=color green , 
 2=color red, 3 = color yellow.... ...
 6-10 --> all type 2. 2 = color green,
 2 = color red ... ...

So, do you have any ideas on how to minimize the ram use, but also easy for me to read... ...thx

A: 

Go ahead and store a bunch of objects in the array, but with these two refinements:

  • Store a pointer to the object, not the object itself. (Objective C may handle this for you automatically; I don't know.)

  • Remember that a pointer to a single object can appear in more than one position in the array. All squares that share the same color and behavior can share an object.

It would also help if you did the math on the size of the array and the number of distinct squares so we can know exactly how much RAM you are talking about.

Norman Ramsey
This is about 10*10 arrays, but u may know it is not only thing that eat up my memory, also the images, effect , bububu... ...
Tattat
If it's only 10x10 tiles, I wouldn't worry about the memory. And yes, when you store an object in an array in Objective-C, you just store the pointer (and the array retains the object). That said, I would probably still consider carefully using a plain C 2D array and store structs, because it's much more scalable. But if it feels like a pain right now, just go with the simplest solution. 100 objects won't cause any problems.
Felixyz