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 Nico

Hey guys so I'm trying to dynamically check whether or not the player is clicking on an NPC by looping through the array of EntityNpc's. If an entity is clicked on something should happen but the players weapon shouldn't fire.

It's super easy to just check whether ig.game.getEntitiesByType( EntityNpc )[0] is inFocus and then not fire if it is. But that doesn't scale at all...I feel like I'm on the right track but could use a push in the right direction.

Currently when I'm in a map with NPC's and I use the code as is, all the NPC's are affected by my click, no matter where I click on the screen.

            this.npcList = ig.game.getEntitiesByType( EntityNpc );
            if(this.npcList.length > 0){
                for( e in this.npcList ){
                    this.npc = ig.game.getEntitiesByType( EntityNpc )[e];
                    if(ig.input.pressed('click') && this.npc.inFocus ){
                        this.npc.vel.y = -150;
                    }else if(ig.input.pressed('click') && !this.npc.inFocus ){
                        this.shootSwitch();
                    }
                }
            }else if(this.npcList.length <= 0 && ig.input.pressed('click')){
                this.shootSwitch();
            }

Any help is greatly appreciated !

1 decade ago by Joncom

Quote from Nico
...easy to just check whether ig.game.getEntitiesByType( EntityNpc )[0] is inFocus and then not fire if it is. But that doesn't scale at all.
I'm curious what you mean by "doesn't scale at all." You mean it works for the entity with index 0, but not others?

One suggestion I would make is to not use ig.game.getEntitiesByType in your for-loop. The function is expensive, and that&039;s just plain unnessessary since you already have all the results stored as #this.npcList.

I'd rewrite your code something like this:
this.npcList = ig.game.getEntitiesByType(EntityNpc);

// getEntitiesByType returns undefined when no results.
if (typeof this.npcList.length !== 'undefined') {

    for (var i = 0; i < this.npcList.length; i++) {

        // More efficient than calling getEntitiesByType each time.
        this.npc = this.npcList[i];

        if (ig.input.pressed('click') {
            if (this.npc.inFocus) this.npc.vel.y = -150;
            else this.shootSwitch();
        }
    }

} else if (ig.input.pressed('click')) {

    this.shootSwitch();
}

Although, I'm not sure if that actually solves your problem or not... :P

1 decade ago by Nico

Quote from Joncom
I'm curious what you mean by "doesn't scale at all." You mean it works for the entity with index 0, but not others?


Exactly, and I want it to trigger for any NPC I click instead of having to hardcode the index number to my function.

I tried that snippet you posted and tweaked it to no avail.

I can tell that it's much less taxing on the system but I still have the same problem: when I click the action is triggered for all NPC's on the map, regardless of whether I actually click on them or not.

EDIT:

If it helps here's the snippet that works if you assign a specific index value to the entity:

            this.npc = ig.game.getEntitiesByType( EntityNpc )[0];
            if(ig.input.pressed('click')){
                if(this.npc){
                    if(this.npc.inFocus()){
                        this.npc.vel.y = -150;
                    }else{
                        this.shootSwitch();
                    }
                }else{
                    this.shootSwitch();
                }
            }

1 decade ago by Joncom

Quote from Nico
when I click the action is triggered for all NPC's on the map, regardless of whether I actually click on them or not.
That makes sense. Because apparently .pressed() works this way:
In contrast to .state(), this method only returns true once for each button press.
Since your loop checked .pressed several times, only the first time will actually return true. This is why it works when you supply just a single index.

Try putting your input check outside the entire block of code so it's only run once. Something like:

if (ig.input.pressed('click') {

	this.npcList = ig.game.getEntitiesByType(EntityNpc);

	if (typeof this.npcList.length !== 'undefined') {

	    for (var i = 0; i < this.npcList.length; i++) {

	        this.npc = this.npcList[i];
	  
	        if (this.npc.inFocus) this.npc.vel.y = -150;
	        else this.shootSwitch(); 
	    }

	} else {

	    this.shootSwitch();
	}
}

1 decade ago by Nico

Solved with this.

main.js update :

        if(  ig.input.pressed('click')){           
            this.clicked=new Array();
        }

        if(ig.input.released('click')){

            this.sTarget=null;
            if(this.clicked != null){
                for(i = 0;i < this.clicked.length; i++)
                    {
                        this.sTarget=this.clicked[i];
                        if(this.sTarget.inFocus()){
                            this.sTarget.click();
                        }
                    }
            }
        }

anyentity.js

        update: function() {
            if(ig.input.pressed('click')  && this.inFocus()){
                ig.game.clicked.push(this);
            }
        	this.parent();
        },
        click: function() {
            console.log('do something');
        }

Appreciate the help, Joncom :)
Page 1 of 1
« first « previous next › last »