Skip to main content

FlixelTouchManager

View source

class

org.flixelgdx.input.touch.FlixelTouchManager

public class FlixelTouchManager implements FlixelInputProcessorManager

Multitouch input manager backed by libGDX's InputProcessor callbacks.

Access via Flixel.touches after the framework is initialized. The manager tracks up to FlixelTouchManager.getMaxPointers() simultaneous fingers in the FlixelTouchManager.list array. Each slot is a reused FlixelTouch instance; slot 0 always corresponds to pointer index 0 (the first finger), slot 1 to index 1, and so on.

// Check the first finger each frame.
if (Flixel.touches.list[0].isJustPressed()) {
spawnEffect(Flixel.touches.list[0].worldX, Flixel.touches.list[0].worldY);
}

// React to any active touch.
if (Flixel.touches.anyTouched()) {
player.move(Flixel.touches.list[0].worldX, Flixel.touches.list[0].worldY);
}

// Detect a tap on a sprite using world coordinates.
if (Flixel.touches.justTouchedWorld(
mySprite.getX(), mySprite.getY(),
mySprite.getWidth(), mySprite.getHeight())) {
onButtonTapped();
}

// Track whether a finger is currently held inside a screen region.
if (Flixel.touches.touchingScreen(0, 0, halfWidth, screenHeight)) {
moveLeft();
}

Coordinate systems

Each FlixelTouch carries both screen and world coordinates. Screen coordinates use libGDX's top-left origin (Y increases downward). World coordinates are unprojected via the manager's active camera and use the standard bottom-left origin (Y increases upward), matching the rest of the scene. Set a custom camera with FlixelTouchManager.setWorldCamera(FlixelCamera); otherwise the first camera in Flixel.cameras is used.

Frame contract

The InputProcessor is the authoritative source for justPressed, justReleased, and justCancelled flags; they are set inside the callbacks and cleared by FlixelTouchManager.endFrame(). FlixelTouchManager.update() refreshes screen and world coordinates each frame for all active pointers.

Max pointer count

The default limit is 10, which covers the maximum simultaneous touches supported by virtually all Android hardware. Call FlixelTouchManager.setMaxPointers(int) to raise or lower the limit; existing live touch state is preserved for pointers that fall within the new size.

Constructors

FlixelTouchManager()

public FlixelTouchManager()

FlixelTouchManager(int)

public FlixelTouchManager(int maxPointers)

Fields

DEFAULT_MAX_POINTERS

public static final int DEFAULT_MAX_POINTERS = 10

Default maximum number of simultaneous touch pointers tracked.


list

public FlixelTouch[] list

Per-pointer state array. Read FlixelTouch instances directly for zero-overhead access. Do not reassign this field from outside the manager; use FlixelTouchManager.setMaxPointers(int) instead.

Every slot is always a non-null FlixelTouch. Check FlixelTouch.pressed() to determine whether a slot represents an active finger.


enabled

public boolean enabled

When false, all queries return inactive state.

Note that this is typically false by default, although mobile backends will automatically enable the touch manager when a game is compiled for mobile platforms.


Methods

setMaxPointers(int)

public void setMaxPointers(int n)

Resizes the pointer limit and reallocates FlixelTouchManager.list. Existing FlixelTouch objects are preserved for indices that fall within the new size; new slots beyond the old size receive fresh instances. Reducing the limit discards state for pointers above the new ceiling.

Parameters:

NameDescription
nNew maximum pointer count (must be positive).

getMaxPointers()

public int getMaxPointers()

Returns the current maximum number of tracked pointers.


getInputProcessor()

public InputProcessor getInputProcessor()

Returns the stable InputProcessor that must be registered on the InputMultiplexer for the lifetime of the game session. Registered automatically by FlixelGame.create().


update()

public void update()

Refreshes screen and world coordinates for all active pointers. Called once per frame by FlixelGame.update() before game logic runs.


endFrame()

public void endFrame()

Clears per-frame edge flags (FlixelTouch.justPressed(), FlixelTouch.justReleased(), FlixelTouch.justCancelled()) for all pointers. Called once per frame by FlixelGame.render() after game logic and drawing finish.


reset()

public void reset()

Resets all pointer state to inactive without changing the pointer limit or camera.


setWorldCamera(FlixelCamera)

public void setWorldCamera(FlixelCamera camera)

Sets the camera used to unproject screen coordinates to world coordinates in FlixelTouchManager.update(). Pass null to fall back to the first camera in Flixel.cameras.

Parameters:

NameDescription
cameraCamera to use for unprojection, or null for the default.

getWorldCamera()

public FlixelCamera getWorldCamera()

Returns the camera used for world coordinate unprojection, or null if using default.


pressed(int)

public boolean pressed(int pointer)

Returns true while the given pointer index is currently in contact with the screen.

Parameters:

NameDescription
pointerZero-based pointer index.

Returns: true if the pointer is pressed and input is enabled.


justPressed(int)

public boolean justPressed(int pointer)

Returns true on the single frame the given pointer first touched the screen.

Parameters:

NameDescription
pointerZero-based pointer index.

Returns: true if the pointer was just pressed and input is enabled.


justReleased(int)

public boolean justReleased(int pointer)

Returns true on the single frame the given pointer lifted off the screen.

Parameters:

NameDescription
pointerZero-based pointer index.

Returns: true if the pointer was just released and input is enabled.


anyPressed()

public boolean anyPressed()

Returns true if any tracked pointer is currently in contact with the screen.

Returns: true if at least one pointer is pressed and input is enabled.


anyJustPressed()

public boolean anyJustPressed()

Returns true on the single frame any pointer first touched the screen.

Equivalent to checking FlixelTouch.justPressed() across every slot. Useful when you do not need to know which finger triggered the press.

if (Flixel.touches.anyJustPressed()) {
Flixel.haptics.vibrate(30);
}

Returns: true if at least one pointer was just pressed this frame and input is enabled.


anyJustReleased()

public boolean anyJustReleased()

Returns true on the single frame any pointer lifted off the screen.

Equivalent to checking FlixelTouch.justReleased() across every slot. Useful when you only care that some finger was released, not which one.

if (Flixel.touches.anyJustReleased()) {
releaseButtonHighlight();
}

Returns: true if at least one pointer was just released this frame and input is enabled.


count()

public int count()

Returns the number of pointers currently in contact with the screen.

Returns: Active pointer count, or 0 when input is disabled.


touchingWorld(float, float, float, float)

public boolean touchingWorld(float x, float y, float width, float height)

Returns true while any active pointer is inside the given world-space rectangle.

Coordinates use the bottom-left origin (Y increases upward), matching the game world. Use this to track a finger held inside an area. For detecting the moment a finger lands, use FlixelTouchManager.justTouchedWorld(...) instead.

if (Flixel.touches.touchingWorld(button.getX(), button.getY(),
button.getWidth(), button.getHeight())) {
button.setHighlighted(true);
}

Parameters:

NameDescription
xLeft edge of the rectangle in world units.
yBottom edge of the rectangle in world units.
widthWidth of the rectangle in world units.
heightHeight of the rectangle in world units.

Returns: true if at least one pressed pointer is inside the rectangle.


touchingScreen(float, float, float, float)

public boolean touchingScreen(float x, float y, float width, float height)

Returns true while any active pointer is inside the given screen-space rectangle.

Coordinates use the top-left origin (Y increases downward), matching libGDX screen space. Use this to track a finger held inside an area. For detecting the moment a finger lands, use FlixelTouchManager.justTouchedScreen(...) instead.

float half = Gdx.graphics.getWidth() / 2f;
if (Flixel.touches.touchingScreen(half, 0, half, Gdx.graphics.getHeight())) {
moveRight();
}

Parameters:

NameDescription
xLeft edge of the rectangle in screen pixels.
yTop edge of the rectangle in screen pixels.
widthWidth of the rectangle in screen pixels.
heightHeight of the rectangle in screen pixels.

Returns: true if at least one pressed pointer is inside the rectangle.


justTouchedWorld(float, float, float, float)

public boolean justTouchedWorld(float x, float y, float width, float height)

Returns true on the single frame any pointer first touches the given world-space rectangle.

Coordinates use the bottom-left origin (Y increases upward), matching the game world. Clears to false after the frame ends, matching the timing of FlixelTouch.justPressed().

if (Flixel.touches.justTouchedWorld(button.getX(), button.getY(),
button.getWidth(), button.getHeight())) {
onButtonTapped();
}

Parameters:

NameDescription
xLeft edge of the rectangle in world units.
yBottom edge of the rectangle in world units.
widthWidth of the rectangle in world units.
heightHeight of the rectangle in world units.

Returns: true if any pointer was just pressed inside the rectangle this frame.


justTouchedScreen(float, float, float, float)

public boolean justTouchedScreen(float x, float y, float width, float height)

Returns true on the single frame any pointer first touches the given screen-space rectangle.

Coordinates use the top-left origin (Y increases downward), matching libGDX screen space. Clears to false after the frame ends, matching the timing of FlixelTouch.justPressed().

if (Flixel.touches.justTouchedScreen(0, 0, Gdx.graphics.getWidth() / 2f,
Gdx.graphics.getHeight())) {
onLeftSideTapped();
}

Parameters:

NameDescription
xLeft edge of the rectangle in screen pixels.
yTop edge of the rectangle in screen pixels.
widthWidth of the rectangle in screen pixels.
heightHeight of the rectangle in screen pixels.

Returns: true if any pointer was just pressed inside the rectangle this frame.