1 decade ago by congwang0517
I refer to Graphikos's code ,wrote a click plugin about entity click .But has the Click-through problem .When i click a entity, another entity overlied behind it is also be clicked. Thanks for any help.
ig.Entity.prototype.clicked = function() { if (ig.input.released('mouseLeft') && this.inFocus()) { var x_up = ig.input.mouse.x,y_up = ig.input.mouse.y; var distance = Math.sqrt(Math.pow((ig.game.mousePressed.x - x_up),2)+Math.pow((ig.game.mousePressed.y - y_up),2)); //ig.log(distance); if(distance > 5){//is drag,not click return false; }else{ return true; } }else{ return false; } }, ig.Entity.prototype.inFocus = function() { return ( (this.pos.x <= (ig.input.mouse.x + ig.game.screen.x)) && ((ig.input.mouse.x + ig.game.screen.x) <= this.pos.x + this.size.x) && (this.pos.y <= (ig.input.mouse.y + ig.game.screen.y)) && ((ig.input.mouse.y + ig.game.screen.y) <= this.pos.y + this.size.y) ); }