Setting a FlxCamera's position/size in worldspace based on a "parent" FlxCamera?

12 views
Skip to first unread message

Michael Zehnich

unread,
Nov 22, 2015, 2:23:20 AM11/22/15
to HaxeFlixel
Hi all,

So I'm trying to get a camera to act as the background for a tilemap: that is, have this camera be the same x/y/w/h of my tilemap, even while moving around and zooming the main camera in and out.

My reason for doing so is: I can't use scrollbounds to make it so the main camera doesn't scroll past the tilemap (for gameplay reasons), but I still want a parallax background behind my tilemap, but I don't want it to "bleed out" past the boundaries of the tilemap (which parallax sprites would of course do while you move around).

So, ideally, I'd have a camera that's the size of my tilemap, behind the tilemap, that renders my parallax background.

So right now I'm doing tilemap.getScreenPosition() to get the map's x/y coords, and then setting the background camera's position to these coords. This works great, except, when my main camera zooms in or out. The map's x/y coords of course don't change based on the main cameras zoom, so simply setting the x/y position of the second camera to the map no longer works when the main camera's zoom level != 1.

I got pretty close with some math like this to try and figure out how the zoom level would affect the size and location of the map:

var mappos:FlxPoint = FlxPoint.get(0, 0);
map
.getScreenPosition(mappos, mainCamera);
backgroundCamera
.x = mappos.x + (map.width - mappos.x) - ((map.width - mappos.x) * mainCameraZoomLevel);
backgroundCamera
.y = mappos.y + (map.height - mappos.y) - ((map.height - mappos.y) * mainCameraZoomLevel);

but it's still not quite perfect. (probably because this relies on everything being perfectly center which might not be the case for my game?)

I guess alternatively if I could somehow place a camera within a camera, that could maybe work, but you can't "add" cameras to cameras, can you? (Or, maybe make the getScreenPosition() take camera zoom into account somehow? I tried this but couldn't figure it out :v)

If anyone has a different idea for how to achieve a "parallax objects that are restricted to a rect" style effect, let me know, I'm certainly not attached to using a camera, it's just the only thing I can think of!

Of course, just putting big black FlxSprites on the sides of my tilemap in front of the background would probably do the job, but I was wondering if there was any more elegant solution available for the sake of learning :)

Thanks!
Reply all
Reply to author
Forward
0 new messages