Author Topic: Getting the player position using color  (Read 913 times)

caladan21ph

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Getting the player position using color
« on: Sat, Mar 3, 2012 »
Hi! I'm trying to use Paint.net to draw the level and then just use FlxTilemap.bitmapToCsv. What I want to do this is to specify the player starting position in the map using a color. How would I go about doing this? Thanks!
Check out my game journal: http://alternatdev.wordpress.com

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Getting the player position using color
« Reply #1 on: Sun, Mar 4, 2012 »
The basic way would be to just iterate over the image. Since you're doing this one at the start of your level, the likelihood that this will be too slow is very little, so it would be extremely simple to do:

Code: [Select]
var pixels:BitmapData = new FlxSprite(0, 0, YOUR_MAP_BITMAP).pixels;
for (var x:uint = 0; x < pixels.width; x++) {
  for (var y:uint = 0; y < pixels.height; y++) {
    if (pixels.getPixel(x, y) == PLAYER_START_COLOR) {
      setPlayerStart(x, y);
      return;
    }
  }
}

Another way off the top of my head is:

Load the map using your bitmapToCSV. Map the player color to some specific tile ID that is blank or whatever. Then do:

Code: [Select]
var startIndexArray:Array = tilemap.getTileInstances(PLAYER_START_ID);
if (startIndexArray.length != 1) {
  throw new Error("Didn't have a singular player starting position.");
}
var startIndex:uint = startIndexArray[0];
var startX:uint = startIndex % tilemap.width;
var startY:uint = Math.floor(startIndex / tilemap.height);

These are just off the top of my head, so might not work as is, but should help point you in the right direction.

caladan21ph

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: Getting the player position using color
« Reply #2 on: Sun, Mar 4, 2012 »
It worked!Thanks! :) BTW I'm studying your work. Great stuff!!
Check out my game journal: http://alternatdev.wordpress.com