I have the following code, in one file, but it doesnt seem to work.
I'm basically trying to create an object and try to simply call the object's function and display it, it doesn't do that and I dont know why.
var mice = new Mice(10, 10);
function Mice(posX, posY)
{
this.x = posX;
this.y = posY;
this.moveLeft = function ()
{
this.x = x - 1;
}
this.moveRight = function ()
{
this.x = x + 1;
}
this.getXPos = function ()
{
return this.x;
}
}
document.onkeydown = function(e)
{
//document.getElementById("mainBody").innerHTML = e.keyCode;
switch(e.keyCode)
{
case 37:
//document.getElementById("mainBody").innerHTML = "you have pressed left";
mice.moveLeft();
document.getElementById("mainBody").innerHTML = mice.getXPos();
break;
default:
//do nothing
break;
}
}
any help on trying to get this working will be greatly appreciated.
Thanks
In your "move" functions, you have to consistently refer to this.x
:
this.x = this.x - 1;
Similarly, the "getXPos" function must also:
return this.x;
return x;
You never made an x
variable anywhere.
You mean return this.x;
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With