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++;
}
}