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 CaueCR

Hello again!
I am working in a project where an entity animation must change when I click it. The problem is, when I write the condition for colision, even what was working before stops working

The following is the code for colision with mouse coordinates I've made:

if(ig.input.state('click') && (ig.input.mouse.x >= this.pos.x && ig.input.mouse.x <= this.pos.x + this.size.x) && (ig.input.mouse.y >= this.pos.y && ig.input.mouse.y <= this.pos.y + this.size.y)){
	this.currentAnim = this.anims.erro;
	if(this.flag == false){
		this.flag = true;
	}
 }
if(ig.input.state('click') && this.flag == true){
        this.currentAnim = this.anims.idle;
	if((ig.input.mouse.x >= this.pos.x && ig.input.mouse.x <= this.pos.x + this.size.x) && (ig.input.mouse.y >= this.pos.y && ig.input.mouse.y <= this.pos.y + this.size.y)){
		this.flag = false;
	}
 }
 this.parent();

If I change the code of the second condition to be like the first condition, none of them works.

I've been putting this code in the update method of my entities, can anyone help me?

Thanks for your time!
CauĂȘ.

1 decade ago by dominic

First of all, Impact's mouse position is in screen coordinates, while entity positions are in world coordinates. I.e. if your game screen is 640x480, possible mouse positions range from 0,0 to 640,480. However, entity positions may be well outside of this 640,480 range, because you can move the "camera", i.e. scroll the screen.

That said, if your game doesn't use scrolling, this may not be an issue (it doesn't seem to be one in your game).

Also, you probably want to use ig.input.pressed() instead of ig.input.state(), otherwise the animations will flickr back and forth for each frame. pressed() only is true for the single frame where the button is pressed, as opposed to being true for all frames while the button is held down.

Now, imagine the mouse is over your entity and clicked down, i.e. ig.input.state('click') returns true: what you do is setting flag = true and then do the same check again immediately and set flag = false again. This doesn't make much sense.


Lastly, I don&039;t think you need #flag at all, since you can just check what the current animation is.

// Calculate the current screen position of the entity
var sx = this.pos.x - ig.game.screen.x;
var sy = this.pos.y - ig.game.screen.y;

if(
	ig.input.pressed('click') && 
	ig.input.mouse.x >= sx && ig.input.mouse.x < sx + this.size.x &&
	ig.input.mouse.y >= sy && ig.input.mouse.y < sy + this.size.y
) {
	if( this.currentAnim != this.anims.idle ) {
		this.currentAnim = this.anims.idle;
	}
	else {
		this.currentAnim = this.anims.erro;
	}
}

1 decade ago by CaueCR

Hello Dominic!
Thanks for answering me! It worked perfectly!

Thank you so much for the help!

Sorry for the trouble!
Page 1 of 1
« first « previous next › last »