Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/296106023528308736
Tags go in tags, not title.
Source Link
House
  • 73.3k
  • 17
  • 185
  • 273

JavaScript - Initiate Game Objects Instantiate objects that Share Same Variableshare same variable

I'm just wondering whats the best wayhow to initiateinstantiate the same objects that all share the same variables, except their positions.

I've built my lamp within a variable function (I think they're called that 'var = {'var = {), and within my actual game DrawFunctionDrawFunction, I'm calling 'LampPost.draw();'LampPost.draw();.

etc ... and then possibly place each initiated Lampinstantiate Lamp within an array?

I sure would appreciate the help here, I'm kind of new to JavaScript. Thanks!

JavaScript - Initiate Game Objects that Share Same Variable

I'm just wondering whats the best way to initiate the same objects that all share the same variables, except their positions.

I've built my lamp within a variable function (I think they're called that 'var = {'), and within my actual game DrawFunction, I'm calling 'LampPost.draw();'.

etc ... and then possibly place each initiated Lamp within an array?

I sure would appreciate the help here, I'm kind of new to JavaScript. Thanks!

Instantiate objects that share same variable

I'm just wondering how to instantiate the same objects that all share the same variables, except their positions.

I've built my lamp within a variable function (I think they're called that var = {), and within my actual game DrawFunction, I'm calling LampPost.draw();.

etc ... and then possibly place each instantiate Lamp within an array?

added 14 characters in body
Source Link
LampPost(0,0);
LampPost(100, 0);
LampPost(200, 0);

LampPost(0,0); LampPost(100, 0); LampPost(200, 0); etcetc ... and then possibly place each initiated Lamp within an array?

LampPost(0,0); LampPost(100, 0); LampPost(200, 0); etc ... and then possibly place each initiated Lamp within an array?

LampPost(0,0);
LampPost(100, 0);
LampPost(200, 0);

etc ... and then possibly place each initiated Lamp within an array?

added 994 characters in body
Source Link

----- UPDATE -----

Okay, So I've converted it into an instance of object, this is what I have so far:

LampPost = function(lx, ly, la){

    x = lx; //500;
    y = ly; //335;

    centrex = 12;
    centrey = 17;

    rotate = la; //-8;

}

LampPost.prototype.draw = function (){

    ctxPropsOver.setTransform(1, 0, 0, 1, (-Map.x + gameWidth/2), (-Map.y + gameHeight/2) );
     
    ctxPropsOver.clearRect(0, 0, 1000, 1000);
    
    ctxPropsOver.save();
        ctxPropsOver.translate(this.x, this.y);
        ctxPropsOver.rotate(this.rotate*Math.PI/180);
        ctxPropsOver.translate(-this.centrex, -this.centrey);
        ctxPropsOver.globalAlpha = this.lightFlicker;
        ctxPropsOver.drawImage(imgLights, 0, 36, 500, 463, -60, -190, 500, 463);
        ctxPropsOver.globalAlpha = 1;
        ctxPropsOver.drawImage(imgLights, 0, 0, 210, 36, 0, 0, 210, 36);
    ctxPropsOver.restore();
};

----- UPDATE -----

Okay, So I've converted it into an instance of object, this is what I have so far:

LampPost = function(lx, ly, la){

    x = lx; //500;
    y = ly; //335;

    centrex = 12;
    centrey = 17;

    rotate = la; //-8;

}

LampPost.prototype.draw = function (){

    ctxPropsOver.setTransform(1, 0, 0, 1, (-Map.x + gameWidth/2), (-Map.y + gameHeight/2) );
     
    ctxPropsOver.clearRect(0, 0, 1000, 1000);
    
    ctxPropsOver.save();
        ctxPropsOver.translate(this.x, this.y);
        ctxPropsOver.rotate(this.rotate*Math.PI/180);
        ctxPropsOver.translate(-this.centrex, -this.centrey);
        ctxPropsOver.globalAlpha = this.lightFlicker;
        ctxPropsOver.drawImage(imgLights, 0, 36, 500, 463, -60, -190, 500, 463);
        ctxPropsOver.globalAlpha = 1;
        ctxPropsOver.drawImage(imgLights, 0, 0, 210, 36, 0, 0, 210, 36);
    ctxPropsOver.restore();
};
Source Link
Loading