Flash 绕中心点旋转

package tranbmp

{

import flash.events.Event;  

import flash.geom.Matrix;  

import flash.geom.Point;  

import flash.display.*;  

public class TestR extends Sprite

{

public function TestR():void   

{  

if (stage) init();  

else addEventListener(Event.ADDED_TO_STAGE, init);  

}  

private function init(e:Event = null):void   

{  

removeEventListener(Event.ADDED_TO_STAGE, init);  

// entry point  

var s1:Shape = new Shape();  

s1.graphics.lineStyle(0);  

s1.graphics.beginFill(0);  

s1.graphics.drawRect(0, 0, 100, 100);  

s1.graphics.endFill();  

s1.x = 200;  

s1.y = 200;  

addChild(s1);  

var s2:Shape = new Shape();  

s2.graphics.lineStyle(0);  

s2.graphics.beginFill(0xff0000);  

s2.graphics.drawRect(0, 0, 100, 100);  

s2.graphics.endFill();            

s2.x = s1.x;  

s2.y = s1.y;  

addChild(s2);  

var angle:Number = 45;  

centerRotate(s2, angle);  

centerRotate(s1, 10);  

centerRotate(s2, 60);  

}  

/** 

* 绕中心点旋转 

*  

* @param   mc  元件 

* @param   angle   角度 

*/  

public function centerRotate(mc:DisplayObject,angle:Number):void   

{         

var currentRotation:Number = mc.rotation;  

//获取mc不旋转时候的尺寸  

mc.rotation = 0;  

var mcWidth:Number = mc.width;  

var mcHeight:Number = mc.height;  

mc.rotation = currentRotation;  

//获取mc当前中心点坐标  

var pointO:Point = mc.localToGlobal(new Point(mcWidth / 2, mcHeight / 2));  

//旋转mc  

mc.rotation = angle;  

//获取mc旋转后中心点坐标  

var pointO2:Point = mc.localToGlobal(new Point(mcWidth / 2, mcHeight / 2));  

//平移到原来中心点O  

var p3:Point = pointO.subtract(pointO2);  

var matrix:Matrix = mc.transform.matrix;  

matrix.translate(p3.x, p3.y);  

mc.transform.matrix = matrix;  

}  

}  

}  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值