The camera class is used to display the game's visuals. By default one camera is created automatically, that is the same size as window. You can add more cameras or even replace the main camera using utilities in FlxG.cameras.

Every camera has following display list: flashSprite:Sprite (which is a container for everything else in the camera, it's added to FlxG.game sprite)

	|->_scrollRect:Sprite (which is used for cropping camera's graphic, mostly in tile render mode)
			|->	_flashBitmap:Bitmap	(its bitmapData property is buffer BitmapData, this var is used in blit render mode. Everything is rendered on buffer in blit render mode)
			|->	canvas:Sprite		(its graphics is used for rendering objects in tile render mode)
			|-> debugLayer:Sprite	(this sprite is used in tile render mode for rendering debug info, like bounding boxes)

Constructor

new(X:Int = 0, Y:Int = 0, Width:Int = 0, Height:Int = 0, Zoom:Float = 0)

Instantiates a new camera at the specified location, with the specified size and zoom level.

Parameters:

X

X location of the camera's display in pixels. Uses native, 1:1 resolution, ignores zoom.

Y

Y location of the camera's display in pixels. Uses native, 1:1 resolution, ignores zoom.

Width

The width of the camera display in pixels.

Height

The height of the camera display in pixels.

Zoom

The initial zoom level of the camera. A zoom level of 2 will make all pixels display at 2x resolution.

Variables

alpha:Float = 1

The alpha value of this camera display (a Number between 0.0 and 1.0).

angle:Float = 0

The angle of the camera display (in degrees). Currently yields weird display results, since cameras aren't nested in an extra display object yet.

antialiasing:Bool = false

Whether the camera display is smooth and filtered, or chunky and pixelated. Default behavior is chunky-style.

bgColor:FlxColor

The natural background color of the camera, in AARRGGBB format. Defaults to FlxG.cameras.bgColor. On flash, transparent backgrounds can be used in conjunction with useBgAlphaBlending.

buffer:BitmapData

The actual bitmap data of the camera display itself. Used in blit render mode, where you can manipulate its pixels for achieving some visual effects.

canvas:Sprite

Sprite used for actual rendering in tile render mode (instead of _flashBitmap for blitting). Its graphics is used as a drawing surface for drawTriangles() and drawTiles() methods. It is a child of _scrollRect Sprite (which trims graphics that should be unvisible). Its position is modified by updateInternalSpritePositions() method, which is called on camera's resize and scale events.

color:FlxColor = FlxColor.WHITE

The color tint of the camera display. (Internal, help with color transforming the flash bitmap.)

deadzone:FlxRect

You can assign a "dead zone" to the camera in order to better control its movement. The camera will always keep the focus object inside the dead zone, unless it is bumping up against the camera bounds. The deadzone's coordinates are measured from the camera's upper left corner in game pixels. For rapid prototyping, you can use the preset deadzones (e.g. PLATFORMER) with follow().

debugLayer:Sprite

Sprite for visual effects (flash and fade) and drawDebug information (bounding boxes are drawn on it) for tile render mode. It is a child of _scrollRect Sprite (which trims graphics that should be unvisible). Its position is modified by updateInternalSpritePositions() method, which is called on camera's resize and scale events.

filtersEnabled:Bool = true

Enables or disables the filters set via setFilters()

flashSprite:Sprite = new Sprite()

Used to render buffer to screen space. NOTE: We don't recommend modifying this directly unless you are fairly experienced. Uses include 3D projection, advanced display list modification, and more. This is container for everything else that is used by camera and rendered to the camera.

Its position is modified by updateFlashSpritePosition() method which is called every frame.

read onlyfollowLead:FlxPoint = FlxPoint.get()

Used to force the camera to look ahead of the target.

followLerp:Float = 60 / FlxG.updateFramerate

Used to smoothly track the camera as it follows: The percent of the distance to the follow target the camera moves per 1/60 sec. Values are bounded between 0.0 and FlxG.updateFrameRate / 60 for consistency across framerates. The maximum value means no camera easing. A value of 0 means the camera does not move.

height:Int = 0

How tall the camera display is, in game pixels.

read onlyinitialZoom:Float = 1

Camera's initial zoom value. Used for camera's scale handling.

maxScrollX:Null<Float>

Upper bound of the cameras scroll on the x axis

maxScrollY:Null<Float>

Upper bound of the cameras scroll on the y axis

minScrollX:Null<Float>

Lower bound of the cameras scroll on the x axis

minScrollY:Null<Float>

Lower bound of the cameras scroll on the y axis

pixelPerfectRender:Bool

Whether the positions of the objects rendered on this camera are rounded. If set on individual objects, they ignore the global camera setting. Defaults to false with FlxG.renderTile and to true with FlxG.renderBlit. WARNING: setting this to false on blitting targets is very expensive.

regen:Bool = false

Whether checkResize checks if the camera dimensions have changed to update the buffer dimensions.

read onlyscaleX:Float

The scaling on horizontal axis for this camera. Setting scaleX changes scaleX and x coordinate of camera's internal display objects (_flashBitmap, canvas and debugSprite).

read onlyscaleY:Float

The scaling on vertical axis for this camera. Setting scaleY changes scaleY and y coordinate of camera's internal display objects (_flashBitmap, canvas and debugSprite).

screen:FlxSprite

Sometimes it's easier to just work with a FlxSprite than it is to work directly with the BitmapData buffer. This sprite reference will allow you to do exactly that. Basically this sprite's pixels property is camera's BitmapData buffer. NOTE: This varible is used only in blit render mode.

FlxBloom demo shows how you can use this variable in blit render mode:

See:

scroll:FlxPoint = FlxPoint.get()

Stores the basic parallax scrolling values. This is basically camera's top-left corner position in world coordinates. There is also useful method focusOn(point:FlxPoint) which you can use to make camera look at specified point in world coordinates.

style:FlxCameraFollowStyle

Tells the camera to use this following style.

target:FlxObject

Tells the camera to follow this FlxObject object around.

read onlytargetOffset:FlxPoint = FlxPoint.get()

Offset the camera target

read onlytotalScaleX:Float

Product of camera's scaleX and game's scalemode scale.x multiplication. Added this var for less calculations at rendering time (in tile render mode).

read onlytotalScaleY:Float

Product of camera's scaleY and game's scalemode scale.y multiplication. Added this var for less calculations at rendering time (in tile render mode).

useBgAlphaBlending:Bool = false

Whether to use alpha blending for camera's background fill or not. If true then previosly drawn graphics won't be erased, and if camera's bgColor is transparent/semitransparent then you will be able to see graphics of the previous frame. Useful for blit render mode (and works only in this mode). Default value is false.

Usage example can be seen in FlxBloom demo:

See:

width:Int = 0

How wide the camera display is, in game pixels.

x:Float = 0

The X position of this camera's display (used for positioning flashSprite of this camera). Zoom does NOT affect this number. Measured in pixels from the left side of the flash window. You might be interested in using camera's scroll.x instead.

y:Float = 0

The Y position of this camera's display (used for positioning flashSprite of this camera). Zoom does NOT affect this number. Measured in pixels from the top of the flash window. You might be interested in using camera's scroll.y instead.

zoom:Float

The zoom level of this camera. 1 = 1:1, 2 = 2x zoom, etc. Indicates how far the camera is zoomed in.

Methods

copyFrom(Camera:FlxCamera):FlxCamera

Copy the bounds, focus object, and deadzone info from an existing camera.

Parameters:

Camera

The camera you want to copy from.

Returns:

A reference to this FlxCamera object.

copyPixels(?frame:FlxFrame, ?pixels:BitmapData, ?sourceRect:Rectangle, destPoint:Point, ?transform:ColorTransform, ?blend:BlendMode, smoothing:Bool = false, ?shader:FlxShader):Void

destroy():Void

Clean up memory.

drawPixels(?frame:FlxFrame, ?pixels:BitmapData, matrix:FlxMatrix, ?transform:ColorTransform, ?blend:BlendMode, smoothing:Bool = false, ?shader:FlxShader):Void

drawTriangles(graphic:FlxGraphic, vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>, ?position:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false):Void

fade(Color:FlxColor = FlxColor.BLACK, Duration:Float = 1, FadeIn:Bool = false, ?OnComplete:Void‑>Void, Force:Bool = false):Void

The screen is gradually filled with this color.

Parameters:

Color

The color you want to use.

Duration

How long it takes for the fade to finish.

FadeIn

True fades from a color, false fades to it.

OnComplete

A function you want to run when the fade finishes.

Force

Force the effect to reset.

fill(Color:FlxColor, BlendAlpha:Bool = true, FxAlpha:Float = 1.0, ?graphics:Graphics):Void

Fill the camera with the specified color.

Parameters:

Color

The color to fill with in 0xAARRGGBB hex format.

BlendAlpha

Whether to blend the alpha value or just wipe the previous contents. Default is true.

flash(Color:FlxColor = FlxColor.WHITE, Duration:Float = 1, ?OnComplete:Void‑>Void, Force:Bool = false):Void

The screen is filled with this color and gradually returns to normal.

Parameters:

Color

The color you want to use.

Duration

How long it takes for the flash to fade.

OnComplete

A function you want to run when the flash finishes.

Force

Force the effect to reset.

inlinefocusOn(point:FlxPoint):Void

Move the camera focus to this location instantly.

Parameters:

Point

Where you want the camera to focus.

follow(Target:FlxObject, ?Style:FlxCameraFollowStyle, ?Lerp:Float):Void

Tells this camera object what FlxObject to track.

Parameters:

Target

The object you want the camera to track. Set to null to not follow anything.

Style

Leverage one of the existing "deadzone" presets. Default is LOCKON. If you use a custom deadzone, ignore this parameter and manually specify the deadzone after calling follow().

Lerp

How much lag the camera should have (can help smooth out the camera movement).

getNewDrawTrianglesItem(graphic:FlxGraphic, smoothing:Bool = false, isColored:Bool = false, ?blend:BlendMode):FlxDrawTrianglesItem

onResize():Void

Called by camera front end every time you resize the game. It triggers reposition of camera's internal display objects (flashSprite, _scrollRect, _flashBitmap, canvas, debugSprite).

setFilters(filters:Array<BitmapFilter>):Void

Sets the filter array to be applied to the camera.

inlinesetPosition(X:Float = 0, Y:Float = 0):Void

Helper function to set the coordinates of this camera. Handy since it only requires one line of code.

Parameters:

X

The new x position

Y

The new y position

setScale(X:Float, Y:Float):Void

Helper function to set the scale of this camera. Handy since it only requires one line of code.

Parameters:

X

The new scale on x axis

Y

The new scale of y axis

setScrollBounds(MinX:Null<Float>, MaxX:Null<Float>, MinY:Null<Float>, MaxY:Null<Float>):Void

Specify the bounds of where the camera is allowed to move. Set the boundary of a side to null to leave that side unbounded.

Parameters:

MinX

The minimum X value the camera can scroll to

MaxX

The maximum X value the camera can scroll to

MinY

The minimum Y value the camera can scroll to

MaxY

The maximum Y value the camera can scroll to

setScrollBoundsRect(X:Float = 0, Y:Float = 0, Width:Float = 0, Height:Float = 0, UpdateWorld:Bool = false):Void

Specify the bounding rectangle of where the camera is allowed to move.

Parameters:

X

The smallest X value of your level (usually 0).

Y

The smallest Y value of your level (usually 0).

Width

The largest X value of your level (usually the level width).

Height

The largest Y value of your level (usually the level height).

UpdateWorld

Whether the global quad-tree's dimensions should be updated to match (default: false).

inlinesetSize(Width:Int, Height:Int):Void

Shortcut for setting both width and Height.

Parameters:

Width

The new sprite width.

Height

The new sprite height.

shake(Intensity:Float = 0.05, Duration:Float = 0.5, ?OnComplete:Void‑>Void, Force:Bool = true, ?Axes:FlxAxes):Void

A simple screen-shake effect.

Parameters:

Intensity

Percentage of screen size representing the maximum distance that the screen can move while shaking.

Duration

The length in seconds that the shaking effect should last.

OnComplete

A function you want to run when the shake effect finishes.

Force

Force the effect to reset (default = true, unlike flash() and fade()!).

Axes

On what axes to shake. Default value is XY / both.

snapToTarget():Void

Snaps the camera to the current target. Useful to move the camera without any easing when the target position changes and there is a followLerp.

startQuadBatch(graphic:FlxGraphic, colored:Bool, hasColorOffsets:Bool = false, ?blend:BlendMode, smooth:Bool = false, ?shader:FlxShader):FlxDrawTilesItem

startTrianglesBatch(graphic:FlxGraphic, smoothing:Bool = false, isColored:Bool = false, ?blend:BlendMode):FlxDrawTrianglesItem

stopFX():Void

Just turns off all the camera effects instantly.

update(elapsed:Float):Void

Updates the camera scroll as well as special effects like screen-shake or fades.

updateFollow():Void

Updates camera's scroll. Called every frame by camera's update() method (if camera's target isn't null).

updateScroll():Void

Updates (bounds) the camera scroll. Called every frame by camera's update() method.

Static variables

staticdefaultCameras:Array<FlxCamera>

Which cameras a FlxBasic uses to be drawn on when nothing else has been specified. By default, this is just a reference to FlxG.cameras.list / all cameras, but it can be very useful to change.

staticdefaultZoom:Float

While you can alter the zoom of each camera after the fact, this variable determines what value the camera will start at when created.

Inherited Variables

Defined by FlxBasic

ID:Int = -1

IDs seem like they could be pretty useful, huh? They're not actually used for anything yet though.

active:Bool = true

Controls whether update() is automatically called by FlxState/FlxGroup.

alive:Bool = true

Useful state for many game objects - "dead" (!alive) vs alive. kill() and revive() both flip this switch (along with exists, but you can override that).

camera:FlxCamera

Gets ot sets the first camera of this object.

cameras:Array<FlxCamera>

This determines on which FlxCameras this object will be drawn. If it is null / has not been set, it uses FlxCamera.defaultCameras, which is a reference to FlxG.cameras.list (all cameras) by default.

exists:Bool = true

Controls whether update() and draw() are automatically called by FlxState/FlxGroup.

visible:Bool = true

Controls whether draw() is automatically called by FlxState/FlxGroup.

Inherited Methods

Defined by FlxBasic

draw():Void

Override this function to control how the object is drawn. Overriding draw() is rarely necessary, but can be very useful.

kill():Void

Handy function for "killing" game objects. Use reset() to revive them. Default behavior is to flag them as nonexistent AND dead. However, if you want the "corpse" to remain in the game, like to animate an effect or whatever, you should override this, setting only alive to false, and leaving exists true.

revive():Void

Handy function for bringing game objects "back to life". Just sets alive and exists back to true. In practice, this function is most often called by FlxObject.reset().