Actions – Building with Blocks


Introduction

Actions are pieces of pre-defined logic that can be added to Actors and Scenes to extend their functionality. There is no particular base class for an action, instead actions conform to a strict framework which consists of the following:

  • A constructor which creates an instance of the action
  • An onInit() method which is called when the action is first executed or when it is executed again after it has finished executing and ran again
  • An onTick() method which is called each time the action is updated

Actions once created are added to an actions list, each action in an actions list is executed consecutively, which means that the next action will not execute until the current action has finished executing. For example:

  • Move object to position 100,100 over 5 seconds
  • Move object to position 0,0 over 2 seconds
  • Play a sound effects

The 2nd action will not execute until the object reaches position 100,100 after 5 seconds. The 3rd action will not execute until the object reaches position 0,0 after a further 2 seconds, so the sound effect will not play until 7 seconds have passed.

Action lists can repeat any number of times or can play forever, you can specify the number of repeats when creating an instance of b5.ActionList.

By building up a library of actions, game functionality can be bolted together by creating lists of those actions and assigning them to game objects.

Actors and Scenes both contain an ActionsListManager which allows the object to run multiple action lists simultaneously

Lets take a quick look at an example that shows how to add some of the pre-defined actions that ship with Booty5 to an actor object to modify its behaviour:

In the above example, we create an actions list called “moveme1”, we add two actions to the actions list that moves the object then we add the actions list to the actors actiosn list manager for processing and set if off playing.

Actions are added to the ActionsList using b5.ActionsList.add(), they can later be removed by calling b5.ActionsList.remove(). Once an ActionsList has been added to the manager you can later find it by calling b5.ActionsList.find(actions_list_name). You can also find an actions list from a path, e.g.:

An ActionsList has a number of methods that can affect its behaviour:

  • b5.ActionsList.play() – Starts the actions list playing or resumes if paused
  • b5.ActionsList.pause() – Pauses playback of the the actions list
  • b5.ActionsList.restart() – Restarts the actions list from the beginning, also resets the number of repeats

Creating Custom Actions

Whilst a large collection of pre-defined actions have been created for you, you can easily create and register actions of your own that fit your specific game.

To create an action you need to create an actions class then optionally register it with the actions register ActionsRegister. An action class consists of a constructor, an onInit() method and an onTick() method:

  • constructor – The constructor creates an instance of the action remembering any passed parameters
  • onInit() – This method sets up the initial state of the action
  • onTick() – This method is called every time the action is executed (every frame)

Each time an action is encountered in the actions list it is initialised, this includes when the actions list repeats. Once initialised the action will call its onTick() method until false is returned from the onTick() method. Note that if no onTick() method is supplied then the action will exit immediately.

Lets take a quick look at an example that shows how to create our own action:

In the above code we declare a new action called A_StopMove which accepts 5 parameters. We store these parameters in the instance of the action because we will need them later.
We have also declared onInit() and onTick() methods. Notice how we return true from onTick() until the action has ran for its duration, at which point we return false to let the actions system know that it should move onto the next action in the list.

Finally, notice how we register the action with the actions register. We pass in the name of the action “StopMove” along with a function that creates an instance of the actions class. Registering your class with the actions register is only required if you want to make the action callable as a custom action from the Booty5 game maker editor.

 

Predefined Actions

Booty5 comes with a large range of pre-defined actions out of the box. A complete list of these actions can be found in the API Reference.

Next Topic

1,739 total views, 1 views today