在 Flash 中,可以利用設定 rotation 屬性,使物件達到旋轉效果,但如果需要使物件隨著滑鼠

旋轉,就得使用 radians to degrees 的公式幫我們算出目前的 degrees,並將求得值設

給物件的 rotation 屬性。

 

radians to degrees 公式:

degrees = radians * 180/Math.PI;

 

Flash 程式:

 

 

程式碼:

 

 

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    
    [SWF(width=300,height=300,backgroundColor="#000000")]
    public class Main extends Sprite 
    {                        
        private var angle:Number=0;        
        private var square:Sprite = new Sprite();
        private var circle:Sprite = new Sprite();
        public function Main():void 
        {    
            //畫圓
            circle.graphics.beginFill(0x558FF0);
            circle.graphics.drawCircle(0, 0, 100);
            circle.x = 150;
            circle.y = 150;
            addChild(circle);    
            
            //畫指針
            square.graphics.beginFill(0xFFCC00);            
            square.graphics.drawRect(-5, -5, 100, 10);                        
            square.x = 150;
            square.y = 150;            
            addChild(square);
                        
            
            addEventListener(Event.ENTER_FRAME,go);
            
        }
        
        private function go(evt:Event):void{
    
            var x:Number = mouseX;
            var y:Number = mouseY;
            
            //求得轉動角度
            angle = Math.atan2(y - square.y , x - square.x) * (180 / Math.PI);            
            //將求得值傳給指針 rotation 屬性
            square.rotation = angle;    
            
        }        
            
        
    }
    
}

 

 

檔案下載:rotation.zip

參考網站:

http://www.seinia.com/tutorials/TrigonometryInFlashPart1/

http://www.foundation-flash.com/tutorials/rotation/

iammic 發表在 痞客邦 PIXNET 留言(0) 人氣()