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 kanito

Hi everyone!

I have a strange problem with one entity. The intention of the script is to rotate the entity and face it to one point defined by the mouse when the user clicks on the screen.

The entity rotates, but it doesn't stop when it reaches the angle.

This is the code:

EntityPlayer = ig.Entity.extend({
	
	size: {x: 66, y: 31},
	animSheet: new ig.AnimationSheet( 'media/T72.png', 66, 31 ),

	init: function( x, y, settings ) {
		this.parent( x, y, settings );
		x = 0;
		y = 0;
		angle = 0;
		clicked = false;
		
		this.addAnim( 'idle', 1, [0] );
	},
	
	update: function(  ) {
		if( ig.input.pressed( 'click' ) && clicked == false ){
			clicked = true;
			x = this.pos.x;
			y = this.pos.y;
			mx = ig.input.mouse.x;
			my = ig.input.mouse.y;
			angle = Math.acos((x-mx)/(Math.sqrt((x-mx)*(x-mx) + (y-my)*(y-my))));
		}
		if( (Math.abs(this.currentAnim.angle) < (Math.abs(angle + 0.1))) && (Math.abs(this.currentAnim.angle > (Math.abs(angle - 0.1)))) && (clicked == true)){
			this.anims.idle.angle = angle;
			clicked = false;
		}
		else if ( clicked == true ){
			this.anims.idle.angle  = this.currentAnim.angle - 0.01;
			document.writeln(' # object: ', Math.abs(this.currentAnim.angle), ', angle: ', Math.abs(angle), ', clicked: ', clicked);
		}
		else {
			clicked = false;
		}
	}
});

Can anyone help me?

Thanks in advance!

1 decade ago by StuartTresadern

This line may be the issue (not tested just a quick look at the above code)

 if( (Math.abs(this.currentAnim.angle) < (Math.abs(angle + 0.1))) && (Math.abs(this.currentAnim.angle) > (Math.abs(angle - 0.1))) && (clicked == true)){ 

check the bracket change on 2nd angle test .

1 decade ago by kanito

Yes!!!!!! That's it! At last I found the error. Thank you very much!
Page 1 of 1
« first « previous next › last »