萬盛學電腦網

 萬盛學電腦網 >> 圖文處理 >> Flash教程 >> Flash AS3制作交互式3D旋轉動畫效果

Flash AS3制作交互式3D旋轉動畫效果

   效果演示:

Flash AS3制作交互式3D旋轉動畫效果  三聯

  使圖標具有交互性:

  當鼠標單擊某個圖標時使之旋轉,並讓圖標顯示在屏幕的最前面。

  //定義旋轉的最終角度

  var endAngle:Number = 90;

  //保存已經旋轉的角度

  var tempAngle:Number = 0;

  //保存旋轉的狀態

  var isRotating:Boolean = true;

  //注冊圖標的單擊事件

  mc.addEventListener(MouseEvent.MOUSE_DOWN,startRotation);

  //定義偵聽器函數

  function startRotation(e:MouseEvent):void

  {

  //得到圖標

  var currentIcon::MovieClip = e.target;

  //得到圖標的角度

  enaAngle = atan2D(currentIcon.y,cosD(currentIcon.angle) * disy);

  //計算旋轉的度數

  endAngle = (endAngle > -180&&endAngle < -90) ? - 270 - endAngle:90 - endAngle;

  //單擊後旋轉,需要注冊enterFrame事件偵聽器

  menu.addEventListener(Event.ENTER_FRAME,moveMenu);

  }

  每次旋轉前要重新初始化旋轉角度

  function initAngle(b:Boolean)

  {

  //如果處於旋轉狀態保存角度

  if (isRotating)

  //保存圖標角度

  tempAngle += speed;

  //旋轉速度重新設置為0

  speed = 0;

  //設置旋轉狀態

  isRotating = b;

  }

  修改函數moveMenu():

  function moveMenu(e:Event):void

  {

  var iconCount:int = menu,numChildren;

  var depthArrat:Array = new Array();

  var angle:Number = 360 / iconCount;

  for (var z:int; z < iconCount; z++)

  {

  var mc:MovieClip = menu.getChildAt(z);

  mc.gotoAndStop(z + 1);

  //把圖標角度保存在mc的動態屬性中

  mc.angle = tempAngle + speed + angle * z;

  mc.x = cosD(mc.angle) * disy;

  mc.y = sinD(mc.angle) * disy;

  depthArray[z] = mc;

  setProp(mc, "alpha");

  setProp(mc, "scaleX", .2, .7);

  setProp(mc, "scaleY", .2, .7);

  }

  arrange(depthArray);

  //利用緩沖公式把圖標旋轉到endAngle角度

  speed += (endAngle - speed) * .2;

  if (Math.abs(speed - endAngle) < 1)

  {

  //移除事件偵聽器

  menu.removeEventListener(Event.ENTER_FRAME,moveMenu);

  initAngle(false);

  }

  }

  下面給出完整代碼

  include "Math2.as"

  stage.frameRate = 30;

  var disx:Number = 200;

  var disy:Number = 10;

  var speed:Number = 0;

  var endAngle:Number = 90;

  var tempAngle:Number = 0;

  var isRotating:Boolean = true;

  var menu:Sprite = new Sprite();

  menu.x = 300;

  menu.y = 200;

  menu.addEventListener(Event.ENTER_FRAME,moveMenu);

  this.addChild(menu);

  initMenu(5);

  function initMenu(n:int) {

  for (var i:int; i

  var mc:MovieClip = new IconMenu();

  mc.scaleX = mc.scaleY = .5;

  mc.addEventListener(MouseEvent.MOUSE_DOWN,startRotation);

  menu.addChild(mc);

  }

  }

  function initAngle(b:Boolean) {

  if (isRotating) {

  tempAngle += speed;

  }

  speed = 0;

  isRotating = b;

  }

  function startRotation(e:MouseEvent):void {

  var currentIcon:MovieClip = e.target;

  endAngle = atan2D(currentIcon.y,cosD(currentIcon.angle)*disy);

  endAngle = (endAngle> -180&&endAngle < -90)? -270 - endAngle:90 - endAngle;

  initAngle(true);

  menu.addEventListener(Event.ENTER_FRAME,moveMenu);

  }

  function moveMenu(e:Event):void {

  var iconCount:int = menu.numChildren;

  var depthArray:Array = new Array();

  var angle:Number = 360 / iconCount;

  for (var z:int; z

  var mc:MovieClip = menu.getChildAt(z);

  mc.gotoAndStop(z + 1);

  mc.angle = tempAngle + speed+angle * z;

  mc.x = cosD(mc.angle) * disx;

  mc.y = sinD(mc.angle) * disy;

  depthArray[z] = mc;

  setProp(mc,"alpha");

  setProp(mc,"scaleX",.2,.7);

  setProp(mc,"scaleY",.2,.7);

  }

  arrange(depthArray);

  speed += (endAngle-speed) * .2;

  if (Math.abs(speed - endAngle) < 1) {

  menu.removeEventListener(Event.ENTER_FRAME,moveMenu);

  initAngle(false);

  }

  }

  function arrange(depthArray:Array):void {

  depthArray.sortOn("y", Array.NUMERIC);

  var i:int = depthArray.length;

  while (i--) {

  menu.setChildIndex(depthArray[i], i);

  }

  }

  function setProp(mc:MovieClip,prop:String,n1:Number = .5, n2:Number = 1):void {

  mc[prop] = ((mc.y + 2 * disy) / disy - 1) / 2 * (n2 - n1) + n1;

  }

copyright © 萬盛學電腦網 all rights reserved