HaxeFlixel / flixel

Free, cross-platform 2D game engine powered by Haxe and OpenFL
https://haxeflixel.com/
MIT License
1.99k stars 438 forks source link

FlxTilemap#computePathDistance() works in neko and flash targets, but not in html5 target #1390

Closed bguiz closed 9 years ago

bguiz commented 9 years ago

The class flixel.tile.FlxTilemap has a method computePathDistance. When compiled with the neko or flash targets, this compiles and runs without issue. When compiled using openfl-bitfive with the html5 target, however, this compiles, but does not run.

The relevant parts of the transpiled Javascript code:

flixel.tile.FlxTilemap.prototype = $extend(flixel.FlxObject.prototype,{
...
,computePathDistance: function(StartIndex,EndIndex,WideDiagonal,StopOnEnd) {
    ...
    while(i < mapSize) {
        if(this._tileObjects[this._data[i]].allowCollisions != 0) distances[i] = -2; else distances[i] = -1;
        i++;
    }

An error occurs on the line before the i++, because this._tileObjects[this._data[i]] is sometimes undefined:

 Uncaught TypeError: Cannot read property 'allowCollisions' of undefined

@YellowAfterlife the maintainer of openfl-bitfive thinks that this issue has to do with haxeflixel, not openfl-bitfive: https://github.com/YellowAfterlife/openfl-bitfive/issues/31#issuecomment-64843735

After comparing the original Haxe source and the transpiled Javascript output, it does look as if the error surfaces here, but most likely occurs earlier on, elsewhere. The likely culprits are when _tileObjects (Array<FlxTile>) or _data (Array<Int>) are populated. However, that is about as deep as I know how to dig.

Can someone smarter than I please take a look and see if there is something going amiss here?

MSGhero commented 9 years ago

Are you on dev flixel? How are you loading in the map data (ie which method)?

bguiz commented 9 years ago

@MSGhero Nope I'm not on dev flixel, where is that?

Here's what I do in PlayState.hx:

override public function create():Void
{
    tileMap = new FlxTilemap();
    var mapCsv = Assets .getText("assets/data/map.csv");
    tileMap.loadMap(mapCsv, "assets/images/tileset.png", TILE_WIDTH, TILE_HEIGHT, 0, 1);
    add(tileMap);
MSGhero commented 9 years ago

http://haxeflixel.com/documentation/install-development-flixel/

The loadMap function has been replaced with other ones, so idk if they would fix this now. There's lots of other breaking changes from master to dev, so watch for the errors.

Gama11 commented 9 years ago

@bguiz Can you provide the necessary assets to compile that code snippet? Also, where's your pathfinding code? computePathDistance() wouldn't be called without a findPath() call.

Does the Pathfinding demo run fine?

bguiz commented 9 years ago

@msghero thanks for that. What has FlxTilemap#loadMap been replaced with though?

Also, I think I would prefer to compile against released versions of libraries.. As it is, I'm having trouble with API differences from one version to the next preventing openfl-bitfive from compiling it, this using the bleeding edge is likely to just exacerbate this.

bguiz commented 9 years ago

@gama11 Ok thanks, I'll find the path finding demo and attempt to reproduce this.

bguiz commented 9 years ago

@gama11 Just got a chance to try this out. Unfortunately, the path finding demo does not work. It fails in a different function - findPath instead of computePathDistance - but the error does look like it most likely shares a common cause, which is that either _tileObjects or _data is incorrectly populated at the time that findPath function is called:

flixel.tile.FlxTilemap.prototype = $extend(flixel.FlxObject.prototype,{
...
,findPath: function(Start,End,Simplify,RaySimplify,WideDiagonal) {
...
if(this._tileObjects[this._data[startIndex]].allowCollisions > 0 || this._tileObjects[this._data[endIndex]].allowCollisions > 0) return null;

Results in an error: Uncaught TypeError: Cannot read property 'allowCollisions' of undefined

In the pathfinding demo app,

$ /usr/lib/haxe/lib/flixel-demos/1,1,1/Flixel Features/Pathfinding
$ lime test html5
# in the browser, click on "Move to Goal" button immediately, without doing anything else

this resulted in an error because

This might be the root cause of it, but I am not able to say for sure.

bguiz commented 9 years ago

Nudge, nudge On 28 Nov 2014 22:41, "Gama11" notifications@github.com wrote:

@bguiz https://github.com/bguiz Can you provide the necessary assets to compile that code snippet? Also, where's your pathfinding code? computePathDistance() wouldn't be called without a findPath() call.

Does the Pathfinding demo run fine?

— Reply to this email directly or view it on GitHub https://github.com/HaxeFlixel/flixel/issues/1390#issuecomment-64885429.

Gama11 commented 9 years ago

Can't reproduce this issue in the Pathfinding demo anymore on latest dev. There've been fixes here and there, so I assume this is not an issue anymore.