Sprite animation controls

On the previous chapter, we saw how to run animations by simply invoking a method. However, the animation might need to be run on certain conditions, or do some stuff mid-animations etc. MonkeySheet provides some help for that.


MSAction wraps an high level action, for example "run", "idle", "shoot". The sprite is supposed to be doing one and only one MSAction at each time. It is not a control, but it contains some lifecycle methods and hooks.


MSActionMachine inherit from AbstractControl (and therefore runs on the update loop), and contains a set of MSAction. The purpouse of MSActionMachine is to run the appropriate MSAction.


public class HitReceived extends MSAction {

    public HitReceived(MSControl msc) {
        this.msc = msc;

    public void init() {
        //you might want to notify the MSActionMachine that this animation is running...
        //myActionMachine.hitreceiving = true;

        // play the actual animation

    protected void msUpdate(float f) {

    public void finish() {
        //you might want to notify the MSActionMachine that this animation is running...
        //myActionMachine.hitreceiving = false;

On this simple class the animation hit-received is played. After that the animation is finished, so will the MSAction.

public class MyActionMachine extends MSActionMachine {
    protected void init() {
//don't forget to add this control to the Geometry!
//also don't forget to add the MSControl to the Geometry!

                new HitReceived(msc)
                //other MSActions

    protected void msUpdate(float tpf) {
        if (hitReceived) {
            hitReceived = false;

MSAction lifecycle

init: A good place to put the MSAction.play or MSAction.playOnce. Notify the MSActionMachine that the action is started.

msUpdate: useful for mid-animation effects.

if (MonkeySheetAppState.tTPF == 0) {
   if (msc.position == 2) {
      //do something at position 2

whatPlay: the MSAction wraps multiple animations, on this method you can put the logic to choose which animation to run next.

hasEnded: By default, the MSAction is over when the animation is over. However you can override the hasEnded method to change the duration of the MSAction

finish: Notify the MSActionMachine that the action is over

results matching ""

    No results matching ""