Mouse

Mouse Functions

There are two ways you can get information from the user's mouse: Event Listeners and Direct Polling. With an Event Listener, you write a function that gets called automatically when the user uses their mouse. With Direct Polling, you can ask about the mouse whenever you want, which you would typically do during each frame in your update world function.You can also control the mouse pointer - showing and hiding it.

- Mouse Direct Polling

getMousePosition

Returns the current position of the mouse.

Returns

array an array that contains the x and y coordinates of the mouse

let [x, y] = getMousePosition()

getMouseButton

Returns whether a given mouse button is pressed or not.

name
type
default
description

button*

number

-

a number indicating which button to check (1 for left, 2 for middle, 3 for right) (use 1 on a Mac with one button)

Returns

boolean true means that button is currently pressed and false means it is not

if (getMouseButton(1)) {
  fireRockets = true
}

- Mouse Event Listeners

onMousePress

Registers your function that listens for mouse pressed events.

name
type
description

listenerFunction*

function

the listener function that you wrote.

Your listener function must take four arguments:

  • the world

  • the x coordinate where the mouse was when the button was pressed

  • the y coordinate where the mouse was when the button was pressed

  • a number indicating which button was pressed (1 for left, 2 for middle, 3 for right) (use 1 for a Mac with one button)

Returns

nothing

function myMousePressedFunction(world, mouseX, mouseY, button) {
  if (button == 1 && mouseX < 100 && mouseY < 100) {
    world.myball.xspeed = 0
  }
}

function startWorld(world) {
  onMousePress(myMousePressedFunction)
}

onMouseRelease

Registers your function that listens for mouse pressed events.

name
type
description

listenerFunction*

function

the listener function that you wrote. Your listener function must take four arguments:

  • the world

  • the x coordinate where the mouse was when the button was pressed

  • the y coordinate where the mouse was when the button was pressed

  • a number indicating which button was pressed (1 for left, 2 for middle, 3 for right) (use 1 for a Mac with one button)

Returns

nothing

onWheelForward

Registers your function that listens for mouse pressed events.

name
type
description

listenerFunction*

function

the listener function that you wrote. Your listener function must take three arguments:

  • the world

  • the x coordinate where the mouse was when the button was released

  • the y coordinate where the mouse was when the button was released

Returns

nothing

onWheelBackward

Registers your function that listens for mouse pressed events.

name
type
description

listenerFunction*

function

the listener function that you wrote. Your listener function must take three arguments:

  • the world

  • the x coordinate where the mouse was when the wheel was rotated

  • the y coordinate where the mouse was when the wheel was rotated

Returns

nothing

onMouseMotion

Registers your function that listens for mouse pressed events.

name
type
description

listenerFunction*

function

the listener function that you wrote. Your listener function must take eight arguments:

  • the world

  • the x coordinate that the mouse moved to

  • the y coordinate that the mouse moved to

  • the amount by which the x coordinate changed during this move (may be positive or negative)

  • the amount by which the y coordinate changed during this move (may be positive or negative)

  • a boolean for mouse button 1 (true if it was pressed, and false if it was not)

  • a boolean for mouse button 2 (true if it was pressed, and false if it was not)

  • a boolean for mouse button 3 (true if it was pressed, and false if it was not)

Returns

nothing

- Mouse Control

hideMouse

Makes the mouse cursor invisible.

Returns

nothing

hideMouse()

showMouse

Makes the mouse cursor visible - this is only needed if you have previously called hideMouse().

Returns

nothing

showMouse()

Last updated