UIImage question

Posts: 6
Joined: 2010.02
Post: #1
Hi all,

I am creating a breakout style game with the paddle and rows of bricks above, the first hurdle I've hit is how to create the rows of bricks... Would I be correct in thinking that i need to create an NSArray of UIImages, one UIImage for each brick. Is there a memory limit to the number of UIImages you can create, as I may have 50 bricks on screen at any one time?

Sorry I know this is probably a dumb question!

Quote this message in a reply
⌘-R in Chief
Posts: 1,277
Joined: 2002.05
Post: #2
You don't need an array of UIImages like that, no. You need an array of something which represents what kind of brick is that location. That can be an number, a character, a struct with multiple properties...

Say for instance you're using characters. A space ' ' would be mean there is no brick, an 'r' could mean it's a red brick, a 'b' could mean it's blue brick, etc.

If you use an array of structs, the struct could be something like

struct Brick {
    uint8_t color;  // numbers mapped to a color
    uint8_t health; // how many hits remaining needed to destroy it

When you're drawing the playing field, you'd loop through all of your bricks, check whether or not they should be visible, map the color/type to some image or texture, and use that to draw it.

Just an example:

UIImage * brickColorToImage[5];
brickColorToImage[0] = [[UIImage imageNamed:@"RedBrick"] retain];
brickColorToImage[1] = [[UIImage imageNamed:@"BlueBrick"] retain];

When you're drawing the bricks:
loop {
   Brick * brick = bricks + brickIndex;
   UIImage * brickImage = brickColorToImage[brick->color];
   ... draw using the image ...

I'm not saying that any of this is exactly how you should do it or what I'd do, but this certainly a general approach you can use. It should stimulate some thought.

Hope that helps.
Quote this message in a reply
Posts: 6
Joined: 2010.02
Post: #3
Thanks Seth - confusing stuff but I will try and digest, this beats mapping an IBOutlet Grin
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Trouble with UIImage in a class Jmcclane 8 7,035 Sep 10, 2010 01:24 PM
Last Post: Jmcclane
  UIImage Dimensions? Jmcclane 6 8,762 Sep 2, 2010 06:27 PM
Last Post: AndyKorth