1 decade ago
by Razbun
In my code I have an entity that has the following in its init: function
this.shield.active = false;
I noticed that if i have multiple spawned entities and i modify this.shield.active for one of them, it modifies it for all.
If i change it to this.shieldActive = false and then modify it, it will only modify, as expected, the active entity. Could someone please explain what is going on in the first scenario? I am sure I am missing some basic JS understanding.
thanks!
1 decade ago
by riceje7
can you show some more code? it's difficult to determine cause without the context of the code you gave.
1 decade ago
by Razbun
sure, i thought it might be something obvious with how properties or impact behaves. Didn't think it was an actual issue with the code.
Entity, init:
init: function(x, y, settings){
this.parent(x,y,settings);
this.shield.active = false;
...
In the draw function of the above entity, I invoke:
if (ig.input.released('click_mouse1')&&(this.plat.activeEntity == this)){
this.shield.active = true;
};
this.plat.activeEntity is always returning the currently selected entity. The above IF statement triggers corretly. I does not trigger on release of mouse button for non selected but instantiated entities.
The above is likely not enough to help me with this i guess. I'll likely have to take a more in depth look at the code then, if this is indeed an issue with how i'm using it.
1 decade ago
by Joncom
Tip: People are more likely to read your code if you enclose it within #'s like this:
##
/* you code here */
##
var foo = function() {
console.log("Because syntax highlighting is nice...");
}
Could you post the part of the code where you declare shield ?
1 decade ago
by Razbun
Thanks for the tip
shield: function (){
var startX = 0;
var startY = 0;
var endX = 0;
var endY = 0;
if ((ig.input.pressed('click_mouse1'))&&(this.plat.activeEntity == this)&&(this.phasing != 'onMouse')){
this.shieldActive = false;
this.timer.reset();
this.timer.unpause();
startX = ig.input.mouse.x-ig.game.screen.x;
startY = ig.input.mouse.y-ig.game.screen.y;
this.points = [];
this.point = [];
this.point.push(startX);
this.point.push(startY);
this.points.push(this.point);
};
if (ig.input.released('click_mouse1')&&(this.plat.activeEntity == this)){
this.timer.reset();
this.timer.pause();
this.timer2.reset();
this.timer2.unpause();
this.shieldActive = true;
};
if((this.timer.delta()>=0)&&(this.plat.activeEntity == this)&&(this.phasing != 'onMouse')){
this.timer.reset();
ig.system.context.strokeStyle = "blue";
ig.system.context.lineWidth = .5;
ig.system.context.beginPath();
endX = ig.input.mouse.x-ig.game.screen.x;
endY = ig.input.mouse.y-ig.game.screen.y;
ig.system.context.moveTo(this.points[0][0],this.points[0][1]);
this.point = [];
this.point.push(endX);
this.point.push(endY);
this.points.push(this.point);
for (var i=1;i<this.points.length;i++){
ig.system.context.lineTo(this.points[i][0],this.points[i][1]);
ig.system.context.moveTo(this.points[i][0],this.points[i][1]);
ig.system.context.stroke();
ig.system.context.closePath();
};
startX=endX;
startY=endY;
};
if(this.shieldActive == true){
ig.system.context.strokeStyle = "blue";
ig.system.context.lineWidth = .9;
ig.system.context.beginPath();
ig.system.context.moveTo(this.points[0][0],this.points[0][1]);
for (var i=1;i<this.points.length;i++){
ig.system.context.lineTo(this.points[i][0],this.points[i][1]);
ig.system.context.moveTo(this.points[i][0],this.points[i][1]);
ig.system.context.stroke();
ig.system.context.closePath();
};
};
if(this.timer2.delta()>=this.shield.duration){
this.timer2.reset();
this.timer2.pause();
this.shieldActive = false;
};
},
1 decade ago
by drhayes
So, in your original example, you had code that looked like this:
this.shield.active = true;
Was
shield
a function then? If it was then, yes, this would propagate across all your entities because each one is sharing the same copy of the
shield
function (for more info, check out an article about JavaScript and prototypal inheritance).
You&
039;re better off doing what you have here with #this.shieldActive=true;
.
Incidentally, when you're making points you can change this code:
this.point = [];
this.point.push(x);
this.point.push(y);
this.points.push(this.point);
...to this code:
this.points.push([x, y]);
And it'll work the same, but be less typing.
1 decade ago
by Razbun
Thank you for the excellent reply! You clarified exactly what I suspected. (and bonus for the points tip)
Page 1 of 1
« first
« previous
next ›
last »