views:

48

answers:

3

hi all,

wat i am trying to is, each time user touches screen, i am adding a small bulletr image on screen

UIImage *img = [UIImage imageNamed:@"shoot_a.png"]; //bullet shot image
        im.image = img;
[self.view addSubview:im];

, if user touches 50 times, there will be 50 images on screen.

now i want, if user hit a button, all these bullet should be remove from the screen.

i hope i am clear about my question.

[im removefromsuperview] doesnt work for me.

if i want to add images to array at run time, how can i add and release? or is there any better way to clear all images regards.


this is my touch method, i m adding bullet images on screen frm heere,, but these images are not getting added in Array

// Touch method
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if(restrictTap == YES){



    NSUInteger numTaps = [[touches anyObject] tapCount];
    NSString *numTapsMessage = [[NSString alloc] initWithFormat:@"%i", numTaps];
        [numTapsMessage release];
    // getting location
    CGPoint touchLocation = [[touches anyObject] locationInView:self.view];
    NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: X = %.0f Y = %.0f", touchLocation.x, touchLocation.y];
        //bullet shot  image
        im = [[UIImageView alloc] initWithFrame:CGRectMake(self.location.x, self.location.y, 12.0, 12.0)];

        float j =self.location.x;
        float i =self.location.y;

        img = [UIImage imageNamed:@"shoot_a.png"]; //bullet shot image
        im.image = img;
         //im.hidden = NO;
        [self.view addSubview:im]; ///bullet shot image is over transparent view            

        [imageArray addObject:img];



}
}
+1  A: 

You could keep track of them via a NSMutableArray property and use that to remove them:

// create e.g. in initializer:
imageArray = [[NSMutableArray alloc] init];

// clean-up, e.g. in dealloc:
self.imageArray = nil;

// add image:
[imageArray addObject:im];

// remove all:
for (UIView *img in imageArray) {
    [img removeFromSuperview];
}
[imageArray removeAllObjects];
Georg Fritzsche
@ this[imageArray addObject:im];i am getting 0 objects each time..wat to do?
shishir.bobby
@shi: What do you mean with that? The array stays empty? Two things now: you have `addObject:img` instead of `addObject:im` and the image view is over-retained.
Georg Fritzsche
@georgyes array is emmmmpty, no images is getting added to array.it's showing 0 objectsok let me try with img instread of im.
shishir.bobby
@shi: Use `im`, not `img` - you could avoid that confusion if you'd name the view better, e.g. `imageView`. As for the array, are you creating it in your initializer method?
Georg Fritzsche
i m creating this array on touch methiod only, when user taps, object should add , on dat time only....is it ok to create array in touch?
shishir.bobby
@shi: No, you want to remember the objects longer than that - you have to make it a property.
Georg Fritzsche
@ george, so, wat should i do next?
shishir.bobby
Outlined one way to do it.
Georg Fritzsche
didnt get you, what do u mean be outlined?
shishir.bobby
@shi: I updated the answer.
Georg Fritzsche
@ george , FInally thanks a lot GeorgeIts workingi am able to clear image....thanks a ton.regards
shishir.bobby
A: 

[moved to question]

shishir.bobby
Just update your question next time and delete this *"answer"* when you get to it.
Georg Fritzsche
+1  A: 

You can set a particular tag to all the bullet imageViews when you add it to your view

im.tag = 1000;

and then on button touch, you can iterate through subviews array of your view and remove all views with that tag. something like :

    int count = [[self.view subviews] count];

for(int i = 0; i < count; i++)
{
    if([(UIView*)[[self.view subviews] objectAtIndex:i] tag] == 1000)
    {
        [(UIView*)[[self.view subviews] objectAtIndex:i] removeFromSuperview]
    }
}
lukya
Thanks lukya...i cant get it working......can u manipulate my code, i commented on as a answerregards
shishir.bobby