Hi Im trying to get my head around javascript oop and am running into a problem,I'm building a constructor class, in the class I define the properties now i want to define some methods that alter those properties, but when I instanciate the object and invoke the method the the consol tells me that the properties are undefined. I imagine that this might have something to do with scope, I`ve been looking around google alot but all introductory articles are basically the same.
heres a simplifed version of the code. In my example code I want a shape to move around on the canvas. the object it self will have the method which controls its movement (just to right for now). when i instantiate the object i call its moveRight method which should alter its xy coordinates. then every second i rendor it to the screen in a separate function which calls for the objects xand y properties
//here i define object
function Mechanoid(){
//object properties
this.life=100;
this.x=500;
this.y=200;
this.anArray=new Array(0, 0); //can i create an array like this? i know it works when called from outside the object
//object methods
this.moveAround=function(){
var clock=setInterval(Function () {
this.x=this.x+1; //console log says undefined
this.y=this.y+1;
this.anArray[0]=this.x; //console says cannot read propety of null
this.anArray[1]=this.y;
},1000);
}
}
//then instanciate
var mech=new Mechanoid;
mech.moveAround(); // calls method to change object properties
//A request for the x any y coordinates of mech object will be called in a render function where it
//will be drawn to the canvas.
Can anyone tell me why the properties are not accessible from within the object method? and what i have to do to access them? thanks... there's probably a bracket missing in the syntax or something I wrote it on the fly i don't think theres a syntax error in the original code , and i dont think thats the problem.