Author Topic: Problems with Tilemap.following()  (Read 1075 times)

tomategames

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
    • Tomate-Games.de
Problems with Tilemap.following()
« on: Tue, Apr 26, 2011 »
Hi!

I'm new in FLIXEL and AS3. I've tried a little bit with Tilemaps, etc. It's a real nice Engine.

At last I wanted to built a smooth following camera like it is done in the sample Mode (https://github.com/AdamAtomic/Mode). But I can't do this with the new FLIXEL version like this. I tried many different ways but it didn't work :(.

Here is the result: http://tomate-games.de/test.html

I couldn't find documentation about the new FLIXEL version.

How sould I solve it??

This is my code:
Code: [Select]
package 
{
import org.flixel.*;
/**
* ...
* @author Tomate-Games.de
*/
public class GameState extends FlxState
{
[Embed(source = "../media/game.png")] private var BackgroundImage:Class;
[Embed(source = "../media/map.txt", mimeType = "application/octet-stream")] private var MapData:Class;
[Embed(source="../media/tiles.png")] private var Tiles:Class;

private var _tilemap:FlxTilemap;
private var _player:Player;
private var _output:FlxText;

public function GameState()
{
_tilemap = new FlxTilemap();
_tilemap.loadMap(new MapData, Tiles, 8, 8);

_player = new Player(20, 20);

FlxG.camera = new FlxCamera(0, 0, FlxG.width, FlxG.height);
FlxG.camera.follow(_player);

add(new FlxSprite(0, 0, BackgroundImage));
add(_tilemap);
add(_player);
}

override public function update():void
{
super.update();

FlxG.collide(_player, _tilemap);
}

}

}

Adam Atomic

  • Founder
  • Key Contributor
  • *****
  • Posts: 852
  • Karma: +0/-0
  • new dad
    • View Profile
    • Adam Atomic
Re: Problems with Tilemap.following()
« Reply #1 on: Tue, Apr 26, 2011 »
V2.50 got rid of the weird lerpy camera in favor of using simpler dead zones.  You can still get whatever behavior you want from the camera, but if you want look-ahead or some other behavior then you should create a new object for the camera to follow, and embed the logic you want in that object.

The reasoning is that deadzones are simple and clear and easy to customize, while a smoothly interpolated camera that actually does what you want is pretty weird/difficult in comparison.

The current, v2.50 build of Mode uses FlxCamera.STYLE_PLATFORMER to constrain the camera motion, which means the dead zone is not very wide but is quite tall.  This prevents jumps from messing with the camera too much.  There are a bunch of "style presets" like that, and you can specify your own deadzone too.

You can also do the "fake" follow object trick as well!  If you do that, probably you will want to use STYLE_LOCKON (but not necessarily).  You can use the visual debugging overlay to track your invisible focus object while you are debugging it's behavior too!

tomategames

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
    • Tomate-Games.de
Re: Problems with Tilemap.following()
« Reply #2 on: Wed, Apr 27, 2011 »
Hi Adam,

it works for me now. Thank you!
I've added the following lines to my update-method:
Code: [Select]
FlxG.collide(_player, _tilemap);
FlxG.camera.setBounds(0, 0, _tilemap.width - (_tilemap.width / _tilemap.widthInTiles), _tilemap.height);
FlxG.camera.follow(_player, FlxCamera.STYLE_TOPDOWN);

(There were 1 tile-width to much in the setBounds-method width.)

Now I don't know how I can actualize smooth scrolling like in "Mode".

Here is my code:

Code: [Select]
package 
{
import org.flixel.*;
/**
* ...
* @author Tomate-Games.de
*/
public class GameState extends FlxState
{
[Embed(source = "../media/map.txt", mimeType = "application/octet-stream")] private var MapData:Class;
[Embed(source="../media/tiles.png")] private var Tiles:Class;

private var _tilemap:FlxTilemap;
private var _player:Player;
private var _output:FlxText;

public function GameState()
{
FlxG.camera = new FlxCamera(0, 0, FlxG.width, FlxG.height);

_tilemap = new FlxTilemap();
_tilemap.loadMap(new MapData, Tiles, 8, 8);
_tilemap.follow();

_player = new Player(20, 20);
_output = new FlxText(10, 10, 200);

add(_tilemap);
add(_player);
add(_output);
}

override public function update():void
{
super.update();

FlxG.collide(_player, _tilemap);
FlxG.camera.setBounds(0, 0, _tilemap.width - (_tilemap.width / _tilemap.widthInTiles), _tilemap.height);
FlxG.camera.follow(_player, FlxCamera.STYLE_TOPDOWN);

_output.y = _player.y - 16;
_output.x = _player.x;

if (!_player.alive)
{
remove(_player);
_player = new Player(20, 20);
add(_player);
}
}

}

}

Adam Atomic

  • Founder
  • Key Contributor
  • *****
  • Posts: 852
  • Karma: +0/-0
  • new dad
    • View Profile
    • Adam Atomic
Re: Problems with Tilemap.following()
« Reply #3 on: Wed, Apr 27, 2011 »
you only need to set the camera bounds and follow behavior once, up in create()

tomategames

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
    • Tomate-Games.de
Re: Problems with Tilemap.following()
« Reply #4 on: Thu, Apr 28, 2011 »
Ok, I always set it in the constructor, but now it work's!

Thanks for your support.  ::)