I’m creating a game in objective-c and need to detect when two objects intersect frames.
I typically would detect this by using
if (CGRectIntersectsRect(object1.frame, object2.frame)) {/*do something*/}
My only issue is that I’ve created individual class files for each object. I have object1 in class1 and object2 in class2, so I can’t access object1 in class2.
I tried passing the object as a parameter but received a strong id error, so I assume objects aren’t allowed to be passed. How I can access the object1.frame in class2? I’m still new to objective-c, so I would appreciate any advice! Thanks in advance, let me know if you need any more information.
EDIT: Sorry for the ambiguity, I wasn’t very clear. I declared a UIImageView or “object1” in class1.h and is used throughout the class1.m file.
I have another class where I declared another UIImageView “object2” in class2.h and class2.m. At one point during the class2.m file, I need to see if object1 and object2 are intersecting.
Actually as they are both image views neither of them should really be dealing with intersecting with each other. The better route is to have a third class that has a connection to each of the first two classes that takes care of detecting the collisions or to create a C function that just takes any two instances of
UIImageViewthat conforms to an appropriately designed protocol and if there is a collision tells the objects to handle it.With something like this it makes life easier when later on you need to detect collisions involving a third or fourth object. Both objects do need to have the same superview though or this will not work.
Also, you can pass objects but you normally pass by reference. In order for object1 to tell, or ask, object2 anything it has to have a pointer to it the same applies in reverse.