- This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
Viewing 2 posts - 1 through 2 (of 2 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › my 2d game engine problem
hi i wanted to make my own js canvas library (got the idea from js snake :)) and it was all going fine till i came across a problem:
i have a “class” for a rectangle
function rect(width,height,x,y,colour,move){
this.height = height;
this.width = width;
this.x = x;
this.y = y;
this.colour = colour;
this.move = undefined;
this.paint = function(ctx){
ctx.fillStyle = colour;
ctx.fillRect(x,y,height,width);
//console.log('rect paint',x,y,height,width);
//console.log('this',this);
};
};
the “move” property and the “paint” property are called every frame
i wanted to make them available to overload with your own custom move and possibly paint function
like this:
ENGINE.game.init();
var rect = new ENGINE.shapes.rect(10,10,10,10,'#fc1');
rect.move = function(){
this.x ++;
};
but this dosent work, however the function does get called every frame?
tl;dr : cant overload move function!
full code: main.js — http://www.pastie.org/2117678
shapes.js — http://www.pastie.org/2117685
hope you can help as any help is greatly appreciated;
-James
its ok i have found the solution
i needed to refer to the variables with this
like
this.x++;
this.y = Math.sin(this.x/50)*100;
-James