# Enemy x keeps equaling nan

**URL:** <https://discourse.processing.org/t/enemy-x-keeps-equaling-nan/18302>\
**Category:** Beginners\
**Created:** [March 2, 2020, 8:41am UTC](https://discourse.processing.org/t/enemy-x-keeps-equaling-nan/18302 "2020-03-02T08:41:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [March 2, 2020, 8:41am UTC](https://discourse.processing.org/t/enemy-x-keeps-equaling-nan/18302/1 "2020-03-02T08:41:03Z")

</div>

I’m trying to create a tower defense game, but atm my enemy keeps vanishing & it’s x keeps showing up nan. Can anyone tell me why?

sketch.js

```auto
var wall = [35];
var bs = 25;
var enemy;

function preload(){
  wallT = loadTable('wall.csv', 'csv');
}
function setup() {
  createCanvas(600, 600);
  for (var i = 0; i < wallT.getColumnCount(); i++){
    var tn = wallT.getNum(0,i);
    wall[i] = new Wall(tn);
  }
  enemy = new Enemy(50);
}

function draw() {
  background(0);
  for (var i = 0; i < wall.length; i++){
    wall[i].show();
  }
  enemy.show();
  enemy.move();
}

```

Enemy.js

```auto
class Enemy extends Block{
  
  constructor(n) {
    super(n);
    var xspd = 1;
    var yspd = 0;
  }
  
  
  show() {
    fill(0,255,0);
    rect(this.x,this.y,bs,bs);
  }
  
  move() {
    this.x += this.xspd;
    //this.y += this.yspd;
    //print(this.x);
  }
}

```

wall.js

```auto
class Wall extends Block{
  
  constructor(n) {
    super(n);
  }
  
  show() {
    fill(255);
    rect(this.x,this.y,bs,bs);
  }
  
}

```

block.js

```auto
class Block{
  
  constructor(n) {
    this.x = n;
    this.y = 0;
    while (this.x > 23){
      this.y += 1;
      this.x -= 24;
    }
    this.y *= bs;
    this.x *= bs;
  }
  
}

```

wall.csv

```auto
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,47,48,71,72,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,119,120,143,144,167,168,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,215,216,239,240,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,287,288,311,312,335,336,359,360,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,407,408,431,432,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,479,480,503,504,527,528,551,552,553,554,555,556,557,558,559,560,561,562,563,564,565,566,567,568,569,570,571,572,573,574,575

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 2, 2020, 9:45am UTC](https://discourse.processing.org/t/enemy-x-keeps-equaling-nan/18302/2 "2020-03-02T09:45:51Z")

</div>

> [@ctrembla](#):
>
> … & its _x_ keeps showing up `NaN`.

This is the statement that makes property _x_ become `NaN`: `this.x += this.xspd;`

It is so b/c property _xspd_ does’t exist!

Instead you declare a local variable named _xspd_ inside Enemy’s constructor: `var xspd = 1;`

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [March 2, 2020, 10:42am UTC](https://discourse.processing.org/t/enemy-x-keeps-equaling-nan/18302/3 "2020-03-02T10:42:44Z")

</div>

I tried making it above constructor it gave me an error saying it expected a module. & if creating variables in constructor make them only local why does this.x & this.y work how I want them to? I declared them the same way.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 2, 2020, 3:06pm UTC](https://discourse.processing.org/t/enemy-x-keeps-equaling-nan/18302/4 "2020-03-02T15:06:50Z")

</div>

> [@ctrembla](#):
>
> … why does _this.x_ & _this.y_ work how I want them to?

Properties _x_ & _y_ are inherited by class Enemy from class Block: `class Enemy extends Block {`

Also they’re both initialized by calling **super()**: `super(n);`

Obviously if you need a property named _xspd_ you’re gonna need to use keyword `this`:  
`var xspd = 1;` → `this.xspd = 1;`

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [March 2, 2020, 8:37pm UTC](https://discourse.processing.org/t/enemy-x-keeps-equaling-nan/18302/5 "2020-03-02T20:37:29Z")

</div>

that doesn’t answer why I made both the same way & only x y work & I alrdy knew where x y came from I made it in block constructor like I tried to declare xspd & yspd.

I got it working I switched var with this.

Enemy.js

```auto
class Enemy extends Block{
  
  constructor(n) {
    super(n);
    this.xspd = 1;
    this.yspd = 0;
  }
  
  
  show() {
    fill(0,255,0);
    rect(this.x,this.y,bs,bs);
  }
  
  move() {
    this.x += this.xspd;
    //this.y += this.yspd;
    //print(this.x);
  }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 2, 2020, 9:20pm UTC](https://discourse.processing.org/t/enemy-x-keeps-equaling-nan/18302/6 "2020-03-02T21:20:54Z")

</div>

> [@ctrembla](#):
>
> That doesn’t answer why I made both the same way & only _x_ _y_ work &…

I don’t get what you mean b/c object properties aren’t the same thing as local variables & parameters.

Declaring a variable like this `var xspd = 1;` or this `let x = n;`  
isn’t the same as appending another property to an object as `this.xspd = 1;` or `this.x = n;`

> **[Variables vs. Properties in JavaScript](https://javascriptweblog.wordpress.com/2010/08/09/variables-vs-properties-in-javascript/)**
>
> ( 한국어 ) What is a property? What is a variable? In what ways, if any, do they differ? Basic questions. Fundamental to understanding the language, but mostly overlooked in the literature of JavaScri…

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [March 2, 2020, 10:46pm UTC](https://discourse.processing.org/t/enemy-x-keeps-equaling-nan/18302/7 "2020-03-02T22:46:54Z")

</div>

earlier i meant i made both in constructor, but now I get why it didn’t work.
