I have a sprite that I want to rotate around it’s center point. Below is the custom class I wrote. For some reason the Sprite rotates around 0 point of the Y-axis instead of it’s own center point. If anyone could show me what I’m doing wrong it would be greatly appreciated.
package lib
{
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.geom.Matrix3D;
import flash.geom.Vector3D;
public class CoverFlowObj extends Sprite
{
var _centerStageX:uint;
var _centerStageY:uint;
var _maxRotation:Number;
var _rotationSpeed:Number;
var _w:uint;
var _h:uint;
public function CoverFlowObj (w:uint, h:uint)
{
// set variables
_maxRotation = 65;
_rotationSpeed = .5;
_w = w;
_h = h;
// wait until added to display stack;
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
// initialize;
private function init (e:Event):void
{
_centerStageX = Math.floor (stage.stageWidth/2);
_centerStageY = Math.floor (stage.stageHeight/2);
var bg:Shape = drawRect(_w, _h);
addChild(bg);
init3d();
this.addEventListener(Event.ENTER_FRAME, rotate);
} //end init()
// return rectangle Shape of specified width / height;
private function drawRect (w:uint, h:uint):Shape
{
var rect:Shape = new Shape();
rect.graphics.beginFill (0x666666);
rect.graphics.drawRect (0, 0, w, h);
rect.graphics.endFill();
return rect;
} //end drawRect()
// set starting 3D coordinates;
private function init3d ():void
{
this.x = _centerStageX - this.width / 2;
this.y = _centerStageY - this.height / 2;
this.z = 1;
}
// rotate shape around y axis;
private function rotate (e:Event):void
{
this.transform.matrix3D.appendRotation(_rotationSpeed, Vector3D.Y_AXIS);
}
} //end Class
} //end Package