Impact

» Edit Game Info

2 months ago by orangebread

Trying to trigger my animation based on where the angle of the mouse was clicked. Is my approach incorrect or how I'm calculating the math?

toAngle: function( x ) {
            return (x > 0 ? x : (2*Math.PI + x)) * 360 / (2*Math.PI);
        },

// ATTACK
            if (pressed_shoot) {
               
                var radians = Math.atan2(this.pos.y - ig.input.mouse.y, this.pos.x - ig.input.mouse.x);
                var dest = this.toAngle(radians);

                //Down-Left
                if (290.0 <= dest < 350.0) {
                    console.log('down left');
                    this.flip = true;
                    this.currentAnim = this.anims.shootDownRightFront;
                }
                // down 1.3 < x < 1.7
                else if (260.0 <= dest < 290.0) {
                    console.log('down');
                    this.flip = false;
                    this.currentAnim = this.anims.shootDown;
                }
                // downright 0.1 <= x < 1.3
                else if (190.0 <= dest < 260.0) {
                    console.log('down right');
                    this.flip = false;
                    this.currentAnim = this.anims.shootDownRightFront;
                }
                // right -.1 <= x < 0 || 0 < x < 0.1
                else if (170.0 <= dest <= 190.0) {
                    console.log('right');
                    this.flip = false;
                    this.currentAnim = this.anims.shootRightFront;
                }
                // upright -1.3 <= x < -.1
                else if (100.0 <= dest < 170.0) {
                    this.flip = false;
                    this.currentAnim = this.anims.shootUpRightFront;
                }

1 month ago by Joncom

If I remember correctly from our conversation, the problem was that you were checking your conditions like this (100.0 <= dest < 170.0) instead of like this (100.0 <= dest && dest < 170.0).

1 month ago by orangebread

Correct. Javascript hard sometimes!
Page 1 of 1
« first « previous next › last »

Post Reply

Please login or register to reply.