views:

47

answers:

1

Hi all,

I have a drawrect method in my main UIView which draws 8 sprites every game tic. I want to seperate out each of these sprites into a seperate UIView.

I am trying to split out one sprite first as a test.

So far I have added a UIView as subview to my main view and set it's frame. This draws a black box on the view. My question is how do I now get get the sprite drawing in the drawrect method to draw into this UIView ?

Thanks all,

Martin

A: 

Each view will need it's own drawRect, and somewhere you will need to call setNeedsDisplay on every (sub)view visible. Draw the sprite in the drawRect for the view where you want it to appear. You can either have lots of subclassed uiviews, each with their own drawRect, or a switch case statement inside a drawRect that selects what to draw based on some sort of type-of-subview property.

You might also want to set your sprite's view's background color to transparent, so that they are invisible until you draw into them.

hotpaw2
Thanks - but I can't get he subview to display - all I am getting is a black box. Do I need to set anything apart from the subviews frame ?
Ohnomycoco
@Ohnomycoco : Can you change the background color of the subview? Is the drawRect of the subview actually getting called (set a breakpoint)?
hotpaw2
The drawrect is being called (checked via bpoint). This is my code for adding the subview :CGRect fr = CGRectMake(55.0f, 0.0f, 50.0f, 50.0f); self.view2.backgroundColor = [UIColor whiteColor]; self.view2 = [[SecondView alloc] initWithFrame:fr]; [self.view addSubview : view2]; [view2 release];
Ohnomycoco
It just draws a black box
Ohnomycoco
@Ohnomycoco : Are you trying to set the color on a non-existant object???
hotpaw2
I think the problem may be that in my drawrect method I move the context to the x,y position for the sprite and then draw it there. Do you think I also need to move the UIView to the same place ?
Ohnomycoco