# Debugging Game P5js

**URL:** <https://discourse.processing.org/t/debugging-game-p5js/5966>\
**Category:** Coding Questions\
**Created:** [November 26, 2018, 6:26pm UTC](https://discourse.processing.org/t/debugging-game-p5js/5966 "2018-11-26T18:26:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shoneyjosh](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@shoneyjosh](https://discourse.processing.org/u/shoneyjosh)\
**Post date:** [November 26, 2018, 6:26pm UTC](https://discourse.processing.org/t/debugging-game-p5js/5966/1 "2018-11-26T18:26:20Z")

</div>

It is a game where you avoid the incoming enemies.  
Uncaught TypeError: Cannot read property ‘x’ of undefined (sketch: line 62)  
whenever the enemy hits the player

```auto
//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++;
    } 
  }

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);
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [November 26, 2018, 8:17pm UTC](https://discourse.processing.org/t/debugging-game-p5js/5966/2 "2018-11-26T20:17:35Z")

</div>

You’re removing an element from the array using the `splice()` function, but then you’re using the index you just removed in the very next `if` statement.
