ct.inputs, ct.actions
ct.inputs, ct.actions
This module (ct.inputs
) allows you to manipulate Actions. You can create, modify, or delete new actions during the game.
ct.actions
stores existing actions. If you have created an action Move
, then it will be available at ct.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:
/**
* Move the copy around.
* See "Project" > "Actions and input methods"
* and "Actions" in the docs.
*/
this.hspeed = 8 * ct.actions.MoveX.value; // Move by X axis
this.vspeed = 8 * ct.actions.MoveY.value; // Move by Y axis
if (ct.actions.Shoot.pressed) {
ct.templates.copy('Bullet', this.x, this.y);
}
###
Move the copy around.
See "Project" > "Actions and input methods"
and "Actions" in the docs.
###
@hspeed = 8 * ct.actions.MoveX.value # Move by X axis
@vspeed = 8 * ct.actions.MoveY.value # Move by Y axis
if ct.actions.Shoot.pressed
ct.templates.copy 'Bullet', @x, @y
Actions' methods and properties
Number
ctAction.value ⇒ 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.
Boolean
ctAction.pressed ⇒ Returns whether the action became active in the current frame, either by a button just pressed or by using a scalar input.
Returns: Boolean
– true
for being pressed and false
otherwise
Boolean
ctAction.released ⇒ Returns whether the action became inactive in the current frame, either by releasing all buttons or by resting all scalar inputs.
Returns: Boolean
– true
for being released and false
otherwise
Boolean
ctAction.down ⇒ Returns whether the action is active, e.g. by a pressed button or a currently used scalar input
Returns: Boolean
– true
for being active and false
otherwise
Boolean
ctAction.methodExists(code) ⇒ Checks whether the current action listens to a given input method. This does not check whether this input method is supported by ct.
Returns: Boolean
– true
if it exists, false
otherwise.
Param | Type | Description |
---|---|---|
code | String | The code to look up. |
void
ctAction.addMethod(code, [multiplier]) ⇒ Adds a new input method to listen.
Param | Type | Description |
---|---|---|
code | String | The input method's code to listen to. Must be unique per action. |
[multiplier] | Number | An optional multiplier, e.g. to flip its value. Often used with two buttons to combine them into a scalar input identical to joysticks |
void
ctAction.removeMethod(code) ⇒ Removes the provided input method from an action.
Param | Type | Description |
---|---|---|
code | String | The input method to remove. |
void
ctAction.setMultiplier(code, multiplier) ⇒ 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.
Param | Type | Description |
---|---|---|
code | String | The input method's code to change |
multiplier | Number | The new value |
Number
ctAction.update() ⇒ Recalculates the digital value of an action.
Returns: Number
– A scalar value between -1 and 1.
void
ctAction.reset() ⇒ 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
CtAction
ct.inputs.addAction(name, methods) ⇒ Adds a new action and puts it into ct.actions
.
Param | Type | Description |
---|---|---|
name | String | The name of an action, as it will be used in ct.actions . |
methods | Array<Object> | A list of input methods. This list can be changed later. |
Returns: CtAction
– The created action
Example:
ct.inputs.addAction('Move', [{
code: 'keyboard.ArrowLeft',
multiplier: -1
}, {
code: 'keyboard.ArrowRight'
}, {
code: 'keyboard.KeyA',
multiplier: -1
}, {
code: 'keyboard.KeyD'
}]);
ct.inputs.addAction 'Move', [
{
code: 'keyboard.ArrowLeft'
multiplier: -1
}
{
code: 'keyboard.ArrowRight'
}
{
code: 'keyboard.KeyA'
multiplier: -1
}
{
code: 'keyboard.KeyD'
}
]
void
ct.inputs.removeAction(name, methods) ⇒ Removes an action with a given name.
Param | Type | Description |
---|---|---|
name | String | The name of an action |
Returns: void
Creating new actions without adding them to ct.actions
new CtAction(name)
Creates a new ct action.
Param | Type | Description |
---|---|---|
name | String | The name of the new action. |