I'm trying to use Nape with HaxeFlixel. Sadly, there's almost no documentation on how to use the addons.nape
package and I just can't figure out why this code isn't moving the white rectangle (_test
). (I left out imports for simplicity)
class PlayState extends FlxNapeState
{
var _test = new FlxNapeSprite(16, 16);
override public function create():Void
{
super.create();
_test.makeGraphic(16, 16);
_test.body.type = BodyType.KINEMATIC;
add(_test);
}
override public function update():Void
{
_test.body.velocity.x = 100;
super.update();
}
}
There are two issues with your code:
Directly initializing the
_test
variable leads to theFlxNapeSprite
constructor call happening in the constructor of yourPlayState
.create()
is called after the state constructor. This can cause crashes and otherwise weird behavior since Flixel does its internal cleanup between the constructor call of the new state andcreate()
(graphics are disposed, for example, and in this case the NapeSpace
instance doesn't exist yet since it's created in thesuper.create()
call).The
FlxNapeSprite
constructor has acreateRectangularBody
argument which defaults totrue
and calls the function of that same name iftrue
. Since you're not passing any asset to the constructor, it ends up creating aShape
with a width and height of 0. This leads to the following error:Error: Cannot simulate with an invalid Polygon
Instead, you'll want to call
createRectangularBody()
manually aftermakeGraphic()
to create aShape
that matches the graphic's dimensions.The complete, working code looks like this:
Regarding documentation, the FlxNape demo is a great resource to learn from.