Weird problem with checking tiles outside of Tilemap

28 views
Skip to first unread message

MegaBite

unread,
Dec 1, 2015, 3:41:49 AM12/1/15
to haxef...@googlegroups.com
Hey guys and gals!

I have a weird problem. I'm trying to set up the basic player movement for my point&click adventure and when I'm clicking outside of the tilemap, it returns a walkable tile and the player is moving there. Of course I could restrict him to the bounds of the tilemap, but I would like to find out what the problem is. I use a black and white image to define the walkable areas of my map so I can take advantage of the pathfinding function.

create():
        Tiles = new FlxTilemap();
       
Tiles.widthInTiles = 150;
       
Tiles.heightInTiles = 70;
       
Tiles.loadMap(makeTileMap(BG_BWMap), "assets/images/tiles.png", 10, 10);
       
Tiles.setTileProperties(0, FlxObject.ANY);
       
Tiles.setTileProperties(1, FlxObject.ANY);
       
Tiles.setTileProperties(2, FlxObject.NONE);

    private function makeTileMap(bwm:BitmapData):Array<Int> {
       
var map:Array<Int> = new Array<Int>();
       
var tile:Int = 0;
       
for (i in 0...Std.int(bwm.height / 10)) {
         
for (j in 0...Std.int(bwm.width / 10)) {
             
switch(bwm.getPixel(j * 10, i * 10)) {
                 
case 16777215: tile = 2;
                 
default: tile = 0;
             
}
              map
.push(tile);
       
}
       
}
       
return map;
   
}
update():
if (FlxG.mouse.justReleased) {
trace("Pixel: "+BG_BWMap.getPixel(Std.int(FlxG.mouse.x), Std.int(FlxG.mouse.y)));
trace
(Tiles.getTile(Std.int(FlxG.mouse.x / 10), Std.int(FlxG.mouse.y / 10)));   
}



Clicking outside of the Tilemap returns pixel value 0 and tile value 2 - which seems impossible to me. Any ideas what's going on here?

Edit: It seems like it's just to the left and to the right side. Very strange...

Jeru Sanders

unread,
Dec 2, 2015, 1:38:09 PM12/2/15
to HaxeFlixel
I'm confused about what you're doing, you're running getTile() on a tile outside of the map? Such as getTile(-1, -1)?
This should result in an error.

MegaBite

unread,
Dec 2, 2015, 3:04:12 PM12/2/15
to haxef...@googlegroups.com
That's the weird thing, I click outside the Tilemap and it returns not only numbers, but sometimes a 2, so the player starts moving there... ???
I will try to post an easy to recreate example...

JustGabe

unread,
Dec 2, 2015, 10:31:09 PM12/2/15
to HaxeFlixel
Maybe if you include this code it will work?
//Increase world bounds to allow collisions between player and other objects
FlxG.worldBounds.width = tileSize * Tiles.widthInTiles;
FlxG.worldBounds.height = tileSize Tiles.heightInTiles;


MegaBite

unread,
Dec 3, 2015, 1:21:56 AM12/3/15
to HaxeFlixel
Thanks, but it didn't change anything (probably because it's not collision related?)

Here is a simple example of my problem. All you need is a 10x10 tiles.png. If i click outside of the tiles it returns 0 and to the right side also 2.

package;

import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.util.FlxMath;
import flixel.tile.FlxTilemap;

class MenuState extends FlxState
{

   
public var Tiles:FlxTilemap;
     
   
override public function create():Void
   
{
       
super.create();
       
Tiles = new FlxTilemap();

       
var map:String = "1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                             2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                             2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                             2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                             2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                             2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                             2,2,2,2,2,2,2,2,2,2"
;
       
Tiles.loadMap(map, "assets/images/tiles.png", 10);
        add
(Tiles);
       
   
}

   
override public function destroy():Void
   
{
       
super.destroy();
   
}

   
override public function update():Void
   
{
       
if (FlxG.mouse.justPressed) {
        trace
(Tiles.getTile(Std.int(FlxG.mouse.x / 10), Std.int(FlxG.mouse.y / 10))+" at "+Std.int(FlxG.mouse.x)+" "+Std.int(FlxG.mouse.y));    
       
}
       
super.update();
   
}    
}


 
Reply all
Reply to author
Forward
0 new messages