Button

The main form of input for Pixel Vision 8 is the controller's buttons. You can get the current state of any button by calling the Button() method and supplying a button ID. There are optional parameters for specifying an InputState and the controller ID. When called, the Button() method returns a bool for the requested button and its state.

The InputStateenum contains options for testing the Downand Releasedstates of the supplied button ID. By default, Downis automatically used which returns truewhen the key was pressed in the current frame. When using Released, the method returns trueif the key is currently up but was down in the last frame.

Usage

Button ( button, state, controllerID )

Arguments

Name

Value

Description

button

Buttons

Accepts the Buttons enum or int for the button's ID.

state

InputState

Optional InputState enum. Returns down state by default.

controllerID

int

An optional int representing a controller ID. Player 1 is 0 and player 2 is 1. leaving this empty will deafult to player 1.

Returns

Value

Description

bool

Returns a bool based on the state of the button.

Button Enums

Each controller has 8 buttons. A button has a specific ID or you can reference it by the button’s enum name.

Enum

Value

Buttons.Up

0

Buttons.Down

1

Buttons.Left

2

Buttons.Right

3

Buttons.A

4

Buttons.B

5

Buttons.Select

6

Buttons.Start

7

Input State Enums

There are two input states you can use to test a button’s current state:

Enum

Value

InputState.Down

0

InputState.Released

1

Example

In this example, we will loop through all of the buttons on controller 1 and display their names on the screen:

Lua
C#
Lua
-- This array will store any buttons pressed during the current frame
local pressedButtons = {}
-- A list of all the buttons to check on each frame
local buttons = {
Buttons.Up,
Buttons.Down,
Buttons.Left,
Buttons.Right,
Buttons.A,
Buttons.B,
Buttons.Select,
Buttons.Start
}
function Update(timeDelta)
-- Clear the pressedButtons array on each frame
pressedButtons = {}
-- Loop through all the buttons
for i = 1, #buttons do
-- Test if player 1's current button is down and save it to the pressedButtons array
if(Button(buttons[i], InputState.Down, 0)) then
table.insert(pressedButtons, tostring(buttons[i]))
end
end
end
function Draw()
-- Clear the display
Clear()
-- Convert the pressedButtons into a string and draw to the display
local message = table.concat(pressedButtons, ", "):upper()
DrawText("Buttons Down:", 8, 8, DrawMode.Sprite, "large", 15)
DrawText(message:sub(0, #message), 8, 16, DrawMode.Sprite, "medium", 14, - 4)
end
C#
class ButtonExample : GameChip
{
// This array will store any buttons pressed during the current frame
private List<string> pressedButtons = new List<string>();
// A list of all the buttons to check on each frame
private Buttons[] buttons =
{
Buttons.Up,
Buttons.Down,
Buttons.Left,
Buttons.Right,
Buttons.A,
Buttons.B,
Buttons.Select,
Buttons.Start
};
public override void Update(int timeDelta)
{
// Clear the pressedButtons array on each frame
pressedButtons.Clear();
// Loop through all the buttons
for (int i = 0; i < buttons.Length; i++)
{
// Test if player 1's current button is down and save it to the pressedButtons array
if(Button(buttons[i], InputState.Down, 0))
{
pressedButtons.Add(buttons[i].ToString());
}
}
}
public override void Draw()
{
// Clear the display
Clear();
// Convert the pressedButtons into a string and draw to the display
var message = string.Concat(pressedButtons.ToArray()).ToUpper();
DrawText("Buttons Down:", 8, 8, DrawMode.Sprite, "large", 15);
DrawText(message.Substring(0, message.Length), 8, 16, DrawMode.Sprite, "medium", 14, - 4);
}
}

Running this code will output the following:

image alt text