Skip to main content
Participant
March 12, 2014
Question

How to rotate nested movieclips properly?

  • March 12, 2014
  • 1 reply
  • 279 views

I have a tank which has a nested movieclip called Barrel.

Barrel successfully faces the player, unless the tank itself rotates.

So it only works if the tank doesn't rotate.

I need to find a way to make the rotation work, and I googled and found:

    var rot:Number = rotation, p:DisplayObjectContainer = parent, s:DisplayObjectContainer =     stage;

    while (p != s) {

      rot += p.rotation;

      p = p.parent;

    }

But I have no idea how to implement this in my current code:

    // calculate rotation based on target

                                        _dx = this.x - _root.hero.x;

                                        _dy = this.y - _root.hero.y;

    // which way to rotate

                                        _rotateTo = getDegrees(getRadians(_dx, _dy));

 

    // keep rotation positive, between 0 and 360 degrees

                                        if (_rotateTo > barrel.rotation + 90) _rotateTo -= 360;

                                        if (_rotateTo < barrel.rotation - 90) _rotateTo += 360;

 

    // ease rotation

                                        _trueRotation = (_rotateTo - barrel.rotation) /     _rotateSpeedMax;

   

    //barrel rotation

                barrel.rotation += _trueRotation;

I tried changing

    barrel.rotation += _trueRotation;

to

    barrel.rotation += _trueRotation + parent.rotation

after reading online that you can simply add all the rotations, but it doesn't seem to work.

How can I implement

    var rot:Number = rotation, p:DisplayObjectContainer = parent, s:DisplayObjectContainer =         stage;

    while (p != s) {

      rot += p.rotation;

      p = p.parent;

    }

into my code?

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
March 12, 2014

just show the code you're using (that's relevant).  in particular, where's getRadians?