Author Topic: Layering with FlXGroup  (Read 1526 times)


  • Member
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Layering with FlXGroup
« on: Sat, Aug 29, 2015 »
Hello all, its been a while. I'm trying to create a tilebased game that involves the player moving around a city. When the player is in front of a building, he overlaps the building. If the player is behind a building, it overlaps him. I have everything laid out in rows of buildings so that if he's in front of one building, he's in front of them all for that row. Everything sounds good in theory. I'm using FlxGroups to contain all the buildings of a particular row. I have around 30 rows which means 30 FlxGroups. At the moment they are all created in my Level1 which is a FlxGroup itself. Level1 is added to the Playstate (a FlxState) where the player is added as well. My issue however is I'm not actually able to control the layer order of the FlxGroups that contain rows of buildings. I don't think I fully understand the sort command for FlxGroups.
In Level1, the building positions are parsed from the tilemapCSV (there is a tilemapCSV for each row), and building sprites are populated according to that tilemap (works fine).
Those sprites are added to a FlxGroup which is named based on which row those buildings are in (Example: MapRowLayer3 is the FlxGroup that contains all building sprites for row 3, populated by Map3.CSV) - (works fine).
In the Playstate, I have a public variable named Layers:FlxGroup - (ok).
In my override public function create():Void, I create a new Layers FlxGroup. I tell it Layers.add(Level1.MapRowLayer3) and Layers.add(Level1.MapRowLayer4). Then to add Level1 and Level1.MapRowLayer3 & 4, and of course the player.
During my update function - for testing purposes - I have it so that if the player's y position is less than or equal to a certain number, buildings that (already) overlap eachother from when they wre added to the playstate, will swap layer positions (what was before on top, is now below). However, I have tried several combinations and nothing seems to work. Maybe I'm not typing in the correct code? Does anyone have any experience with groups that can be sorted like layers? Being called on top of eachother or below eachother? Is it possible? And can I even add a player to a group so that HE can eventually be the one to overlap or be behind objects?
I'll post my code for my playstate

Code: [Select]
* ...
* @author Moussetticus
import org.flixel.*;

public class PlayState extends FlxState
private var player:PlayerSprite;
private var level1:Level1;
public var layers:FlxGroup;

public function PlayState()


override public function create():void
layers = new FlxGroup();
level1 = new Level1;
level1.width = 1280;
level1.height = 1280;

player = new PlayerSprite(288, 288);
FlxG.worldBounds = new FlxRect(0, 0, level1.width, level1.height);, 0, level1.width, level1.height);, FlxCamera.STYLE_TOPDOWN);
override public function update():void
FlxG.collide(player, level1);
trace("X = " + player.x + "Y = " + player.y);
if (player.y <= 320)
//layers.sort("level1.MapRowLayer3", DESCENDING);
}else if (player.y >= 320) {
//level1.sort("y", DESCENDING);


« Last Edit: Sun, Aug 30, 2015 by Moussetticus »