Search code examples
actionscript-3actionscriptplatform

Rotation issue While Reflected (2D Platform Game)


My player's arm is programmed to follow my mouse and rotate accordingly and I've programmed bullets to be fired using this rotational value

(Math.atan2(this._dy, this._dx) * 180 / Math.PI

where _dy is the y location of the mouse (-) the y of my player's arm and the _dx is the x location of mouse (-) the y of my player's arm.

However, when I program the player to reflect when the mouse has crossed the x-coordinates, the bullet angle is also reflected. How would I fix this issue?

I've already tried subtracting 180 from the angle but it still doesn't fire towards the direction of the mouse.


Solution

  • First, make sure you have this parent-child-sibling relationship: characterObject "A" should be the parent of "B" and "C". "B" and "C" should have no direct link. Their connection is that they have the same parent. So when you want to move the character, move the parent, and both will move. Now, for the good stuff:

    Use key frames and sibling relationship

    beginner level approach

    Make the character and the arm both children of the same parent display object container (Movie Clip in this case). Now, instead of flipping anything by xScale, which I assume you did, you can just have both MC children (arm and character) go to frame 2 (or whatever is available) where the graphics are flipped.


    xScale body, move arm to frame 2, change z order

    moderate level approach (best result)*

    Alternatively, you could do that same "sibling" setup as above, and then scale the character but not the arm (I think scaling the arm will mess it up again, but you could have the arm go to frame 2 and have it drawn reversed so the thumb and handle are pointing the right way. Bonus points for changing the z stacking order so the arm goes to the other side of the body. xScale for only the body allows you to only have one set of frames for animation of his legs and torso etc. but also avoid scaling the arm at all).


    Global properties

    advanced approach

    A third option is to use global rotation and global points. I won't illustrate that here because I'm not that advanced and it would take me a while to figure out the exact syntax. If you already have mastered global properties, try this; if not, try one of the ones above.


    * Example (best result)

    if (facingRight == true && stage.mouseX < totalChar.x){ 
    // totalChar is on the stage
    // and contains two children: 
    // armAndGun and bodyHeadLegs
        totalChar.armAndGun.gotoAndStop(2);
        // in frame 2 of the arm MC, draw the 
        // arm and gun in the flipped orientation
        totalChar.addChild(bodyHeadLegs);
        // re-ads body to parent so it's
        // z-order is above the arm;
        totalChar.bodyHeadLegs.xScale = -1;// flips body and any animation of legs and head
        facingRight = false;
        // use a variable or property like this
        // to keep him from constantly flipping
    }
    

    You'll need similar code to flip him back the other way.