Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

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)
	    );
   }

1 decade ago by congwang0517

I have a method to solve the problem. The precondition is useing sortEntities() in main.js when draw every time. Because that way entites will put by zIndex. The follow code is a EventListener place in init();
ig.system.canvas.addEventListener('mouseup'/*touchend*/,function(){
		var ents = ig.game.entities;
		for(var i = ents.length-1;i>=0;i--){
			if(ents[i].inFocus() && typeof ents[i]['isClicked'] == 'function' ){
				ents[i].isClicked();
				break;
			}
		}
},false);

The keypoint is break, it can jump out of rest entites.
Page 1 of 1
« first « previous next › last »