Hello. I’m trying to rotate a simple green square bitmap. I’ve gone over and over this code. The square bitmap rotates but it doesn’t appear to be rotating correctly. It appears to be offset from center. I’m not exactly sure what the issue is. Maybe some of the gurus could take a peek and spot a code error or shed some light on an obscure bug.
package
{
import adobe.utils.CustomActions;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.setTimeout;
/**
* ...
* @author Roger Allen Avery
*/
public class Main extends Sprite
{
//[Embed(source = '../lib/1_32.png')]
//private var _planetPNG:Class;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
//private var planet:Bitmap;
private var square:Sprite;
private var bmd_square:BitmapData;
private var bitmap:Bitmap;
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
//planet = new _planetPNG();
//planet.x = stage.stageWidth >> 1;
//planet.y = stage.stageHeight >> 1;
//addChild(planet);
square = new Sprite();
square.graphics.beginFill(0x00ff00);
square.graphics.drawRoundRect(0, 0, 100, 100, 30);
square.graphics.endFill();
bmd_square = new BitmapData(100, 100, true, 0x00000000);
bmd_square.draw(square);
//var m:Matrix = new Matrix();
//m.translate( -50, -50);
//m.rotate(2);
//m.translate(50, 50);
//
//var bmd:BitmapData = new BitmapData(100, 100, true, 0x00000000);
//bmd.draw(bmd_square, m, null, null, null, true);
//
//bmd_square = bmd;
bitmap = new Bitmap(bmd_square);
bitmap.x = (stage.stageWidth >> 1) - bitmap.width/2;
bitmap.y = (stage.stageHeight >> 1) - bitmap.width / 2;
addChild(bitmap);
addEventListener(Event.ENTER_FRAME, render);
}
private var rot:Number = 0;
private function render(e:Event):void {
rot = rot < 360?rot+0.01:0;
//var dx:Number = mouseX - square.x + square.width/2;
//var dy:Number = mouseY - square.y + square.height/2;
var radians:Number = Math.PI * 2 * (rot / 360);//Math.atan2(dy, dx);
//var angle:Number = radians * 180 / Math.PI;
var m:Matrix = new Matrix();// new Matrix(1, 0, 0, 1, -16, -16);
m.translate( -50,-50);
m.rotate(radians);
m.translate(50,50);
var bmd:BitmapData = new BitmapData(100,100, true, 0x00000000);
bmd.draw(bitmap.bitmapData,m, null, null, null, true);
bitmap.bitmapData = bmd;
}
}
}