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