class FlxCamera
package flixel
extends FlxBasic
extended by FlxShaderMaskCamera, FlxZoomCamera
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)
Static variables
staticdefaultCameras:Array<FlxCamera>
Used behind-the-scenes during the draw phase so that members use the same default cameras as their parent.
Prior to 4.9.0 it was useful to change this value, but that feature is deprecated.
Instead use the defaultDrawTarget
argument in FlxG.cameras.add
.
orFlxG.cameras.setDefaultDrawTarget
.
See also:
FlxG.cameras.setDefaultDrawTarget
staticdefaultZoom:Float = 1.0
Any FlxCamera
with a zoom of 0 (the default value) will have this zoom value.
Constructor
new(X:Float = 0, Y:Float = 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
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 BitmapData
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 invisible).
Its position is modified by updateInternalSpritePositions()
, which is called on camera's resize and scale events.
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 invisible).
Its position is modified by updateInternalSpritePositions()
, which is called on camera's resize and scale events.
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()
which is called every frame.
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 60 / FlxG.updateFramerate
for consistency across framerates.
The maximum value means no camera easing. A value of 0
means the camera does not move.
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.
pixelPerfectShake:Null<Bool> = null
If true, screen shake will be rounded to game pixels. If null, pixelPerfectRender is used.
5.4.0
.read onlyscaleX:Float = 0
The scaling on horizontal axis for this camera.
Setting scaleX
changes scaleX
and x coordinate of camera's internal display objects.
read onlyscaleY:Float = 0
The scaling on vertical axis for this camera.
Setting scaleY
changes scaleY
and y coordinate of camera's internal display objects.
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 variable is used only in blit render mode.
The FlxBloom demo shows how you can use this variable in blit render mode.
See also:
scroll:FlxPoint = FlxPoint.get()
Stores the basic parallax scrolling values.
This is basically the camera's top-left corner position in world coordinates.
There is also focusOn(point:FlxPoint)
which you can use to
make the camera look at specified point in world coordinates.
useBgAlphaBlending:Bool = false
Whether to use alpha blending for camera's background fill or not.
If true
then previously 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 also:
read onlyviewBottom:Float
The bottom side of the camera's view, in world space.
5.2.0
.read onlyviewMarginBottom:Float
The margin cut off on the bottom by the camera zooming in (or out), in world space
5.2.0
.read onlyviewMarginLeft:Float
The margin cut off on the left by the camera zooming in (or out), in world space.
5.2.0
.read onlyviewMarginRight:Float
The margin cut off on the right by the camera zooming in (or out), in world space
5.2.0
.read onlyviewMarginTop:Float
The margin cut off on the top by the camera zooming in (or out), in world space
5.2.0
.read onlyviewMarginX:Float
The margin cut off on the left and right by the camera zooming in (or out), in world space.
5.2.0
.read onlyviewMarginY:Float
The margin cut off on the top and bottom by the camera zooming in (or out), in world space.
5.2.0
.x:Float = 0
The X position of this camera's display. zoom
does NOT affect this number.
Measured in pixels from the left side of the window.
You might be interested in using camera's scroll.x
instead.
y:Float = 0
The Y position of this camera's display. zoom
does NOT affect this number.
Measured in pixels from the top of the 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.
Note: Changing this property from it's initial value will change properties like:
viewX
, viewY
, viewWidth
, viewHeight
and many others. Cameras always zoom in to
their center, meaning as you zoom in, the view is cut off on all sides.
Methods
bindScrollPos(scrollPos:FlxPoint):FlxPoint
Takes the desired scroll position and restricts it to the camera's min/max scroll properties. This modifies the given point.
Parameters:
scrollPos | The scroll position |
---|
Returns:
The same point passed in, moved within the scroll bounds
5.4.0
.inlinecontainsPoint(point:FlxPoint, width:Float = 0, height:Float = 0):Bool
Checks whether this camera contains a given point or rectangle, in screen coordinates.
4.3.0
.inlinecontainsRect(rect:FlxRect):Bool
Checks whether this camera contains a given rectangle, in screen coordinates.
4.11.0
.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:Null<Rectangle>, destPoint:Point, ?transform:Null<ColorTransform>, ?blend:Null<BlendMode>, smoothing:Bool = false, ?shader:Null<FlxShader>):Void
drawPixels(?frame:FlxFrame, ?pixels:BitmapData, matrix:FlxMatrix, ?transform:Null<ColorTransform>, ?blend:Null<BlendMode>, smoothing:Bool = false, ?shader:Null<FlxShader>):Void
drawTriangles(graphic:FlxGraphic, vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>, ?position:FlxPoint, ?blend:Null<BlendMode>, repeat:Bool = false, smoothing:Bool = false, ?transform:Null<ColorTransform>, ?shader:Null<FlxShader>):Void
fade(Color:FlxColor = FlxColor.BLACK, Duration:Float = 1, FadeIn:Bool = false, ?OnComplete:() ‑> Void, Force:Bool = false):Void
fill(Color:FlxColor, BlendAlpha:Bool = true, FxAlpha:Float = 1.0, ?graphics:Null<Graphics>):Void
Fill the camera with the specified color.
Parameters:
Color | The color to fill with in |
---|---|
BlendAlpha | Whether to blend the alpha value or just wipe the previous contents. Default is |
flash(Color:FlxColor = FlxColor.WHITE, Duration:Float = 1, ?OnComplete:() ‑> 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 |
---|---|
Style | Leverage one of the existing "deadzone" presets. Default is |
Lerp | How much lag the camera should have (can help smooth out the camera movement). |
getViewMarginRect(?rect:FlxRect):FlxRect
The size and position of this camera's margins, via viewMarginLeft
, viewMarginTop
, viewWidth
and viewHeight
.
5.2.0
.getViewRect(?rect:FlxRect):FlxRect
The size and position of this camera's margins, via viewMarginLeft
, viewMarginTop
, viewWidth
and viewHeight
.
Notes: Deprecated, in 4.11.0 this was made public, but the wording is confusing.
In flixel 6.0.0 this will be changed to use viewX
, viewY
, viewWidth
and viewHeight
,
meaning, this will return the world coordinates of the camera.
4.11.0
.onResize():Void
Called by camera front end every time you resize the game. It triggers reposition of camera's internal display objects.
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 |
---|---|
Y | The smallest Y value of your level (usually |
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: |
inlinesetSize(Width:Int, Height:Int):Void
Shortcut for setting both width
and height
.
Parameters:
Width | The new camera width. |
---|---|
Height | The new camera height. |
shake(Intensity:Float = 0.05, Duration:Float = 0.5, ?OnComplete:() ‑> 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 = |
Axes | On what axes to shake. Default value is |
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
.
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.