Drawing Images to Screen with code loop - iPhone help
Hey all, I am trying to make a method that draws a block to the screen, moves to the edge with accelerometer input, stops when it hits the edge and stays, and then draws another block (of a random different color and size). I have this code to draw so far which works...
CGRect myImageRect = CGRectMake(135.0f, 215.0f, 50.0f, 50.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"block1.png"]];
myImage.opaque = NO; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
and then this code:
float newX = myImage.center.x + (accel.x * 12);
float newY = myImage.center.y + (accel.y * -12);
if(newX >= 30 && newY >= 50 && newX <= 290 && newY <= 430)
myImage.center = CGPointMake(newX, newY);
which used to move my block how I wanted with accel input when I wasn't 'drawing' the block 'with code' and I was actually just dragging it into the screen without coding it. So now with the above code that is drawing myImage, it is not working for the accel input for some reason?
So as you can see my first image is called block1.png. Now, I have many different block.png's and they are all different, but right now I am specifying the size and shape. So I either need to make a loop with the above code and somehow get it to randomly change the images, or I need to use an array and load in all of my images and then pull those out one at a time randomly whenever necessary. So my psuedocode for this is:
start of with first image
move
stop
check for win
draw new random image
move
stop
check for win
loop
and this should keep looping until my winning condition. What is the best way I should go about implementing this? Somehow making a loop with my above drawing code (I have tried this and not gotten anything to work) or loading in the images I have in an array and pulling them out randomly? How would I do this? Any help would be appreciated! Thank you!
Last edited by skatesnow73; 05-19-2011 at 12:33 AM.
|