Skip to main content

inputs, actions


inputs, actions

This module (inputs) allows you to manipulate Actions. You can create, modify, or delete new actions during the game.

actions stores existing actions. If you have created an action Move, then it will be available at actions.Move. Each of them is an instance of JS class CtAction.

Here is a generic example on how to use actions in your game:

JavaScript
/**
 * Move the copy around.
 * See "Project" > "Actions and input methods"
 * and "Actions" in the docs.
 */
this.hspeed = 8 * actions.MoveX.value; // Move by X axis
this.vspeed = 8 * actions.MoveY.value; // Move by Y axis
if (actions.Shoot.pressed) {
    templates.copy('Bullet', this.x, this.y);
}

Actions' methods and properties

ctAction.value ⇒ Number

A scalar value between -1 and 1. 0 means that there is no input at the current frame, e.g. all the gamepad's thumbsticks are in the resting position or all buttons were released. When used with a keyboard and mouse, actions' values will alternate between 0, 1, and -1 (if multipliers were used). Gamepad thumbsticks and other custom manipulators may produce other values.

ctAction.pressed ⇒ Boolean

Returns whether the action became active in the current frame, either by a button just pressed or by using a scalar input.

Returns: Booleantrue for being pressed and false otherwise

ctAction.released ⇒ Boolean

Returns whether the action became inactive in the current frame,
either by releasing all buttons or by resting all scalar inputs.

Returns: Booleantrue for being released and false otherwise

ctAction.down ⇒ Boolean

Returns whether the action is active, e.g. by a pressed button
or a currently used scalar input

Returns: Booleantrue for being active and false otherwise

ctAction.methodExists(code) ⇒ Boolean

Checks whether the current action listens to a given input method.
This does not check whether this input method is supported by ct.js.

Returns: Booleantrue if it exists, false otherwise.

ParamTypeDescription
codeStringThe code to look up.

ctAction.addMethod(code, [multiplier]) ⇒ void

Adds a new input method to listen.

ParamTypeDescription
codeStringThe input method's code to listen to. Must be unique per action.
[multiplier]NumberAn optional multiplier, e.g. to flip its value. Often used with two buttons to combine them into a scalar input identical to joysticks

ctAction.removeMethod(code) ⇒ void

Removes the provided input method from an action.

ParamTypeDescription
codeStringThe input method to remove.

ctAction.setMultiplier(code, multiplier) ⇒ void

Changes the multiplier for an input method with the provided code.
This method will produce a warning if one is trying to change an input method that is not listened to by this action.

ParamTypeDescription
codeStringThe input method's code to change
multiplierNumberThe new value

ctAction.update() ⇒ Number

Recalculates the digital value of an action.

Returns: Number – A scalar value between -1 and 1.

ctAction.reset() ⇒ void

Resets the state of the action, setting its value to 0 and its pressed, down, released states to false.

Creating and removing new actions programmatically

inputs.addAction(name, methods) ⇒ CtAction

Adds a new action and puts it into actions.

ParamTypeDescription
nameStringThe name of an action, as it will be used in actions.
methodsArray<Object>A list of input methods. This list can be changed later.

Returns: CtAction – The created action

Example:

JavaScript
inputs.addAction('Move', [{
    code: 'keyboard.ArrowLeft',
    multiplier: -1
}, {
    code: 'keyboard.ArrowRight'
}, {
    code: 'keyboard.KeyA',
    multiplier: -1
}, {
    code: 'keyboard.KeyD'
}]);

inputs.removeAction(name, methods) ⇒ void

Removes an action with a given name.

ParamTypeDescription
nameStringThe name of an action

Returns: void