I “converted” the code from this tutorial:
to work with flashpunk.
package levels.level1
{
import blocks.*;
import enemies.*;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.events.*;
import flash.events.Event;
import flash.geom.*;
import flash.geom.Point;
import flash.net.URLLoader;
import flash.net.URLRequest;
import hero.*;
import net.flashpunk.Entity;
import net.flashpunk.FP;
public class Level1 extends Entity
{
private var xmlLoader:URLLoader; // for reading the tmx file
private var xml:XML; // for storing the tmx data as xml
private var screenBitmap:Bitmap; // for drawing the map
public var screenBitmapTopLayer:Bitmap; // data of an image, for drawing the map that the character will move under
public var mapWidth:uint;
public var mapHeight:uint;
private var tileWidth:uint;
private var tileHeight:uint;
private var tileSets:Array = new Array();
private var totalTileSets:uint = 0;
private var tileSetsLoaded:uint = 0;
public var collisionTiles:Array = new Array();
private var eventLoaders:Array = new Array();
public function Level1(xml:Class)
{
type = "ground";
xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlLoadComplete);
xmlLoader.load(new URLRequest("../Level_1/level_1.tmx"));
}
private function xmlLoadComplete(e:Event):void
{
xml = new XML(e.target.data);
mapWidth = xml.attribute("width");
mapHeight = xml.attribute("height");
tileWidth = xml.attribute("tilewidth");
tileHeight = xml.attribute("tileheight");
var xmlCounter:uint = 0;
for each (var tileset:XML in xml.tileset)
{
var imageWidth:uint = xml.tileset.image.attribute("width")[xmlCounter];
var imageHeight:uint = xml.tileset.image.attribute("height")[xmlCounter];
var firstGid:uint = xml.tileset.attribute("firstgid")[xmlCounter];
var tilesetName:String = xml.tileset.attribute("name")[xmlCounter];
var tilesetTileWidth:uint = xml.tileset.attribute("tilewidth")[xmlCounter];
var tilesetTileHeight:uint = xml.tileset.attribute("tileheight")[xmlCounter];
var tilesetImagePath:String = xml.tileset.image.attribute("source")[xmlCounter];
tileSets.push(new TileSet(firstGid, tilesetName, tilesetTileWidth, tilesetTileHeight, tilesetImagePath, imageWidth, imageHeight));
xmlCounter++;
}
totalTileSets = xmlCounter;
// load images for tileset
for (var i:int = 0; i < totalTileSets; i++)
{
trace("load tilset at " + tileSets[i].source);
var loader:TileCodeEventLoader = new TileCodeEventLoader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, tilesLoadComplete);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
loader.tileSet = tileSets[i];
loader.load(new URLRequest("../assets/" + tileSets[i].source));
eventLoaders.push(loader);
}
screenBitmap = new Bitmap(new BitmapData(mapWidth * tileWidth, mapHeight * tileHeight, false, 0x22ffff));
screenBitmapTopLayer = new Bitmap(new BitmapData(mapWidth * tileWidth, mapHeight * tileHeight, true, 0));
}
private function progressHandler(event:ProgressEvent):void
{
trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}
private function tilesLoadComplete(e:Event):void
{
var currentTileset:TileSet = e.target.loader.tileSet;
currentTileset.bitmapData = Bitmap(e.target.content).bitmapData;
tileSetsLoaded++;
// wait until all the tileset images are loaded before we combine them layer by layer into one bitmap
if (tileSetsLoaded == totalTileSets)
{
addTileBitmapData();
}
}
private function addTileBitmapData():void
{
// load each layer
for each (var layer:XML in xml.layer)
{
var tiles:Array = new Array();
var tileLength:uint = 0;
// assign the gid to each location in the layer
for each (var tile:XML in layer.data.tile)
{
var gid:Number = tile.attribute("gid");
// if gid > 0
if (gid > 0)
{
tiles[tileLength] = gid;
}
tileLength++;
}
var useBitmap:BitmapData;
var layerName:String = layer.attribute("name")[0];
// decide where we're going to put the layer
var layerMap:int = 0;
switch (layerName)
{
case "Top":
layerMap = 1;
break;
default:
//trace("using base layer");
}
// store the gid into a 2d array
var tileCoordinates:Array = new Array();
for (var tileX:int = 0; tileX < mapWidth; tileX++)
{
tileCoordinates[tileX] = new Array();
for (var tileY:int = 0; tileY < mapHeight; tileY++)
{
tileCoordinates[tileX][tileY] = tiles[(tileX + (tileY * mapWidth))];
}
}
for (var spriteForX:int = 0; spriteForX < mapWidth; spriteForX++)
{
for (var spriteForY:int = 0; spriteForY < mapHeight; spriteForY++)
{
var tileGid:int = int(tileCoordinates[spriteForX][spriteForY]);
var currentTileset:TileSet;
// only use tiles from this tileset (we get the source image from here)
for each (var tileset1:TileSet in tileSets)
{
if (tileGid >= tileset1.firstgid - 1 && tileGid <= tileset1.lastgid)
{
// we found the right tileset for this gid!
currentTileset = tileset1;
break;
}
}
var destY:int = spriteForY * tileWidth;
var destX:int = spriteForX * tileWidth;
// basic math to find out where the tile is coming from on the source image
tileGid -= currentTileset.firstgid - 1;
var sourceY:int = Math.ceil(tileGid / currentTileset.tileAmountWidth) - 1;
var sourceX:int = tileGid - (currentTileset.tileAmountWidth * sourceY) - 1;
// copy the tile from the tileset onto our bitmap
if (layerMap == 0)
{
screenBitmap.bitmapData.copyPixels(currentTileset.bitmapData, new Rectangle(sourceX * currentTileset.tileWidth, sourceY * currentTileset.tileWidth, currentTileset.tileWidth, currentTileset.tileHeight), new Point(destX, destY), null, null, true);
}
else if (layerMap == 1)
{
screenBitmapTopLayer.bitmapData.copyPixels(currentTileset.bitmapData, new Rectangle(sourceX * currentTileset.tileWidth, sourceY * currentTileset.tileWidth, currentTileset.tileWidth, currentTileset.tileHeight), new Point(destX, destY), null, null, true);
}
}
}
}
/*for each (var objectgroup:XML in xml.objectgroup)
{
var objectGroup:String = objectgroup.attribute("name");
switch (objectGroup)
{
case "Collision":
for each (var object:XML in objectgroup.object)
{
var rectangle:Shape = new Shape();
rectangle.graphics.beginFill(0x0099CC, 1);
rectangle.graphics.drawRect(0, 0, object.attribute("width"), object.attribute("height"));
rectangle.graphics.endFill();
rectangle.x = object.attribute("x");
rectangle.y = object.attribute("y");
collisionTiles.push(rectangle);
FP.world.add(rectangle);
}
break;
default:
trace("unrecognized object type:", objectgroup.attribute("name"));
}
}*/
// load background layer
FP.world.add(screenBitmap);
// load player, enemies, powerups, etc
// rectangle just to demonstrate how something would look in-between layers
var playerExample:Shape = new Shape();
playerExample.graphics.beginFill(0x0099CC, 1);
playerExample.graphics.lineStyle(2); // outline rectangle
playerExample.graphics.drawRect(0, 0, 100, 100);
playerExample.graphics.endFill();
playerExample.x = 420;
playerExample.y = 260;
collisionTiles.push(playerExample);
FP.world.add(playerExample);
// load top layer
FP.world.add(screenBitmapTopLayer);
}
}
}
Level1.as is in the same fashion as in the tutorial,
tileset.as and tilecodeventloader.as too.
I get these errors:
col: 17 Error: Implicit coercion of a value of type flash.display:Bitmap to an unrelated type net.flashpunk:Entity.
col: 17 Error: Implicit coercion of a value of type flash.display:Shape to an unrelated type net.flashpunk:Entity
col: 17 Error: Implicit coercion of a value of type flash.display:Bitmap to an unrelated type net.flashpunk:Entity..