Quote:
Originally Posted by LeopardDevX
What if i had a game where i wanted to pop up enemies from a array called "enemies" and the image is named "enemy", if i wanted them to pop up randomly on the "x" axe with the width of the view.
How should i set that up?
That would really help me understanding NSMutableArrays
|
Here's code to do something similar - it will put 10 copies of the same image on the screen, and put them in an array so you can get to them later. This code would work in viewDidLoad of a viewController, for example.
Code:
//declare the array in the .h file if you want to accessible
//from all methods.
NSMutableArray *arrayOfBalls
//create the array
arrayOfBalls = [[NSMutableArray alloc] init];
UIImage *ballImage = [UIImage imageNamed:@"ball.png"];
for (int i=0; i<10 ; i++){
UIImageView *newBall = [[UIImageView alloc] initWithImage: ballImage];
//set X and Y of the new imageView
newBall.center = CGPointMake(100 + i*10, 100+ i*10);
//add to array
[arrayOfBalls addObject:newBall];
//add to the view, so it gets displayed.
[self.view addSubview: newBall];
}
Quote:
The for (UIView *myBlock in safeKeeping).....
method isnt working...
im getting a warning on myBlock.....
|
Not sure why - the syntax is fine. You might get a warning if you already have a variable called myBlock; when you make two variables with the same name in different scopes, the local one hides the gobal one.