請依照下列步驟進行:
(一) 練習檔案預備
1. 請下載已初步完成之練習檔 [fish_moving.zip]
2. 解壓後在 Flash CS5 中開啟
(二) 安排主場景畫面
1. 背景
(三) 加入魚的圖層
1. 增加圖層
2. 置入魚
3. 設定實體名稱『fish』
(四) 撰寫程式碼
1. 增加圖層『Scripts』
2. 進入程式編寫 (按F9)
3. 程式碼如下:
=============================================
var goalX:Number;
var goalY:Number;
goalX = -20 + Math.random() * 640;
goalY = -20 + Math.random() * 340;
addEventListener(Event.ENTER_FRAME, moving);
function moving(Event)
{
fish.x += (goalX-fish.x) / 20;
fish.y += (goalY-fish.y) / 20;
if(fish.x>=goalX-10 && fish.x<=goalX+10 && fish.y >=goalY-10 && fish.y <=goalY+10)
{
goalX = -20 + Math.random() * 640;
goalY = -20 + Math.random() * 340;
}
if(goalX>=fish.x)
{
fish.scaleX = 1;
}
else
{
fish.scaleX = -1;
}
}
=============================================
(五) 音樂的程式碼
1. 確認外部聲音檔『music.mp3』
2. 加入程式碼
=============================================
var mySound:Sound = new Sound();
var myLink:URLRequest = new URLRequest("music.mp3");
mySound.load(myLink);
mySound.play();
=============================================
(六) 挑戰─增加第二隻魚 (提示)
1. 增加圖層、增加魚
2. 設定魚的實體名稱『fish2』
3. 修改程式,增加 fish2 的相關語法 (如下)
=============================================
var mySound:Sound = new Sound();
var myLink:URLRequest = new URLRequest("music.mp3");
mySound.load(myLink);
mySound.play();
var goalX:Number;
var goalY:Number;
goalX = -20 + Math.random() * 640;
goalY = -20 + Math.random() * 340;
var goalX2:Number;
var goalY2:Number;
goalX2 = -20 + Math.random() * 640;
goalY2 = -20 + Math.random() * 340;
addEventListener(Event.ENTER_FRAME, moving);
function moving(Event)
{
fish.x += (goalX-fish.x) / 20;
fish.y += (goalY-fish.y) / 20;
if(fish.x>=goalX-10 && fish.x<=goalX+10 && fish.y>=goalY-10 && fish.y<=goalY+10)
{
goalX = -20 + Math.random() * 640;
goalY = -20 + Math.random() * 340;
}
if(goalX>=fish.x)
{
fish.scaleX = 1;
}
else
{
fish.scaleX = -1;
}
// fish 2
fish2.x += (goalX2-fish2.x) / 12;
fish2.y += (goalY2-fish2.y) / 12;
if(fish2.x>=goalX2-10 && fish2.x<=goalX2+10 && fish2.y>=goalY2-10 && fish2.y<=goalY2+10)
{
goalX2 = -20 + Math.random() * 640;
goalY2 = -20 + Math.random() * 340;
}
if(goalX2>=fish2.x)
{
fish2.scaleX = -1;
}
else
{
fish2.scaleX = 1;
}
}
=============================================
教材參考:Flash ActionScript 3.0 範例應用 20 例 (松崗)
教材參考:Flash ActionScript 3.0 範例應用 20 例 (松崗)
張貼留言