Question

So first off i'd like to state that i am not very good at AS3, i'm completely self taught so i am sure that there are many things i've done badly, inefficiently or plain wrong and i'm happy for any comments on these if there is things i can improve.

So i'm trying to add the functionality for a player to be able to click on a part of my game and a"hook" will fire from the character in the direction of the mouse click for a certain length. the end of this will then be collision detection between that hook and a platform.

however the problem i'm having is i have no idea how to run the code which will locate the angle which the hook needs to be fired in, i understand this is a certain amount of maths and any help which can be given would be helpful ( even if you can't fix the problem for me)

So far i have been able to locate the x and y positions of both my hero and the position on the mouse click this code is shown below:

 public function Hookshot (event:MouseEvent):void {

    PlatformInstance[i] =new Platform();
        var _point:Point = localToGlobal (new Point(mouseX,mouseY));
         var heroX:int = Smallclock_hero.x;
         var heroY:int = Smallclock_hero.y;
         trace (heroX)
        trace (heroY)
         trace(_point.x)
        trace(_point.y)
    }

I'm not sure there is any other relevant code as of yet as i haven't been able to wrap my head around how to do this kind of maths.

if there is any additional code you need or any questions you have please ask away and thanks in advance!

Was it helpful?

Solution

Math.atan2(y, x) will convert an x and y value into an angle:

var mousePos:Point = localToGlobal(new Point(mouseX,mouseY));
var heroPos:Point = localToGlobal(new Point(Smallclock_hero.x, Smallclock_hero.y));

var dx:Number = mousePos.x - heroPos.x;
var dy:Number = mousePos.y - heroPos.y;
var radians:Number = Math.atan2(dy, dx);

// Convert the radians value to degrees
var angleInDegrees:Number = radians * 180/Math.PI;

trace(angleInDegrees);

Math.atan2() documentation: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Math.html#atan2()

OTHER TIPS

Your question isn't so much AS3 related, as it is simple algebra.

If you have the x,y of the character, and the x,y of the target, then you just need to do the equation of a line to get the angle.

atan((Y2-Y1)/(X2-X1))

That should give you radians, and then simply multiply it by (180/pi) to get degrees

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top