Search code examples
actionscript-3flashactionscriptairadobe

Object does not rotate around pivot point


I have an object which looks like this: enter image description here

When I try to rotate it using [INSTANCENAME].rotation += 10; it still rotates around the middle of the object. Am I doing something wrong?


Solution

  • I've done a little research, and apparently there is no direct way to change the pivot point programmatically, AFAIK. Rather odd, seeing as you can change it in the Flash Pro IDE. (For those reading, the pivot point is NOT the same as the registration point.)

    [EDIT: Thinking more about it, I don't think the pivot point actually "exists", at least in the context of a programmable property. It only exists in the context of some tools in the Flash IDE.]

    You will need to place your object ALONE inside another object, so that the desired pivot point of the inner object is over the center of the outer object. Then, rotate the outer object.

    You can later control your object's "pivot point" by changing the position of the inner object.

    Since the outer object's center moves depending on its size, and the position of the inner object determines the size of the outer object, you'd need to apply some math. The x position of the inner object would need to be equal to the distance from the desired pivot point on the inner object to the far right edge of that inner object. The same concept applies to the y position.

    This can be accomplished both in the IDE directly, or through code, whichever you choose.

    An example of code to change this dynamically. This function is inside the outer object. (You can get pivotX and pivotY off an event listener, if you want.) [Sorry, the code is untested.]

    function newPivot(int pivotX, int pivotY):void
    {
       inner.x = inner.width - pivotX;
       inner.y = inner.height - pivotY;
    }
    

    I hope that solves your problem!