Null Object Reference when making a FlxPath

631 views Asked by At

I'm trying to tweak this HaxeFlixel tutorial in order to make it more interesting and learn how to make games in the process. One of the first things I'm doing is restricting movement to tiles.

However, I've come to a wall – none of what I've tried is working. I've replaced the content of the movement() function in the Player class (which, for reference, is called every update()) with this:

if (FlxG.mouse.justReleased) {
    var _path:FlxPath;
    _path = new FlxPath(this, Reg.mWalls.findPath(getGraphicMidpoint(), FlxG.mouse.getWorldPosition()), speed);
}

(Reg.mWalls is where I moved PlayState._mWalls to make it accessible to this code.) According to the docs this should create a FlxPath and immediately start it going (as the first argument isn't null), but instead it generates this error:

Null Object Reference
Called from openfl._v2.display.Stage.__pollTimers (openfl/_v2/display/Stage.hx line 1020)
Called from openfl._v2.display.Stage.__checkRender (openfl/_v2/display/Stage.hx line 317)
Called from openfl._v2.display.Stage.__render (openfl/_v2/display/Stage.hx line 1035)
Called from openfl._v2.display.DisplayObjectContainer.__broadcast (openfl/_v2/display/DisplayObjectContainer.hx line 280)
Called from openfl._v2.display.DisplayObject.__broadcast (openfl/_v2/display/DisplayObject.hx line 174)
Called from openfl._v2.display.DisplayObject.__dispatchEvent (openfl/_v2/display/DisplayObject.hx line 195)
Called from openfl._v2.events.EventDispatcher.dispatchEvent (openfl/_v2/events/EventDispatcher.hx line 100)
Called from openfl._v2.events.Listener.dispatchEvent (openfl/_v2/events/EventDispatcher.hx line 270)
Called from flixel.FlxGame.onEnterFrame (flixel/FlxGame.hx line 493)
Called from flixel.FlxGame.step (flixel/FlxGame.hx line 648)
Called from flixel.FlxGame.update (flixel/FlxGame.hx line 700)
Called from flixel.FlxState.tryUpdate (flixel/FlxState.hx line 155)
Called from PlayState.update (PlayState.hx line 125)
Called from flixel.group.FlxTypedGroup.update (flixel/group/FlxTypedGroup.hx line 89)
Called from Player.update (Player.hx line 47)
Called from Player.movement (Player.hx line 59)
Called from flixel.tile.FlxTilemap.findPath (flixel/tile/FlxTilemap.hx line 794)
Called from flixel.tile.FlxTilemap.computePathDistance (flixel/tile/FlxTilemap.hx line 1802)
Called from *._Function_1_1 (openfl/_v2/display/Stage.hx line 124)
AL lib: (EE) alc_cleanup: 1 device not closed
1

There are 1 answers

0
Malltog On BEST ANSWER

I ended up fixing this problem myself.

It turns out that I hadn't set any collision data for tile 0 (the blank tile) and that my map was full of spots that had no tile at all since they were 'outside'.

The fix was to flood-fill the empty area with tile 0 and add this to PlayState.create():

Reg.mWalls.setTileProperties(0, FlxObject.ANY);