0

I'm getting an error:

Uncaught TypeError: Cannot read property 'x' of undefined

I'm trying to make an object move, however, I'm getting this issue when trying to use Vector2. Here is my code:

Player.js

var bullets = [];
var LEFT = 0;
var RIGHT = 1;
var UP = 2;
var DOWN = 3;

var Player = function(){
this.sprite = new Sprite("playerplaceholder.png");
this.sprite.buildAnimation(1, 1, 32, 32, -1, [0]);
this.sprite.setAnimationOffset(0, 0, 0);
this.sprite.setLoop(0, false);

this.position = new Vector2();
this.position.set(200, 200);

this.width = 48
this.height = 48

this.velocity = new Vector2();

this.moveVector = new Vector2();
this.moveVector.set(this.position.x, this.position.y);
};

Player.prototype.update = function(deltaTime)
{
this.sprite.update(deltaTime);

if((keyboard.isKeyDown(keyboard.KEY_LEFT)) != (keyboard.isKeyDown(keyboard.KEY_RIGHT)))
{
    if(keyboard.isKeyDown(keyboard.KEY_RIGHT))
    {
        Player.moveVector.x += 1;
    }
    else
    {
        Player.moveVector.x += -1;
    }
}
if((keyboard.isKeyDown(keyboard.KEY_UP)) != (keyboard.isKeyDown(keyboard.KEY_UP)))
{
    if(keyboard.isKeyDown(keyboard.KEY_DOWN))
    {
        Player.moveVector += -1;
    }
    else
    {
        Player.moveVector += 1;
    }
}

Player.position += Player.moveVector;

}

Player.prototype.draw = function()
{
this.sprite.draw(context, this.position.x, this.position.y);
}

Vector2

var Vector2 = function()
{
this.x = 0;
this.y = 0;
};

Vector2.prototype.set = function(x,y)
{
this.x = x;
this.y = y;
};

Vector2.prototype.add = function(a_x,a_y)
{
    return (this.x + a_x),(this.y + a_y);
}

Vector2.prototype.subtract = function(a_x,a_y)
{
    return (this.x - a_x),(this.y - a_y);
}

Vector2.prototype.MultiplyScalar = function(input)
{
return (this.x * input),(this.y * input);
}

Vector2.prototype.Magnitude = function ()
{

return Math.sqrt((x*x) + (y*y)) //Gives magnitude of current vector2
}

Vector2.prototype.Normalize = function ()
{

var rect = new Vector2();
rect.x = this.x / this.Magnitude();
rect.y = this.y / this.Magnitude();

return ret;
}
ZygD
  • 22,092
  • 39
  • 79
  • 102
Whipgun
  • 3
  • 1
  • 4
  • Just forgot to mention that the error is on line 33 which is: Player.moveVector.x +=1; – Whipgun Jun 24 '15 at 10:16
  • `Player.moveVector` is undefined. try `console.log(Player);` at line 32 and check the console output. Also, at no point to you assign a `moveVector` property to `Player` – atmd Jun 24 '15 at 10:47
  • 3
    Possible duplicate of [Detecting an undefined object property](https://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) – Liam Nov 20 '18 at 12:04

1 Answers1

1

Probably this error is because you are trying to access moveVector function wrong.

Player.moveVector.x += 1;

Here you are accessing moveVector on the Player function that is undefined. Probably what you wanted was something like this:

this.moveVector.x += 1;

Same for the other in your Player.prototype.update:

Player.prototype.update = function(deltaTime)
{
    this.sprite.update(deltaTime);

    if((keyboard.isKeyDown(keyboard.KEY_LEFT)) != (keyboard.isKeyDown(keyboard.KEY_RIGHT)))
    {
        if(keyboard.isKeyDown(keyboard.KEY_RIGHT))
        {
            this.moveVector.x += 1;
        }
        else
        {
            this.moveVector.x += -1;
        }
    }
    if((keyboard.isKeyDown(keyboard.KEY_UP)) != (keyboard.isKeyDown(keyboard.KEY_UP)))
    {
        if(keyboard.isKeyDown(keyboard.KEY_DOWN))
        {
            this.moveVector += -1;
        }
        else
        {
            this.moveVector += 1;
        }
    }

    this.position += this.moveVector;

}
dreyescat
  • 13,558
  • 5
  • 50
  • 38