Type Error Message, Not sure where the Error is

Hey, I am having trouble having getting my button to move forward to the next screen. It keeps giving me an error message saying "Type Error: Cannot Read Properties of Undefined (reading ‘mouse’).

https://replit.com/@starvlightt/Game-Jam-Project-Gita-S

This is the problem area.:

// Checking Button Function
if (nextButton.mouse.presses()){
  print("pressed");
  background("paleturquoise");
  text("Welcome to screen 1"); 
}

This is my whole code:

//Move the catcher with the left and right arrow keys to catch the falling objects. 

/* VARIABLES */
let catcher, fallingObject;
let cHeight = 400
let score = 0;
let player;
let nextButton;


/* PRELOAD LOADS FILES */
function preload(){
  
}


/* SETUP RUNS ONCE */
function setup() {
  createCanvas(400,cHeight);
  player = new Sprite(200, 380, 100, 20, "kinematic");
  player.color = "blue";
  nextButton = new Sprite(335, 375, 100, 20, "kinematic");
}

// Screen 2 Maze Scene 
function draw() {
  background(224,224,224);
  nextButton.color = "orange";
  nextButton.text = "Done";
}

  //If keys are pressed the sprite moves according to the keys
function keyPressed () {
    if (keyCode === LEFT_ARROW) {
    player.vel.x = -3;
  } else if (keyCode === RIGHT_ARROW) {
    player.vel.x = 3;
  } else if (keyCode === UP_ARROW) {
    player.vel.y = -3;
  } else if (keyCode === DOWN_ARROW) {
    player.vel.y = 3;
  } else {
    player.vel.x = 0;
    player.vel.y = 0;
  }
}


// Motion st
function keyReleased () {
  player.vel.x = 0;
  player.vel.y = 0;
}


// Checking Button Function
if (nextButton.mouse.presses()){
  print("pressed");
  background("paleturquoise");
  text("Welcome to screen 1"); 
}

rename that code to if (nextButton.mouse.pressed()){ since you probably named the function incorrectly

2 Likes

Thank you for the input. I tried that and the error still continues.

I’m not sure if all the code that you shared is actually in the same file. But if it is all in the same file, then the issue is that the top-level if statements are being executed right when the file is loaded/run. This means those if statements are being executed before the functions are executed. As such the setup() function hasn’t initialized the nextButton variable yet.

When the file is loaded all the code is run, this means the functions are created and the if statements are executed. You don’t have any code that actually calls the functions, so they are just loaded and not executed. You can put a call to setup() right before your if statement and that should help you get past this issue.

3 Likes