0

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.

5
  • 1
    "I have object1 in class1 and object2 in class2, so I can't access object1 in class2." <- this is ambiguous. Do you mean object1 is an instance of class1 and so on? Or object1 is an ivar of class1? Or what? Commented Apr 28, 2012 at 18:18
  • Did you #import the necessary class interface files? Commented Apr 28, 2012 at 18:26
  • 1
    "I tried passing the object as a parameter but received a strong id error" < Please post the code that generates this error. Commented Apr 28, 2012 at 18:39
  • Why do you create "individual class files for each object"??? You can create a million objects from a single class file. Commented Apr 28, 2012 at 19:19
  • Whether or not object1 and object2 are the same class or different classes, you still need addressability to both in order to check for intersection. The fact that they're two different classes hardly affects things at all, other than the fact that you should declare the pointers appropriately. Commented Apr 28, 2012 at 19:21

1 Answer 1

1

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 UIImageView that conforms to an appropriately designed protocol and if there is a collision tells the objects to handle it.

void checkForCollision(object1, object2) {
    if (CGRectIntersectsRect(object1.frame, object2.frame)) {
        [object1 handleCollisionWith:object2];
        [object2 handleCollisionWith:object1];
    }
}

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the delayed response. This is what I was wanting to hear, thanks for all of the replies. I was trying to pass the object directly and not use a pointer, which was my fault. I am taking your suggestion theMikeSwan and creating a separate intersecting class, thanks for the advice!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.