Mouse Input

TCJSgame v3 includes support for mouse input, allowing you to build games that react to clicks or track the cursor position. This is useful for puzzle games, drawing tools, or point-and-click adventures. The engine tracks display.mouseX and display.mouseY automatically.

You can also check display.mouseDown to see if the mouse button is currently pressed. This makes it easy to implement drag-and-drop mechanics or shooting actions triggered by clicks.

Syntax

if (display.mouseDown) {
  console.log(display.mouseX, display.mouseY);
}

Example

const ball = new Component(20, 20, "green", 100, 100, "rect");
display.add(ball);

function update() {
  if (display.mouseDown) {
    ball.x = display.mouseX;
    ball.y = display.mouseY;
  }
}

Notes