Flash AS3代碼繪制旋轉(zhuǎn)的3D立體菜單動(dòng)畫效果
在這篇教程里,我們將學(xué)會(huì)如何利用Flash AS3代碼繪制旋轉(zhuǎn)的3D立體菜單動(dòng)畫效果,教木馬將會(huì)根據(jù)鼠標(biāo)決定旋轉(zhuǎn)速度。教程繪制效果非常酷,轉(zhuǎn)發(fā)過來,感興趣的朋友快點(diǎn)來學(xué)習(xí)吧。
演示:
繪制步驟:
1、新建Flash文件,設(shè)置寬、高屬性為 550 × 400 。
2、用圓角矩形工具,畫一個(gè) 158 × 35的長方形。筆觸為8白色,填充色#0 F7E 88。圖1:
3、將長方形轉(zhuǎn)換成名為 Menu Item 的影片剪輯。設(shè)定注冊(cè)點(diǎn)為中心。圖2:
4、雙擊舞臺(tái)上的影片剪輯,進(jìn)入編輯狀態(tài)。創(chuàng)建動(dòng)態(tài)文本,在它里面輸入需要的本文。圖3
5、在屬性面板中輸入實(shí)例名字 menuItemText 。
6、按下字符嵌入按鈕,插入下列字型。圖4:
7、切換回主場景1,刪除舞臺(tái)上的影片剪輯,實(shí)例將由代碼生成。
8、打開庫元件面板,右鍵單擊影片剪輯,(CS3選鏈接、CS4選屬性)給元件添加一個(gè)綁定類。類名 MenuItem 。圖5:
9、選中第1幀,打開動(dòng)作面板輸入代碼:
const NUMBER_OF_ITEMS:uint = 20;
//This array will contain all the menu items
var menuItems:Array = new Array();
//Set the focal length
var focalLength:Number = 350;
//Set the vanishing point
var vanishingPointX:Number = stage.stageWidth / 2;
var vanishingPointY:Number = stage.stageHeight / 2;
//We calculate the angleSpeed in the ENTER_FRAME listener
var angleSpeed:Number = 0;
//Radius of the circle
var radius:Number = 128;
//Calculate the angle difference between the menu items (in radians)
var angleDifference:Number = Math.PI * (360 / NUMBER_OF_ITEMS) / 180;
//This loop creates and positions the carousel items
for (var i:uint = 0; i < NUMBER_OF_ITEMS; i++) {
//Create a new menu item
var menuItem:MenuItem = new MenuItem();
//Calculate the starting angle for the menu item
var startingAngle:Number = angleDifference * i;
//Set a currentAngle attribute for the menu item
menuItem.currentAngle = startingAngle;
//Position the menu item
menuItem.xpos3D = - radius * Math.cos(menuItem.currentAngle) * 0.5;
menuItem.ypos3D = radius * Math.sin(startingAngle);
menuItem.zpos3D = radius * Math.cos(startingAngle);
//Calculate the scale ratio for the menu item (the further the item -> the smaller the scale ratio)
var scaleRatio = focalLength/(focalLength + menuItem.zpos3D);
//Scale the menu item according to the scale ratio
menuItem.scaleX = menuItem.scaleY = scaleRatio;
//Position the menu item to the stage (from 3D to 2D coordinates)
menuItem.x = vanishingPointX + menuItem.xpos3D * scaleRatio;
menuItem.y = vanishingPointY + menuItem.ypos3D * scaleRatio;
//Assign an initial alpha
menuItem.alpha = 0.3;
//Add a text to the menu item
menuItem.menuItemText.text = Menu item + i;
//We don’t want the text field to catch mouse events
menuItem.mouseChildren = false;
//Assign MOUSE_OVER, MOUSE_OUT and CLICK listeners for the menu item
menuItem.addEventListener(MouseEvent.MOUSE_OVER, mouseOverItem);
menuItem.addEventListener(MouseEvent.MOUSE_OUT, mouseOutItem);
menuItem.addEventListener(MouseEvent.CLICK, itemClicked);
//Add the menu item to the menu items array
menuItems.push(menuItem);
//Add the menu item to the stage
addChild(menuItem);
}
//Add an ENTER_FRAME listener for the animation
addEventListener(Event.ENTER_FRAME, moveCarousel);
//This function is called in each frame
function moveCarousel(e:Event):void {
//Calculate the angle speed according to mouseY position
angleSpeed = (mouseY - stage.stageHeight / 2) * 0.0002;
//Loop through the menu items
for (var i:uint = 0; i < NUMBER_OF_ITEMS; i++) {
//Store the menu item to a local variable
var menuItem:MenuItem = (MenuItem)(menuItems[i]);
//Update the current angle of the item
menuItem.currentAngle += angleSpeed;
//Calculate a scale ratio
var scaleRatio = focalLength/(focalLength + menuItem.zpos3D);
//Scale the item according to the scale ratio
menuItem.scaleX=menuItem.scaleY=scaleRatio;
//Set new 3D coordinates
menuItem.xpos3D=- radius*Math.cos(menuItem.currentAngle)*0.5;
menuItem.ypos3D=radius*Math.sin(menuItem.currentAngle);
menuItem.zpos3D=radius*Math.cos(menuItem.currentAngle);
//Update the item’s coordinates.
menuItem.x=vanishingPointX+menuItem.xpos3D*scaleRatio;
menuItem.y=vanishingPointY+menuItem.ypos3D*scaleRatio;
}
//Call the function that sorts the items so they overlap each other correctly
sortZ();
}
//This function sorts the items so they overlap each other correctly
function sortZ():void {
//Sort the array so that the item which has the highest
//z position (= furthest away) is first in the array
menuItems.sortOn(zpos3D, Array.NUMERIC | Array.DESCENDING);
//Set new child indexes for the images
for (var i:uint = 0; i < NUMBER_OF_ITEMS; i++) {
setChildIndex(menuItems[i], i);
}
}
//This function is called when a mouse is over an item
function mouseOverItem(e:Event):void {
//Change the alpha to 1
e.target.alpha=1;
}
//This function is called when a mouse is out of an item
function mouseOutItem(e:Event):void {
//Change the alpha to 1
e.target.alpha=0.3;
}
//This function is called when an item is clicked
function itemClicked(e:Event):void {
trace(Item clicked! Add your own logic here.);
}
10、完成,測試你的影片
教程結(jié)束,以上就是利用Flash AS3代碼繪制旋轉(zhuǎn)的3D立體菜單動(dòng)畫效果過程,希望對(duì)大家有所幫助!
相關(guān)文章

flash cs6鼠標(biāo)跟隨效果實(shí)現(xiàn)代碼分享
flash cs6想要實(shí)現(xiàn)鼠標(biāo)跟隨效果?該怎么制作呢?今天我們就來看看使用as2.0實(shí)現(xiàn)鼠標(biāo)跟隨效果的教程,需要的朋友可以參考下2019-05-19
Flash cs6怎么使用代碼輸入中英文文本?Flash cs6中可以使用文字工具直接輸入文本,也可以使用代碼來輸入文本,該怎么使用代碼輸入文本呢?請(qǐng)看下文詳細(xì)的教程,需要的朋友2018-03-11
flash as3.0抽象類怎么定義? as3.0中有很多抽象類,該怎么定義抽象類和抽象方法呢?下面我們就來看看簡單的例子,需要的朋友可以參考下http://www.fzitv.net/softs/408402.2018-02-28
flash cs6中怎么使用ActionScript3.0?
flash cs6中怎么使用ActionScript3.0?flash cs6中想要使用ActionScript3.0功能,該怎么使用呢?下面我們就來看看詳細(xì)的教程,需要的朋友可以參考下2018-01-25
Flash中怎么實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊決定圖像位置?
本教程給大家分享一個(gè)Flash小教程,教大家在Flash CS6中怎么實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊決定圖像位置?方法很簡單,感興趣的朋友歡迎前來一起分享學(xué)習(xí)2018-01-12
Flash中如何用代碼將圖片放在自己想要的舞臺(tái)位置?
本教程教腳本之家的ActionScript教程學(xué)習(xí)者在Flash中如何用代碼將圖片放在自己想要的舞臺(tái)位置,教程講解的詳細(xì),感興趣的朋友歡迎前來分享學(xué)習(xí)2017-11-20
在Flash CS6中使用with函數(shù)繪制背景圖教程
本教程教腳本之家的ActionScript教程學(xué)習(xí)者如何在Flash CS6中使用with函數(shù)繪制背景圖?教程一步步講解的挺詳細(xì),方法也不難,非常適合Flash新手入門學(xué)習(xí)2017-11-18
Flash怎么設(shè)置元件坐標(biāo)?flash使用代碼設(shè)置元件的坐標(biāo)的教程
Flash怎么設(shè)置元件坐標(biāo)?flash中導(dǎo)如的元件需要添加坐標(biāo),該怎么定位元件坐標(biāo)呢?下面我們就來看看flash使用代碼設(shè)置元件的坐標(biāo)的教程,需要的朋友可以參考下2017-10-11
Flash怎么制作來回?fù)u擺的花朵的動(dòng)畫?
Flash怎么制作來回?fù)u擺的花朵的動(dòng)畫?Flash中想要給花朵制作一段搖擺的動(dòng)畫效果,該怎么制作呢?下面我們就來看看詳細(xì)的教程,很簡單,需要的朋友可以參考下2017-05-23
Flash怎么制作流動(dòng)七彩色的文字?想要讓文字動(dòng)起來,該怎么使用flash給文字制作一個(gè)流動(dòng)七彩色的動(dòng)畫呢?下面我們就來看看詳細(xì)的教程,需要的朋友可以參考下2017-04-23











