Nape InteractionListener not calling callback

51 views
Skip to first unread message

Nicolas Mansart

unread,
Mar 27, 2015, 7:28:44 PM3/27/15
to haxef...@googlegroups.com
Hello !

New in here, I'm beginning with haxeflixel and currently trying to set up an InteractionListener (like in there : http://napephys.com/help/manual.html#Callbacks.InteractionListeners ) to hear some collisions, but it does not seem to work.

Here is one of my tests, tried to keep it minimal : a dynamic FlxNapeSprite bouncing on a static one and a listener on them two. There is a FlxText which should show the bounce count, which should be updated in the listener callback.

What do you think is missing ?

package;
import flixel.addons.nape.FlxNapeSprite;
import flixel.addons.nape.FlxNapeState;
import flixel.FlxG;
import flixel.text.FlxText;
import flixel.util.FlxColor;
import nape.callbacks.CbEvent;
import nape.callbacks.CbType;
import nape.callbacks.InteractionCallback;
import nape.callbacks.InteractionListener;
import nape.callbacks.InteractionType;
import nape.phys.BodyType;

/**
 * test based on http://napephys.com/help/manual.html
 * @author ...
 */

class TestNapeCollide extends FlxNapeState
{

   
private var _sprGround:FlxNapeSprite;
   
private var _sprBox:FlxNapeSprite;
   
private var _CBBOX:CbType;
   
private var _CBGROUND:CbType;
   
private var _boxGroundCollide:InteractionListener;
   
private var _collisions:Int;
   
private var _txtInfo:FlxText;
   
   
override public function create():Void
   
{
       
super.create();
       
        napeDebugEnabled
= true;
       
        _CBBOX
= new CbType();
        _CBGROUND
= new CbType();
       
        _collisions
= 0;
        _txtInfo
= new FlxText(10, 10, 0, "Collisions:" + _collisions);
        add
(_txtInfo);
       
        _boxGroundCollide
= new InteractionListener(CbEvent.BEGIN, InteractionType.COLLISION, _CBBOX, _CBGROUND, boxGroundCollide);
       
       
        _sprGround
= new FlxNapeSprite(FlxG.width/2, FlxG.height - 40);
        _sprGround
.makeGraphic(FlxG.width, 20, FlxColor.WHITE);
        _sprGround
.createRectangularBody(0, 0, BodyType.STATIC);
        _sprGround
.setBodyMaterial();
        add
(_sprGround);
       
        _sprBox
= new FlxNapeSprite(FlxG.width / 2, 40);
        _sprBox
.makeGraphic(20, 20, FlxColor.BLUE);
        _sprBox
.createRectangularBody(0, 0, BodyType.DYNAMIC);
        _sprBox
.setBodyMaterial();
        add
(_sprBox);
   
}
   
   
override public function update():Void
   
{
       
super.update();
        _txtInfo
.text = "Collisions:" + _collisions;
       
       
if (FlxG.mouse.justPressed && FlxNapeState.space.gravity.y == 0)
           
FlxNapeState.space.gravity.setxy(0, 500);
       
       
if (FlxG.keys.justPressed.G)
       
{
            napeDebugEnabled
= !napeDebugEnabled;
       
}
   
}
   
   
// Collision callback function
   
private function boxGroundCollide(ic:InteractionCallback):Void
   
{
        trace
("Collision");
        _collisions
++;
   
}
   
}




TestNapeCollide.hx

Thomas Bernard

unread,
Jun 4, 2015, 6:38:39 PM6/4/15
to haxef...@googlegroups.com
My answer is probably late. But I think you miss to add your CbTypes to your FlxNapeSprites and to add your listener to your napespace:

_sprGround.body.cbTypes.add(_CBGROUND);
_sprBox
.body.cbTypes.add(_CBBOX);

FlxNapeState.space.listeners.add(
_boxGroundCollide);


Nicolas Mansart

unread,
Jun 22, 2015, 10:46:15 AM6/22/15
to haxef...@googlegroups.com
Late indeed (I ended up not using nape, which was quite tricky but worked somehow), but it may be useful later, it works, thank you :)
Reply all
Reply to author
Forward
0 new messages