1

Hey everyone so I am having some trouble with this. So I have an Array of Barrel Movie clips that are added to the stage and a movie clip called Circle that when comes in contact with any of the barrels in the array the barrel that it is currently hittesting starts to rotate. It rotates on its registration point which is centered. So in my ENTER_FRAME event is where I have everything setup for the barrel to rotate and hit Test etc...

This is how the circle is added to the barrels:

            if (!circleFired)
            {
                circle.x = globalFirePoint.x;
                circle.y = globalFirePoint.y;
                currentCannon.addChildAt(circle, 0);
            }

Now what I am trying to accomplish is when the user taps the screen the circle Movie clip is shot from the Barrel. Right now I have the circle shooting from the barrel in the angle that its supposed to shoot but instead of going in a straight line from the angle it shoots it shoots and starts to curve really quickly in the clockwise position that the barrel is rotating in.

Here is the code I have that is sort of working besides that one problem:

        var dX:Number = globalFirePoint.x;
        var dY:Number = globalFirePoint.y;
        var angle:Number = Math.atan2(dY, dX);

        if (circleFired)
        {
            circle.x +=  Math.sin(deg2rad(angle -90)) * velocity;
            circle.y += Math.cos(deg2rad(angle - 90)) * velocity;
        }

so the globalFirePointis this:

globalFirePoint = localToGlobal(new Point(currentCannon.mcFirePoint.x, currentCannon.mcFirePoint.y));

Its the starting point I want the circle to shoot from.

Here is the deg2rad Function:

private function deg2rad(num:Number):Number
    {
        var radians:Number = num * (Math.PI / 180);
        return radians;
    }

I know that I need sin and cos to calculate the angle correct? Is the math wrong? Why is the circle curving when the circle shoots from the globalFirePoint? What can I do to make it go in a straight line?

Any help would be appreciated thanks!

Nathan
  • 536
  • 4
  • 21
  • It is definitely just following the barrels rotation path I dont know how to have it break off of its path and just continue in a straight line – Nathan Oct 16 '15 at 23:57

1 Answers1

1

Does globalFirePoint change on each ENTER_FRAME event? If so then the calculated angle is going to change as well, leading to the curve you are experiencing.

One way to fix this is to only calculate globalFirePoint when the user presses the screen. Then on every ENTER_FRAME event instead of re-calculating globalFirePoint, use the already calculated globalFirePoint that won't change unless the user presses the screen when the circle is in a barrel.

You should only calculate the angle when the user presses the screen as well. There is no need to calculate it on every ENTER_FRAME event, since the only time you're interested in the angle is when the circle is shot from a barrel.

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
  • I removed all the variables from the ENTER_FRAME Listener and added them to the mouse event when the user presses the screen but still is curving. I assume it is curving because it is following the globalFirePoints path when it rotates corrrect? I have the the globalFirePoint added at the end of the barrel. So picture a Barrel spinning in a perfect circle and the ball shoots from the end of the barrel which is the globalFirePoint – Nathan Oct 16 '15 at 23:26
  • Maybe my math is wrong? I have updated the question with the extra equations I left out by accident if you can check it out. – Nathan Oct 16 '15 at 23:29
  • @Nathan Don't add the circle as a child to barrel. The reason why the circle is curving is because it's a child of the barrel, and the barrel movie clip is spinning. – Jonny Henly Oct 17 '15 at 00:03
  • I figured that. So then just use this addChildAt(circle, 0); or this stage.addChildAt(circle, 0); what should I add it to in order for the circle to still rotate along with the globalFirePoint? because when ever I try any of those methods the circle spawns on the far left of the stage and no where near the globalFirePoint :/ – Nathan Oct 17 '15 at 01:33
  • 1
    @Nathan `circle` should already be a child of `stage`. If you want the circle to rotate with the `globalFirePoint`, then in your `ENTER_FRAME` event handler set the `x` and `y` of the `circle` to that of the `globalFirePoint`. Leave the `circle` on the `stage` and if in the future you want it to be hidden then set its `visible` property to `false`. – Jonny Henly Oct 19 '15 at 01:13
  • Thanks for the advice and information I really appreciate it. – Nathan Oct 19 '15 at 06:12