MovementBehavior2D

Package: Libraries.Interface.Behaviors.Games · Inherits from: Libraries.Language.Object, Libraries.Interface.Behaviors.Behavior

A MovementBehavior2D moves a 2D item, such as a character, with the keyboard. Add it to an item with AddBehavior, and the item moves at once: 1. In the default mode, TOP_DOWN, the arrow keys (or W, A, S, and D) move the item up, down, left, and right, as in a maze or an adventure game seen from above. Holding two keys moves it diagonally. 2. In SIDE_VIEW mode, left and right (or A and D) move the item, and the space bar (or up, or W) jumps, as in a platform game. Gravity pulls the item down, so this mode needs 2D physics and a Responsive item. Every key can be changed, and each command can have more than one key. The behavior works out how to move the item from its physics settings, so the same code works for any item: 1. An item without physics is moved directly. Nothing stops it, so it passes through walls. 2. A Nonresponsive item is given a velocity. It pushes other items, and they don't push it back. 3. A Responsive item is given a velocity. In SIDE_VIEW mode, only its sideways velocity is set, so gravity still pulls it down, and it jumps only when it is standing on something. 4. An Unmovable item can't be moved. The behavior says so once, on the console. To move the item some other way, such as from a computer-controlled player, turn the keyboard off with SetUseKeyboard(false) and set the commands with SetHorizontal, SetVertical, and Jump.

Example Code

use Libraries.Game.Game
use Libraries.Interface.Item2D
use Libraries.Interface.Behaviors.Games.MovementBehavior2D

class Main is Game
    action Main
        StartGame()
    end

    action CreateGame
        LoadScene("Scenes/Scene.qs")

        Item2D player = FindItem2DByName("1: Boy")
        MovementBehavior2D movement
        player:AddBehavior(movement)
        GetCamera2D():Follow(player)
    end
end

Variables

  • integer TOP_DOWN: The item moves up, down, left, and right, as seen from above.
  • integer SIDE_VIEW: The item moves left and right and jumps, as seen from the side.

Actions

AddDownKey(integer key)

This action adds another key that moves the item down in TOP_DOWN mode.

Parameters

  • key integer: The key code.

AddJumpKey(integer key)

This action adds another key that makes the item jump in SIDE_VIEW mode.

Parameters

  • key integer: The key code.

AddLeftKey(integer key)

This action adds another key that moves the item left.

Parameters

  • key integer: The key code.

AddRightKey(integer key)

This action adds another key that moves the item right.

Parameters

  • key integer: The key code.

AddUpKey(integer key)

This action adds another key that moves the item up in TOP_DOWN mode.

Parameters

  • key integer: The key code.

Dispose()

This action is called when the behavior is removed from an item. It stops an item with physics from moving on its own.

GetAcceleration()

This action returns how quickly the item reaches full speed.

Returns

number: The acceleration.

GetHorizontal()

This action returns the horizontal command, from -1 for full speed left to 1 for full speed right.

Returns

number: The horizontal command.

GetItem()

This action returns the item this behavior moves.

Returns

Libraries.Interface.Item2D: The item, or undefined if none has been set.

GetJumpHeight()

This action returns how high the item jumps in SIDE_VIEW mode.

Returns

number: The jump height.

GetMode()

This action returns how the keys move the item: TOP_DOWN or SIDE_VIEW.

Returns

integer: The mode.

GetSpeed()

This action returns the top speed, in pixels per second.

Returns

number: The speed.

GetVertical()

This action returns the vertical command, from -1 for full speed down to 1 for full speed up.

Returns

number: The vertical command.

IsActive()

This action returns whether the behavior moves the item.

Returns

boolean: true if the behavior is active.

IsFacingMovement()

This action returns whether the item is flipped to face the way it moves.

Returns

boolean: true if the item faces the way it moves.

IsFinished()

This action returns whether the behavior is finished. A behavior added with AddBehavior keeps running until Stop is called.

Returns

boolean: true if Stop has been called, and false otherwise.

IsOnGround()

This action returns whether the item is standing on something: whether it is touching another item whose top is at, or just under, its bottom. An item without physics is never on the ground.

Returns

boolean: true if the item is standing on something, and false otherwise.

IsUsingKeyboard()

This action returns whether the commands come from the keyboard.

Returns

boolean: true if the keyboard is used.

Jump()

This action asks the item to jump on the next frame, in SIDE_VIEW mode. It jumps only if it can: see SetJumpHeight.

Run(Libraries.Interface.Events.BehaviorEvent event)

This action is called when the behavior is added to an item with AddBehavior. It remembers the item, so the behavior knows what to move. Most users never need to call it.

Parameters

SetAcceleration(number acceleration)

This action sets how quickly the item speeds up and slows down, as the share of full speed it can gain or lose each second. For example, 4 reaches full speed in a quarter of a second. The default is 0, which changes speed at once.

Parameters

  • acceleration number: The acceleration.

SetActive(boolean active)

This action sets whether the behavior moves the item. While it is not active, the commands are 0 and the item stops, but the behavior stays on the item and can be made active again.

Parameters

  • active boolean: true to move the item, or false to stop.

SetDownKey(integer key)

This action sets the one key that moves the item down in TOP_DOWN mode, in place of any others.

Parameters

  • key integer: The key code.

SetFaceMovement(boolean face)

This action sets whether the item is flipped left and right to face the way it moves. The default is false. Turn it on for a character whose picture faces right.

Parameters

  • face boolean: true to flip the item to face the way it moves.

SetHorizontal(number value)

This action sets the horizontal command, from -1 for full speed left to 1 for full speed right. While the keyboard is turned on, the keyboard replaces it every frame, so turn it off first with SetUseKeyboard(false).

Parameters

  • value number: The horizontal command.

SetItem(Libraries.Interface.Item2D item)

This action sets the item this behavior moves. AddBehavior sets it automatically, so most users never need to call it.

Parameters

SetJumpHeight(number height)

This action sets how high the item jumps in SIDE_VIEW mode, in the same units as the item's position, usually pixels. The default is 96, three 32-pixel tiles. A height of 0 turns jumping off. Only a Responsive item with physics can jump, and only while it is standing on something, and gravity must be pulling down.

Parameters

  • height number: The jump height.

SetJumpKey(integer key)

This action sets the one key that makes the item jump in SIDE_VIEW mode, in place of any others.

Parameters

  • key integer: The key code.

SetLeftKey(integer key)

This action sets the one key that moves the item left, in place of any others.

Parameters

  • key integer: The key code.

SetMode(integer mode)

This action sets how the keys move the item. TOP_DOWN, the default, moves up, down, left, and right. SIDE_VIEW moves left and right and jumps, and needs 2D physics, gravity, and a Responsive item.

Parameters

  • mode integer: The mode, such as movement:SIDE_VIEW.

Example

use Libraries.Interface.Behaviors.Games.MovementBehavior2D

MovementBehavior2D movement
movement:SetMode(movement:SIDE_VIEW)

SetRightKey(integer key)

This action sets the one key that moves the item right, in place of any others.

Parameters

  • key integer: The key code.

SetSpeed(number speed)

This action sets the top speed, in pixels per second. The default is 200.

Parameters

  • speed number: The speed.

SetUpKey(integer key)

This action sets the one key that moves the item up in TOP_DOWN mode, in place of any others. Use AddUpKey to add a key instead.

Parameters

  • key integer: The key code, such as keys:UP from a KeyboardEvent.

SetUseKeyboard(boolean useKeyboard)

This action sets whether the commands come from the keyboard. Turn it off to move the item from code, with SetHorizontal, SetVertical, and Jump.

Parameters

  • useKeyboard boolean: true to use the keyboard.

SetVertical(number value)

This action sets the vertical command, from -1 for full speed down to 1 for full speed up. It is used only in TOP_DOWN mode. While the keyboard is turned on, the keyboard replaces it every frame, so turn it off first with SetUseKeyboard(false).

Parameters

  • value number: The vertical command.

Stop()

This action stops the behavior. If it was added to an item with AddBehavior, the item removes it at the start of the next frame, and an item with physics stops moving on its own.

Update(number seconds)

This action is called once per frame when the behavior has been added to an item with AddBehavior, before the physics runs. It reads the keyboard, if the keyboard is turned on, and then moves the item.

Parameters

  • seconds number: The time since the last frame, in seconds.

Inherited Actions

4 actions from Libraries.Interface.Behaviors.Behavior
3 actions from Libraries.Language.Object