Pay for Hesitation: arrow rotation

Pages

2008年3月21日 星期五

arrow rotation

這邊有個arrow rotation的範例,簡單易懂。
http://imlab.cs.ncuc.edu.tw/~pie/RotateArrow.swf

  1. package {
  2. import flash.display.Sprite;
  3. public class Arrow extends Sprite {
  4. public function Arrow() {
  5. init();
  6. }
  7. public function init():void {
  8. graphics.lineStyle(1, 0, 1);
  9. graphics.beginFill(0xffff00);
  10. graphics.moveTo(-50, -25);
  11. graphics.lineTo(0, -25);
  12. graphics.lineTo(0, -50);
  13. graphics.lineTo(50, 0);
  14. graphics.lineTo(0, 50);
  15. graphics.lineTo(0, 25);
  16. graphics.lineTo(-50, 25);
  17. graphics.lineTo(-50, -25);
  18. graphics.endFill();
  19. }
  20. }
  21. }
  1. package {
  2. import flash.display.Sprite;
  3. import flash.events.Event;
  4. public class RotateToMouse extends Sprite {
  5. private var arrow:Arrow;
  6. public function RotateToMouse() {
  7. init();
  8. }
  9. private function init():void {
  10. arrow = new Arrow();
  11. addChild(arrow);
  12. arrow.x = stage.stageWidth / 2;
  13. arrow.y = stage.stageHeight / 2;
  14. addEventListener(Event.ENTER_FRAME, onEnterFrame);
  15. }
  16. public function onEnterFrame(event:Event):void {
  17. var dx:Number = mouseX - arrow.x;
  18. var dy:Number = mouseY - arrow.y;
  19. var radians:Number = Math.atan2(dy, dx);
  20. arrow.rotation = radians * 180 / Math.PI;
  21. }
  22. }
  23. }

沒有留言: