Search code examples
haxehaxeflixelnape

FlxNapeSprite and collisions


I'm using Nape Physics with the dev version of HaxeFlixel. I have a few FlxNameSprites moving around in my game. Nape's own handling of collision resolution is excellent, but how do I make my own code react to the collisions happening too?

I.e if player 1 and player 2 collide, Nape resolves the collisions and the physics happens. How can I make some of my own code run too? What is the Nape equivalent of FlxG.overlap()?


Solution

  • You could use listeners for tracking: Create CbTypes for objects, than assign them to corresponding bodies. Than create collision listener for that CbTypes and add it to space

    var CBODY1 = new CbType();
    var CBODY2 = new CbType();
    player1.body.cbTypes.add(CBODY1);
    player2.body.cbTypes.add(CBODY1);
    
    collisionListener = new InteractionListener(
            CbEvent.BEGIN,
            InteractionType.COLLISION,
            CBODY1,
            CBODY2,
            onCollide
        );
    
    space.listeners.add(collisionListener);
    
    private function onCollide(cb:InteractionCallback):Void
    {
        trace("COLLIDE");
    }