Ok, so I have 100 sprite images floating around, each has a different tag number and one image that is stationary. What I am trying to figure out is how to retrieve the tag number of any image that enters the frame of the static image, using the coda below.
Any help or push in the correct direction would be great. I may be way off. I don't know if using tag is the way to go. Is there a way to have an image send info about itself upon entering the static.frame image?
Ok, so I have 100 sprite images floating around, each has a different tag number and one image that is stationary. What I am trying to figure out is how to retrieve the tag number of any image that enters the frame of the static image, using the coda below.
Any help or push in the correct direction would be great. I may be way off. I don't know if using tag is the way to go. Is there a way to have an image send info about itself upon entering the static.frame image?
You can do it using a for loop that checks tags constantly...
This may help you get started....
"Detecting Subviews
You can loop through subviews of an existing view. This works especially well if you use the "tag" property on your views.
Code:
for (UIImageView *anImage in [self.view subviews]) { if (anImage.tag == 1) { // do something } }
for(n = 1; n <= 100; n++)
{
uiview* balls = [self.view viewWithTag:n;
if(CGRectIntersectsRect (static.frame, balls.frame)) {
do something here
}
}
This assumes that your balls are tagged with 1 to 100...
and I added an "s" to ball to differentiate it from your original declaration
So I am trying understand what this code is doing.
I have 100 UIImageView *ball each has a different tag 1 - 100, one UIImageView *static. All the ball images move around they start intersecting the static image and the code that you provided me is looping though 1 - 100 changing the tag number of UIView* balls and searching for balls views that intersect. I just don't understand how collision detection is taking place when balls is not moving but ball is?
So I am trying understand what this code is doing.
I have 100 UIImageView *ball each has a different tag 1 - 100, one UIImageView *static. All the ball images move around they start intersecting the static image and the code that you provided me is looping though 1 - 100 changing the tag number of UIView* balls and searching for balls views that intersect. I just don't understand how collision detection is taking place when balls is not moving but ball is?
Because your are redefining your tagged view...as UIView* balls...to ID it and to then do something. It does not change your original declaration of ball.
Sort of like...my name is Charles...I also go by Chuck....same person, but different monikers.
The tagged view is moving...and was originally declared as ball. The UIView* balls is the tagged view, but with a different name. This sort of thing is done a lot when you try to attach to a view, especially one that was created with IB. To be able to manipulate the view, you need to redefine it using its tag number...which ID's that view. Then you can move it, reframe it, hide it etc etc...
Try the code and see if it works for you.
I've used it to attach to multiple moving objects...and is especially useful when you also need to determine touch...as in which object was touched.
Because your are redefining your tagged view...as UIView* balls...to ID it and to then do something. It does not change your original declaration of ball.
Sort of like...my name is Charles...I also go by Chuck....same person, but different monikers.
The tagged view is moving...and was originally declared as ball. The UIView* balls is the tagged view, but with a different name. This sort of thing is done a lot when you try to attach to a view, especially one that was created with IB. To be able to manipulate the view, you need to redefine it using its tag number...which ID's that view. Then you can move it, reframe it, hide it etc etc...
Try the code and see if it works for you.
I've used it to attach to multiple moving objects...and is especially useful when you also need to determine touch...as in which object was touched.
Ok, thanks that makes sense. I've added the code but cant get it to detect the intersection. Everything is a child of the same view. One thing that may be causing it is I created all the ball images with code not IB. But, the static image was made in IB. I don't think that should matter.
Ok, thanks that makes sense. I've added the code but cant get it to detect the intersection. Everything is a child of the same view. One thing that may be causing it is I created all the ball images with code not IB. But, the static image was made in IB. I don't think that should matter.
You need a trigger that will start the "for" loop. Then it will run the test to find the view.
You can run the "for" loop over and over, but that gets expensive...and cause hesitation in the game you're writing.
Also, I assume you did tag the balls...
ball.tag = 1, ball.tag = 2, etc etc until you get to 100.
Let me load it into the SDK and I'll try to get it to work. I'll probably get back to you on Wednesday.
OK...did it...and it runs fine after I made some minor changes.
The biggest problem as to why you're not getting collision detection is because you're shooting over the top of the tree.
The tree coordinates are 120x120 and your snowball ending coordinate y coordinate is 110.
This type of animation can only be detected at its ending coordinates...it's set and forget. So, where the ball lands is where the rectintersect takes place. Anything in between...the flight of the ball, cannot be intercepted for location on screen.
But for what you're doing it will work fine.
The minor changes I made are the more standard way of handling the animation's end and there's no need to remove each tagNumber. Removing the snowBall UIImageView removes the tagNumber by definition.
And I also substited random number generator for the gunSlider to make it easy to test out.
If you want a copy of the project, give me an email address and I'll send it out to you.