简体   繁体   中英

[AS3][Nape Physics] How to access associated object of a Nape body in a InteractionListener?

I need help with the physics engine Nape in AS3. I have a class with some rectangles (Nape bodies) as member variables. I have a InteractionListener that reacts when these rectangles interacts with some circles (also Nape bodies) in the scene. However, I need to access the class instance that the rectangle is part of when handling the collision inside of the InteractionListener. The class instance has some methods that I need to call. How can I do this?

public function foodInteraction(callback:InteractionCallback):void {
    Food.list.remove(Body(callback.int1));
    Universe.space.bodies.remove(Body(callback.int1));
    // callback.int2 is part of class Organism, 
    // need to access that organism and call organism33.eat();
}

I have a dynamic amount of organism instances. Each organism has a dynamic number of rectangles. It's only the first rectangle in that is interacting. Only solution I can see right now is to have all the interacting rectangles in a separate list along with a list of all the organism instances, and then access the organism via the index of the rectangle in the other list. But it would be much faster to be able to access the organism via some userdata in the rectangle body... I couldn't extend the Body because it's a final class. Please help!

I hate when finding a solution seconds after asking about it. If anyone else is interested in the solution:

Store the instance like this: body.userData.organism = organism;

Then access it: callback.int2.userData.organism.foo();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM