Okay, so I’ve tried working with only one tilemap (which means only one tileset and layer) to make it work before I move on to more tilesets.
This is the code for the GameWorld so far:
package game_handling
{
imports here
public class GameWorld extends World
{
[Embed(source="../assets/circle_gradient2.png")] public static const SPR_LIGHT_CIRCLE_GRADIENT:Class;
random vars here
**public var collisionData:Grid;**
//Level1
[Embed(source = "../../levels/level_1.tmx", mimeType = "application/octet-stream")] private const LEVEL1:Class;
[Embed(source="../../levels/Bricks1_DarkGray.png")]private const BRICKS1_DARKGRAY:Class;
[Embed(source="../../levels/Bricks1_Gold.png")]private const BRICKS1_GOLD:Class;
[Embed(source="../../levels/Bricks1_Ice.png")]private const BRICKS1_ICE:Class;
[Embed(source="../../levels/Bricks1_Purple.png")]private const BRICKS1_PURPLE:Class;
[Embed(source="../../levels/Bricks1_White.png")]private const BRICKS1_WHITE:Class;
public function GameWorld()
{
}
override public function begin():void
{
super.begin();
//var level:Level1 = Level1(add(new Level1(LEVEL_1)));
//var levelstart:Level1 = new Level1();
//Hero stats
add(stats);
//Hp and xp graphics
add(hpglobe);
add(hpglobe_hp);
add(xpbar);
add(xpbar_xp);
add(staminabar);
add(staminabar_stamina);
//HUD
add(gui);
add(sounds);
//Lighting
// create new lighting
add(lighting = new Lighting(FP.screen.width, FP.screen.height));
// add lights
/*for (var i:uint = 0; i < 50; i++)
{
var image:Image = new Image(FP.choose(SPR_LIGHT_SQUARE, SPR_LIGHT_CIRCLE, SPR_LIGHT_CIRCLE_GRADIENT));
image.centerOO();
lighting.add(new Light(FP.rand(FP.screen.width), FP.rand(FP.screen.height), image, Math.random() * 1.5 + 0.5, Math.random(), Math.random() * 100 * FP.elapsed));
}*/
//add light that follows mouse
var image:Image = new Image(SPR_LIGHT_CIRCLE_GRADIENT);
image.centerOO();
mouseLight = new Light(0, 0, image, 4);
lighting.add(mouseLight);
loadLevel1();
}
override public function update():void
{
super.update();
mouseLight.x = mouseX;
mouseLight.y = mouseY;
}
public function loadLevel1():void
{
var mapXML:XML = FP.getXML(LEVEL1);
var mapWidth:uint = uint(mapXML.layer.@width);
var mapHeight:uint = uint(mapXML.layer.@height);
var tileX:uint = 0;
var tileY:uint = 0;
collisionData = new Grid(mapWidth * 120, mapHeight * 120, 120, 120);
// Create a tilemap to show the level.
// Tile size is hardcoded, but could be pulled from the XML.
_map_Ground = new Tilemap(BRICKS1_DARKGRAY, mapWidth * 120, mapHeight * 120, 120, 120);
// Iterate through tiles, adding them to the tilemap.
for each (var tile:XML in mapXML.layer.data.tile)
{
// Once the end of the map is reached, loop back to the start.
if (tileX >= mapWidth)
{
tileX = 0;
tileY++;
}
// Ignore empty tiles.
if (tile.@gid != 0)
{
_map_Ground.setTile(tileX, tileY, uint(tile.@gid - 1));
}
var groundTiles:String = mapXML.layer.attribute("name");
switch (groundTiles)
{
case "Ground":
collisionData.setTile(tileX, tileY, true);
break;
default:
trace("NOTHING")
}
// Move to the next tile.
tileX++;
}
for each (var objectgroup:XML in mapXML.objectgroup)
{
var objectGroup:String = objectgroup.attribute("name");
switch(objectGroup)
{
case "Spawn":
for each (var object:XML in objectgroup.object)
{
var objectVar:String = objectgroup.object.attribute("name");
switch(objectVar)
{
case "Hero":
FP.world.add(new Hero(new Point(int(object.@x), int(object.@y))));
break;
default:
trace("no spawns")
}
}
break;
default:
trace("unrecognized object type:", objectgroup.attribute("name"));
}
}
// Add the map to the world.
addGraphic(_map_Ground);
updateCollision();
}
public function updateCollision():void
{
var collision_:Collision = FP.world.getInstance("collision");
if (collision_ == null)
{
trace("no collision")
FP.world.add(new Collision(0, 0, _map_Ground, collisionData))
}else {
trace("updating collision")
}
}
//End
}
}
As you can see, I tried making a grid which is the same thing as the tiles from the “ground” layer (from the XML).
I also have another class that handles the collision, Collision.as:
package game_handling
{
import net.flashpunk.Entity;
import net.flashpunk.Graphic;
import net.flashpunk.Mask;
public class Collision extends Entity
{
public function Collision(x:Number=0, y:Number=0, graphic:Graphic=null, mask:Mask=null)
{
name = "Collision"
type = "ground";
super(x, y, graphic, mask);
}
override public function update():void
{
trace("collision is active")
}
}
}
I’m guessing it is all working except for one thing, the collision. The hero is simply stuck in the air, as if the grid isn’t aligned to the stage. How can I make the grid visible?
Btw, the FPS is really low now… I’m gonna try and see what is causing this