Coding Help: Trouble Debugging Program


#1

I made a game where you need to dodge the dodgeballs and it says “Cannot read property ‘x’ of undefined” whenever I get hit by an enemy. Any ideas?

var player;
var enemies = [];
var score = 0;
var gameState;
var lives = 3;
var level1 = true, level2 = false, level3 = false, level4 = false, level5 = false;

function setup() {
  createCanvas(600, 450);
  
  // create our new player
  player = new Player();
  
  gameState = "intro";
}

function draw() {
  if(gameState === "intro")
  {
    background(0);
    fill(255);
    textSize(60);
    text("DODGEBALL", 115, 100)
    textSize(42);
    text("Dodge the incoming projectiles", 10, 200);
    text("Press a key to start!", 100, 300);
    textSize(30);
    text("You only have 3 lives so be careful", 60, 250);
  }
    
    if(gameState === "gameover")
  {
    background(0);
    fill(255);
    textSize(50);
    text("Game Over!", 100, 150);
    textSize(42);
    text("Press a key to start again", 75, 200);
  }
 
  if(gameState === "play")
  {
    background(0);
  // display the player
  player.display();
    
    //loop through all enemies
  for(var i = 0; i < enemies.length; i++)
  {
    enemies[i].display();
    
    if(dist(player.x, player.y, enemies[i].x, enemies[i].y) < 20)
    {
     	enemies.splice(i, 1);
      lives = lives - 1;
    }  
    if(enemies[i].x > width)
    {
      enemies.splice(i, 1);
      score++;
    } 
  }

    //levels
  if(level1 = true)
  {  
    level2 = false
    level3 = false
    level4 = false
    level5 = false
  	if(random(0, 100) < 1)
  	{
    enemies.push(new Enemy())
 	 }
  }
  
  if(level2 = true)
  {
    if(random(0, 100 < 2))
    {
      enemies.push(new Enemey())
   		 level1 = false
       level3 = false
   	   level4 = false
    	 level5 = false
    }
  }
    
    
  
       if(lives < 1 )
      {
       gameState = "gameover";
      }
  
  
  //display the score
  fill(255);
  textSize(42);
  text(score, 50, 50);
  text("Lives: " + lives, 300, 50);
}
}

//move and shopt
function keyPressed()
{
 	if(keyCode === RIGHT_ARROW)
  {
   	player.direction = "right" 
  }
  if(keyCode === LEFT_ARROW)
  {
    player.direction = "left"
  }
  if(keyCode === UP_ARROW)
  {
    player.direction = "up"
  }
  if(keyCode === DOWN_ARROW)
  {
    player.direction = "down"
  }
  if(gameState === "intro")
  {
    gameState = "play"
  } 
  if(gameState === "gameover")
  {
    gameState = "intro"
    lives = 3
    player.x = width/2
    player.y = height - 100;
  }
}

function Player()
{
  
  this.x = width/2;
  this.y = height - 100;
  this.direction = "stopped";
  
  this.display = function()
  {
    // make it wrap
    if(this.y > height)
    {
			this.y = 0;
    }
    if(this.y < 0)
    {
      this.y = height;
    }
    
    // direction
    if(this.direction === "up")
    {
      this.y = this.y - 3
    }
    if(this.direction === "down")
    {
      this.y = this.y + 3
    }
    
    // what player looks like
    fill(0, 255, 0);
    rect(this.x, this.y, 20, 20);
  }
}
  
function Enemy()
  {
    this.x = 0;
    this.y = random(0, 300);
    this.speed = random(1, 5);
    
    this.display = function()
    {
     	this.x = this.x + this.speed
      
      fill(255, 0, 0);
      ellipse(this.x, this.y, 20, 20);
    }
  }

#2

Start by editing your post, selecting your code, and hitting the magic format-this-like-code button, which looks like this: </>

Then we will be able to look at your code.


#4

I think i fixed it. Should be good now.