Iphone SDK: Image arrays?
I'm making a "Tapping-App" where you have to kill zombies by tapping on them. Currently i can't make any progress because i can't use the methods i need properly.
My App works like that:
I have a timer that spawns an image 3 times per second:
[NSTimer scheduledTimerWithTimeInterval:1.0/3
target:self
selector:@selector(Spawn)
userInfo:nil
repeats:YES];
then i have the Spawn command:
- (void) Spawn {
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES;
[self.view addSubview:myImage];
[myImage release]
}
I also have a "TouchesBegan" command:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
}
What I want to do, is put all the spawning images into an Array; If one of the images is touched i want it to be 开发者_如何学JAVAremoved from the superview.I want to know how an array works and how i can remove objects added to the array from a different function like "TouchesBegan".
Please help me with my problem!
EDIT:
Nevermind... I searched around the internet and found some useful resources witch were almost what i needed. I finally figured out how it works and got my spawner function to work with collision and with "TouchesBegan".
If somebody wants the code, just ask me.
DD
Use [NSMUtableArray addObject:] and [NSMutableArray removeObject:]
In your interface, something like:
@interface MyClass
{
NSMutableArray *zombies_;
}
In your implementation, after initializing the array, change your spawn method to include the line
[self.zombies addObject:myImage];
and in your touch handler, after you determine which view was touched, something like
[zombies_ removeObject:theView];
[theView removeFromSuperview];
Although, after thinking about it, you may be better off just creating UIButton instances instead of UIIMageViews and writing touch handlers to determine which view was touched.
精彩评论