Skip to main content

CCCamera2D

CCCamera2D

Cocos2D

A 2D-focused camera with scroll, shake, follow, zoom, and viewport culling. Unlike CCCamera (which is 3D/gluLookAt-based), CCCamera2D operates in 2D space and applies its transform by modifying a target node's position and scale. Usage: var camera = new CCCamera2D(viewportWidth, viewportHeight); camera.Follow(playerNode, smoothing: 0.1f); // In update loop: camera.Update(dt); camera.ApplyTo(gameLayer);

Inheritance: System.Object

Constructors

CCCamera2D()

CCCamera2D()()

Creates a new 2D camera with default viewport size (set later via ViewportWidth/Height).

Example

CCCamera2D(float, float)

CCCamera2D(float, float)(System.Single viewportWidth, System.Single viewportHeight)

Creates a new 2D camera.

Parameters:
viewportWidth (System.Single) - The width of the visible area.
viewportHeight (System.Single) - The height of the visible area.
Example

Properties

AutoScrollSpeedX(System.Single)

Speed for auto-scrolling (units per second). Set to 0 to disable.

AutoScrollSpeedY(System.Single)

Speed for auto-scrolling (units per second). Set to 0 to disable.

FollowSmoothing(System.Single)

Smoothing factor for follow mode (0 = instant, 1 = very slow). Typical value: 0.1.

FollowTarget(Cocos2D.CCNode)

The node the camera is following, if any.

IsShaking(System.Boolean)

Whether the camera is currently shaking.

ScrollX(System.Single)

The camera's X scroll position in world coordinates.

ScrollY(System.Single)

The camera's Y scroll position in world coordinates.

ShakeOffset(Cocos2D.CCPoint)

The current shake offset applied to the camera position.

ViewportHeight(System.Single)

The viewport height used for culling calculations.

ViewportWidth(System.Single)

The viewport width used for culling calculations.

Zoom(System.Single)

Zoom level. 1.0 = normal, 2.0 = 2x zoom in, 0.5 = zoom out.

Methods

ApplyTo(CCNode)

ApplyTo(CCNode)(Cocos2D.CCNode layer)

Applies the camera transform to a target node (typically a CCLayer). Sets the node's position to offset all children by the camera's scroll + shake.

Parameters:
layer (Cocos2D.CCNode)
Example

Follow(CCNode, float)

Follow(CCNode, float)(Cocos2D.CCNode target, System.Single smoothing)

Sets the camera to follow a target node.

Parameters:
target (Cocos2D.CCNode) - The node to follow.
smoothing (System.Single) - Smoothing factor (0 = instant snap, higher = smoother/slower).
Example

IsVisible(CCRect)

System.Boolean IsVisible(CCRect)(Cocos2D.CCRect worldBounds)

Checks whether a world-space bounding rectangle is visible within the camera's viewport. Useful for culling off-screen entities.

Parameters:
worldBounds (Cocos2D.CCRect)
Returns:
System.Boolean
Example

Shake(float, float)

Shake(float, float)(System.Single intensity, System.Single duration)

Triggers a screen shake effect.

Parameters:
intensity (System.Single) - Maximum pixel offset of the shake.
duration (System.Single) - Duration in seconds.
Example

Unfollow()

Unfollow()()

Stops following the current target.

Example

Update(float)

Update(float)(System.Single dt)

Updates camera state. Call this once per frame.

Parameters:
dt (System.Single)
Example