I have created a game where I have a floor made of multiple images varying in heights.. (So the floor isn't flat)
Rather then opening a million if statements:
Code:
if (CGRectIntersectsRect(ball1.frame, floor1.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor2.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor3.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor4.frame)) {
}
//and so on...
Is there a way I can check collision using 1 if statement? All of the UIImageView floor pieces are named, "floor1, floor2, floor3, floor4, and so on"
Thanks!
__________________
APPS4LIFE
search for me in the app store.
I have created a game where I have a floor made of multiple images varying in heights.. (So the floor isn't flat)
Rather then opening a million if statements:
Code:
if (CGRectIntersectsRect(ball1.frame, floor1.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor2.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor3.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor4.frame)) {
}
//and so on...
Is there a way I can check collision using 1 if statement? All of the UIImageView floor pieces are named, "floor1, floor2, floor3, floor4, and so on"
Thanks!
Put your floor objects (or their frame rectangles) in an array. Then use a for loop to check the rects for intersections.
You can build the array of floor objects by marking them with tags, then looping though the tag numbers, calling -viewWithTag on the floor view's parent view.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Did you mean something like this? The program is crashing with "SIGBART" /:
Code:
NSMutableArray *floors = [NSMutableArray array];
[floors addObject:wall1];
[floors addObject:wall2];
[floors addObject:wall3];
for (int i = 0; i < 3; i++) {
if (CGRectIntersectsRect(ball1.frame, [floors.self viewWithTag:i].frame)) {
Thanks again for your time so far..
Also, when I'm having levels that have all the way up to wall359 or higher, will looping still be a sufficient method? Thanks.
No, you either need to use an array, or ask the owning view for the views using viewWithTag. You don't send an array the message viewWithTag. NSArray objects don't know about views.
Code:
UIView aFloor;
for (int i = 0; i < 3; i++)
{
aFloor = [floors objectAtIndex: i];
if (CGRectIntersectsRect(ball1.frame, aFloor.frame))
{
}
}
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Works great! But now, how can I detect which wall is being hit so I can do something to that wall (i.e. change the image to a cracked version)
Edit: using afloor works as long as it's still in the if statement, thanks!
__________________
APPS4LIFE
search for me in the app store.
Works great! But now, how can I detect which wall is being hit so I can do something to that wall (i.e. change the image to a cracked version)
Edit: using afloor works as long as it's still in the if statement, thanks!
Jeez, dude. Would you like the forum to write your app for you?
Do you want the code to do something unique for each intersection, or just see if ANY floor intersected?
If you want to stop checking on the first rectangle overlap you detect, add a break statement to the if (intersects) code. Set aFloor to nil at the top of the method so it's nil UNLESS a collision is detected. Then, when the loop exits (because of the break statement) aFloor will contain the floor of the collision, or nil if there were no collisions.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.