You know what, I’ve had enough. Anyone who can just tell me exactly what to type gets a payment through paypal.
Here is all the code:
GameWorld:
(in the begin function)
var levelstart:Level1 = new Level1();
Level1.as:
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;
import net.flashpunk.graphics.Image;
public class Level1 extends Entity
{
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();
[Embed(source = "Level_1.tmx", mimeType = "application/octet-stream")] public static const LEVEL1:Class;
public function Level1()
{
type = "ground";
loadXML();
}
private function loadXML():void
{
xml = new XML(new LEVEL1);
mapWidth = xml.attribute("width");
mapHeight = xml.attribute("height");
tileWidth = xml.attribute("tilewidth");
tileHeight = xml.attribute("tileheight");
var tilesetCounter:uint = 0;
for each (var tileset:XML in xml.tileset)
{
var imageWidth:uint = xml.tileset.image.attribute("width")[tilesetCounter];
var imageHeight:uint = xml.tileset.image.attribute("height")[tilesetCounter];
var firstGid:uint = xml.tileset.attribute("firstgid")[tilesetCounter];
var tilesetName:String = xml.tileset.attribute("name")[tilesetCounter];
var tilesetTileWidth:uint = xml.tileset.attribute("tilewidth")[tilesetCounter];
var tilesetTileHeight:uint = xml.tileset.attribute("tileheight")[tilesetCounter];
var tilesetImagePath:String = xml.tileset.image.attribute("source")[tilesetCounter];
tileSets.push(new TileSet(firstGid, tilesetName, tilesetTileWidth, tilesetTileHeight, tilesetImagePath, imageWidth, imageHeight));
tilesetCounter++;
}
totalTileSets = tilesetCounter;
// load images for tileset
for (var i:int = 0; i < totalTileSets; i++)
{
trace("load tileset at " + tileSets[i].source);
var loader:TileCodeEventLoader = new TileCodeEventLoader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, tilesLoadComplete);
loader.tileSet = tileSets[i];
var request:URLRequest = new URLRequest(tileSets[i].source);
loader.load(request);
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 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
var screenbmap:Entity = new Entity();
screenbmap.graphic = new Image(screenBitmap);
FP.world.add(screenbmap);
// load top layer
var screenbmaptoplayer:Entity = new Entity();
screenbmaptoplayer.graphic = new Image(screenBitmapTopLayer);
FP.world.add(screenbmaptoplayer);
}
}
}
The TMX file (called level_1.tmx) I only excluded the actual data, like "< tile gid = “blabla” > and the objects layer
<map version="1.0" orientation="orthogonal" width="32" height="9" tilewidth="120" tileheight="120">
<tileset firstgid="106" name="Dungeon1" tilewidth="120" tileheight="120">
<image source="../../assets/tilesets/Dungeon1.png" trans="ffffff" width="3500" height="2536"/>
</tileset>
<tileset firstgid="715" name="Dungeon1_front" tilewidth="120" tileheight="120">
<image source="../../assets/tilesets/Dungeon1_front.png" trans="ffffff" width="3500" height="2536"/>
</tileset>
<tileset firstgid="1324" name="Dungeon1_back" tilewidth="120" tileheight="120">
<image source="../../assets/tilesets/Dungeon1_back.png" trans="ffffff" width="3500" height="2536"/>
</tileset>
<tileset firstgid="1933" name="Dungeon1_BG" tilewidth="120" tileheight="120">
<image source="../../assets/tilesets/Dungeon1_background.png" trans="ffffff" width="3500" height="2536"/>
</tileset>
<layer name="BG" width="32" height="9">
<data>
etc etc```
TileSet.as:
package levels.level1
{
import flash.display.BitmapData;
public class TileSet
{
public var firstgid:uint;
public var lastgid:uint;
public var name:String;
public var tileWidth:uint;
public var source:String;
public var tileHeight:uint;
public var imageWidth:uint;
public var imageHeight:uint;
public var bitmapData:BitmapData;
public var tileAmountWidth:uint;
public function TileSet(firstgid:uint, name:String, tileWidth:uint, tileHeight:uint, source:String, imageWidth:uint, imageHeight:uint)
{
this.firstgid = firstgid;
this.name = name;
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.source = source;
this.imageWidth = imageWidth;
this.imageHeight = imageHeight;
tileAmountWidth = Math.floor(imageWidth / tileWidth);
lastgid = tileAmountWidth * Math.floor(imageHeight / tileHeight) + firstgid - 1;
}
}
}
And finally TileCodeEventLoader.as:
package levels.level1
{
import flash.display.Loader;
public class TileCodeEventLoader extends Loader
{
public var tileSet:TileSet;
}
}