Hello!
I'm starting a new little platformer game for Android and I struggle to set up some in-game controls.
On one camera I display the level and the character, and on a second camera I display four buttons (left/right/A/B). My problem is that my part of the code who is supposed to detect if the player touch the screen where the buttons are, no longer work since I put the buttons on the second camera.
Here's my PlayState class code:
class PlayState extends FlxState
{
var _map:TiledMap;
var _mWalls:FlxTilemap;
var _mBackground:FlxTilemap;
var _player:Player;
var btnA:FlxSprite;
var btnB:FlxSprite;
var btnLeft:FlxSprite;
var btnRight:FlxSprite;
var cameraLevel:FlxCamera;
var cameraHUD:FlxCamera;
override public function create():Void
{
//We set up the cameras
cameraLevel = new FlxCamera(0, 0, FlxG.width, FlxG.height);
cameraLevel.bgColor = FlxColor.TRANSPARENT;
cameraHUD = new FlxCamera(0, 0, FlxG.width, FlxG.height);
cameraHUD.bgColor = FlxColor.TRANSPARENT;
FlxG.cameras.reset(cameraLevel);
FlxCamera.defaultCameras = [cameraLevel];
camera.zoom = 6;
//We create the level
_map = new TiledMap(AssetPaths.rogue__tmx);
FlxG.worldBounds.set(0, 0, FlxG.width, FlxG.height);
_mBackground = new FlxTilemap();
_mBackground.loadMapFromArray(cast(_map.getLayer("background"), TiledTileLayer).tileArray, _map.width, _map.height, AssetPaths.tileset__png, _map.tileWidth, _map.tileHeight, FlxTilemapAutoTiling.OFF, 1, 1, 3);
_mBackground.follow();
_mBackground.setTileProperties(1, FlxObject.ANY);
add(_mBackground);
_mWalls = new FlxTilemap();
_mWalls.loadMapFromArray(cast(_map.getLayer("platform"), TiledTileLayer).tileArray, _map.width, _map.height, AssetPaths.tileset__png, _map.tileWidth, _map.tileHeight, FlxTilemapAutoTiling.OFF, 1, 1, 3);
_mWalls.follow();
_mWalls.setTileProperties(2, FlxObject.NONE);
_mWalls.setTileProperties(3, FlxObject.NONE);
_mWalls.setTileProperties(4, FlxObject.NONE);
_mWalls.setTileProperties(5, FlxObject.NONE);
_mWalls.setTileProperties(6, FlxObject.NONE);
_mWalls.setTileProperties(7, FlxObject.NONE);
_mWalls.setTileProperties(8, FlxObject.NONE);
_mWalls.setTileProperties(9, FlxObject.NONE);
_mWalls.setTileProperties(10, FlxObject.NONE);
_mWalls.setTileProperties(11, FlxObject.NONE);
_mWalls.setTileProperties(12, FlxObject.NONE);
add(_mWalls);
//We create and place the player on the level
_player = new Player();
var tmpMap:TiledObjectLayer = cast _map.getLayer("entities");
for (e in tmpMap.objects)
{
placeEntities(e.name, e.xmlData.x);
}
add(_player);
FlxG.camera.follow(_player, TOPDOWN, 1);
//We create the controls
btnLeft = new FlxSprite( 40, FlxG.height - 130 );
btnLeft.makeGraphic( 90, 90, FlxColor.GRAY );
add(btnLeft);
btnRight = new FlxSprite( btnLeft.x + 130, FlxG.height - 130 );
btnRight.makeGraphic( 90, 90, FlxColor.GRAY );
add(btnRight);
btnA = new FlxSprite( FlxG.width - 130, FlxG.height - 130 );
btnA.makeGraphic( 90, 90, FlxColor.BLUE );
add(btnA);
btnB = new FlxSprite( btnA.x - 130, FlxG.height - 130 );
btnB.makeGraphic( 90, 90, FlxColor.RED );
add(btnB);
btnA.scrollFactor.set(0, 0);
btnB.scrollFactor.set(0, 0);
btnRight.scrollFactor.set(0, 0);
btnLeft.scrollFactor.set(0, 0);
//We add the controls to the second camera
FlxG.cameras.add(cameraHUD);
btnA.cameras = [cameraHUD];
btnB.cameras = [cameraHUD];
btnLeft.cameras = [cameraHUD];
btnRight.cameras = [cameraHUD];
super.create();
}
override public function update(elapsed:Float):Void
{
super.update(elapsed);
for (touch in FlxG.touches.list)
{
if (touch.pressed)
{
if (touch.overlaps(btnLeft, cameraHUD))
{
_player.animation.play("run");
_player.flipX = true;
_player.acceleration.x = -_player.speed;
}
if (touch.overlaps(btnRight, cameraHUD))
{
_player.animation.play("run");
_player.flipX = false;
_player.acceleration.x = _player.speed;
}
}
else
{
_player.animation.play("idle");
_player.acceleration.x = 0;
}
}
FlxG.collide(_player, _mWalls);
}
function placeEntities(entityName:String, entityData:Xml):Void
{
var x:Int = Std.parseInt(entityData.get("x"));
var y:Int = Std.parseInt(entityData.get("y"));
if (entityName == "player")
{
_player.x = x;
_player.y = y;
}
}
}
And also, if someone got any clue about why the player doesn't collide with the walls :D
I don't think the other parts of my code are needed, but let me know!