Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public class Actor

  1. Object
  2. Actor

A player or an enemy: a Body driven by an ordered list of Abilitys.

Each PlatformWorld#step the actor takes its tick of Input bits (from a player, a replay or an AIBrain), runs its abilities, applies gravity unless an ability turned it off, moves the body, and settles a MovementState that the renderer animates. All tuning is in fixed point (Fx), in pixels and ticks at 60 ticks per second.

import com.codename1.gaming.platformer.*;

LevelData level = LevelData.parse("{\"tileSize\":16,\"spawn\":[1,1],\"tiles\":[\"........\",\"........\",\"########\"]}");
PlatformWorld world = level.createWorld(1);
Actor hero = new Actor("hero", Fx.of(32), Fx.of(80), Fx.of(12), Fx.of(14));
hero.addAbility(new MoveAbility());
hero.addAbility(new LadderAbility());
hero.addAbility(new WallJumpAbility());
hero.addAbility(new JumpAbility());
hero.addAbility(new CrouchAbility());
hero.addAbility(new DashAbility());
world.addActor(hero);

Constructors

public Actor(String id, int x, int y, int w, int h)Creates an actor.

Methods

public Actor addAbility(Ability a)Adds an ability at the end of the processing order.
public <T extends Ability> T getAbility(Class<T> type)Finds an ability by class, to tune it or switch it on.
public void tick(PlatformWorld world, int bits)Advances one tick.
public void claimState(MovementState s)Sets this tick’s state.
public void forceState(MovementState s)Replaces this tick’s state even if one was claimed, for states that override everything such as being hurt.
public String getKind()The art kind the view draws this actor with ("hero", "slime").
public Actor setKind(String kind)Sets the art kind.
public int getTeam()The team: 0 for the player’s side.
public Actor setTeam(int team)Sets the team.
public HitPoints getHealth()Hit points, or null for an actor that dies at the first hit.
public Actor setHealth(HitPoints health)Gives the actor hit points.
public int getContactDamage()Damage dealt to actors of another team on contact.
public Actor setContactDamage(int damage)Sets contact damage.
public boolean isStompable()Whether landing on this actor’s head hurts it and bounces the player.
public Actor setStompable(boolean stompable)Sets whether the actor can be stomped.
public Actor setDefeatChannel(int channel)Sets a switch channel the actor’s defeat turns on, so beating a boss can open the way out.
public int getDefeatChannel()The switch channel the actor’s defeat turns on.
public AIBrain getBrain()The brain that drives a non-player actor, or null.
public Actor setBrain(AIBrain brain)Gives the actor a brain; the world then ignores its input slot.
public String getId()The actor’s id.
public Body getBody()The collision box.
public int getStandHeight()The height the actor stands at, which crouching shrinks from.
public int getInput()This tick’s input bits.
public boolean held(int button)Whether a button is held this tick.
public boolean pressed(int button)Whether a button went down this tick.
public boolean released(int button)Whether a button was released this tick.
public int getFacing()Facing: 1 right, -1 left.
public void setFacing(int facing)Sets the facing.
public MovementState getState()The state settled in the last tick.
public boolean isStateClaimed()Whether an ability already claimed this tick’s state.
public void suspendGravity()Turns gravity off for this tick (a ladder, a dash, a jetpack).
public int getGravity()Gravity, fixed point per tick squared.
public void setGravity(int gravity)Sets gravity.
public int getMaxFall()Terminal falling speed.
public void setMaxFall(int maxFall)Sets the terminal falling speed.
public void lockControl(int ticks)Locks horizontal control for a number of ticks, so a wall jump or a knock-back carries the actor away before input steers again.
public boolean isControlLocked()Whether horizontal control is locked.
public int getAirTicks()Ticks since the actor last stood on something.
public void consumeJump()Marks this tick’s jump press as used, so later abilities do not act on it.
public boolean isJumpConsumed()Whether an earlier ability used this tick’s jump press.
public boolean isAlive()Whether the actor is alive.
public void kill()Kills the actor; it stops processing until respawn.
public void respawn(int x, int y)Brings the actor back at a point, standing and still.
public long hash(long h)The actor’s state as a hash, for replay verification.

Inherited methods

Constructor details

Actor

public Actor(String id, int x, int y, int w, int h)
Creates an actor.

Parameters

id String
a name, unique in its world
x int
left, fixed point
y int
top, fixed point
w int
width, fixed point
h int
standing height, fixed point

Method details

addAbility

public Actor addAbility(Ability a)
Adds an ability at the end of the processing order.

Parameters

a Ability
the ability

Returns

this actor, for chaining

getAbility

public <T extends Ability> T getAbility(Class<T> type)
Finds an ability by class, to tune it or switch it on.

Parameters

type Class<T>
the ability’s class

Returns

the first ability of that class or a subclass of it, or null

tick

public void tick(PlatformWorld world, int bits)
Advances one tick. Called by PlatformWorld#step.

Parameters

world PlatformWorld
the world
bits int
this tick’s Input

claimState

public void claimState(MovementState s)
Sets this tick’s state. The first ability to claim one wins, so the order of abilities is also their priority.

Parameters

s MovementState
the state

forceState

public void forceState(MovementState s)
Replaces this tick’s state even if one was claimed, for states that override everything such as being hurt.

Parameters

s MovementState
the state

getKind

public String getKind()
The art kind the view draws this actor with ("hero", "slime").

Returns

the kind

setKind

public Actor setKind(String kind)
Sets the art kind.

Parameters

kind String
the kind

Returns

this actor

getTeam

public int getTeam()
The team: 0 for the player’s side. Projectiles and contact damage never hurt their own team.

Returns

the team

setTeam

public Actor setTeam(int team)
Sets the team.

Parameters

team int
the team

Returns

this actor

getHealth

public HitPoints getHealth()
Hit points, or null for an actor that dies at the first hit.

Returns

the health

setHealth

public Actor setHealth(HitPoints health)
Gives the actor hit points.

Parameters

health HitPoints
the health, or null

Returns

this actor

getContactDamage

public int getContactDamage()
Damage dealt to actors of another team on contact.

Returns

the damage, 0 for harmless

setContactDamage

public Actor setContactDamage(int damage)
Sets contact damage.

Parameters

damage int
the damage

Returns

this actor

isStompable

public boolean isStompable()
Whether landing on this actor’s head hurts it and bounces the player.

Returns

true if stompable

setStompable

public Actor setStompable(boolean stompable)
Sets whether the actor can be stomped.

Parameters

stompable boolean
true if stompable

Returns

this actor

setDefeatChannel

public Actor setDefeatChannel(int channel)
Sets a switch channel the actor’s defeat turns on, so beating a boss can open the way out. 0, the default, turns nothing on.

Parameters

channel int
the channel

Returns

this actor

getDefeatChannel

public int getDefeatChannel()
The switch channel the actor’s defeat turns on.

Returns

the channel, 0 for none

getBrain

public AIBrain getBrain()
The brain that drives a non-player actor, or null.

Returns

the brain

setBrain

public Actor setBrain(AIBrain brain)
Gives the actor a brain; the world then ignores its input slot.

Parameters

brain AIBrain
the brain, or null

Returns

this actor

getId

public String getId()
The actor’s id.

Returns

the id

getBody

public Body getBody()
The collision box.

Returns

the body

getStandHeight

public int getStandHeight()
The height the actor stands at, which crouching shrinks from.

Returns

the standing height, fixed point

getInput

public int getInput()
This tick’s input bits.

Returns

the input

held

public boolean held(int button)
Whether a button is held this tick.

Parameters

button int
an Input constant

Returns

true while held

pressed

public boolean pressed(int button)
Whether a button went down this tick.

Parameters

button int
an Input constant

Returns

true on the press edge

released

public boolean released(int button)
Whether a button was released this tick.

Parameters

button int
an Input constant

Returns

true on the release edge

getFacing

public int getFacing()
Facing: 1 right, -1 left.

Returns

the facing

setFacing

public void setFacing(int facing)
Sets the facing.

Parameters

facing int
1 right, -1 left

getState

public MovementState getState()
The state settled in the last tick.

Returns

the state

isStateClaimed

public boolean isStateClaimed()
Whether an ability already claimed this tick’s state.

Returns

true once claimed

suspendGravity

public void suspendGravity()
Turns gravity off for this tick (a ladder, a dash, a jetpack).

getGravity

public int getGravity()
Gravity, fixed point per tick squared.

Returns

the gravity

setGravity

public void setGravity(int gravity)
Sets gravity.

Parameters

gravity int
fixed point per tick squared

getMaxFall

public int getMaxFall()
Terminal falling speed. Keep it under a tile per tick.

Returns

the speed, fixed point per tick

setMaxFall

public void setMaxFall(int maxFall)
Sets the terminal falling speed.

Parameters

maxFall int
fixed point per tick

lockControl

public void lockControl(int ticks)
Locks horizontal control for a number of ticks, so a wall jump or a knock-back carries the actor away before input steers again.

Parameters

ticks int
how long input is ignored by MoveAbility

isControlLocked

public boolean isControlLocked()
Whether horizontal control is locked.

Returns

true while locked

getAirTicks

public int getAirTicks()
Ticks since the actor last stood on something.

Returns

0 while grounded

consumeJump

public void consumeJump()
Marks this tick’s jump press as used, so later abilities do not act on it.

isJumpConsumed

public boolean isJumpConsumed()
Whether an earlier ability used this tick’s jump press.

Returns

true once consumed

isAlive

public boolean isAlive()
Whether the actor is alive.

Returns

false after kill

kill

public void kill()
Kills the actor; it stops processing until respawn.

respawn

public void respawn(int x, int y)
Brings the actor back at a point, standing and still.

Parameters

x int
left, fixed point
y int
top, fixed point

hash

public long hash(long h)
The actor’s state as a hash, for replay verification.

Parameters

h long
the running hash

Returns

the updated hash