欧美日韩精品在线,国内精品久久久久久久久,一级毛片恃级毛片直播,清纯唯美亚洲综合欧美色

Flash教程:用AS3代碼制作躲避碰撞的盒子_Flash教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:Flash AS教程:用動態(tài)遮罩實(shí)現(xiàn)液化效果
簡介:本例介紹運(yùn)用Flash AS的動態(tài)遮罩來實(shí)現(xiàn)液化效果,教程詳細(xì)講解了AS代碼的運(yùn)用,并給出了詳細(xì)的代碼解釋,希望能給朋友們帶來幫助~~本文由中國教程網(wǎng) sanbos 原創(chuàng),轉(zhuǎn)載請保留此信息!

簡介:本例介紹運(yùn)用Flash的AS3代碼制作躲避碰撞的盒子效果,譯者詳細(xì)解釋了代碼的運(yùn)用,喜歡本效果的朋友可以到論壇提交作業(yè)~~

效果演示:(請用鼠標(biāo)點(diǎn)擊小球觀看效果)

在這個(gè)Actionscript 3教程中,我將為你展示怎樣創(chuàng)建一個(gè)躲避被一個(gè)球碰撞的盒子。 看上面的效果(單擊那白色的球使它運(yùn)動).讓我們馬上開始吧!

設(shè)置環(huán)境

1.創(chuàng)建一個(gè)新的Flash Actionscript 3影片 (340x200).
2.在舞臺上畫一個(gè)矩形.設(shè)置大小為20x20.
3.將矩形轉(zhuǎn)換為MC.給它取一個(gè)你喜歡的名字,將注冊點(diǎn)移到中心!
4.設(shè)連接屬性中MC的類名為"Box".如果你對MC連接屬性感到陌生的話,請查看 Actionscript 3 擴(kuò)展類教程.
5.現(xiàn)在在舞臺上畫一個(gè)10x10的園.
6.將園轉(zhuǎn)換為MC.給它取一個(gè)你喜歡的名字,將注冊點(diǎn)移到中心!
7.設(shè)連接屬性中MC的類名為"Ball".
8.從舞臺上刪除園和矩形.
進(jìn)入 Actionsctipt9. 在第一幀輸入下列Actionscript代碼.
//這個(gè)數(shù)組包含所有的盒子
var boxes:Array = new Array();
//設(shè)置球的速度
var ballSpeed:Number = -4;
//循環(huán)添加8個(gè)盒子到舞臺
for (var i = 0; i < 9; i ) {
//創(chuàng)建一個(gè)盒子
var box:Box = new Box();
//添加一個(gè)位置
box.y = 150;
box.x = box.width * i * 1.5 40;
//添加一個(gè)盒子到數(shù)組
boxes.push(box);
//在舞臺上添加一個(gè)盒子
addChild(box);
}
//創(chuàng)建一個(gè)盒子并設(shè)置它的右邊
var ball:Ball = new Ball();
ball.x = 320;
ball.y =155;
//使球看起來象按鈕 (手形光標(biāo))
ball.buttonMode = true;
//把球添加到舞臺上
addChild(ball);
//偵聽用戶點(diǎn)擊球的時(shí)候
ball.addEventListener(MouseEvent.CLICK, ballClicked);
//當(dāng)用戶點(diǎn)擊球時(shí)這個(gè)函數(shù)被調(diào)用
function ballClicked(e:Event):void {
//在整個(gè)動畫過程中添加ENTER_FRAME
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
//在每一幀這個(gè)函數(shù)被調(diào)用
function enterFrameHandler(e:Event):void {
//將球左移2像素
ball.x = ballSpeed;
for (var i = 0; i < boxes.length; i ) {
//從數(shù)組獲得一個(gè)盒子
var box:Box = boxes as Box;
//檢測從球到盒子的x距離
var distX:Number = ball.x - box.x;
//球來自右邊
if (distX < 50 && distX > 0 && ballSpeed < 0) {
//把盒子推上去
box.y -= 2;
}
//球離開左邊
else if (distX < 50 && distX < 0 && ballSpeed < 0) {
//如果球沒有在原來的位置則往下落
if (box.y <= 150) {
box.y = 2;
}
}
//球從左邊來
if (distX < 0 && distX > -50 && ballSpeed > 0) {
//往上推盒子
box.y -= 2;
}
//球離開右邊
else if (distX < 50 && distX > 0 && ballSpeed > 0) {
//如果球沒在原來的位置則往下落
if (box.y <= 150) {
box.y = 2;
}
}
//如果球到了左邊則改變方向
//或者右邊邊緣
if (ball.x 5 >stage.stageWidth || ball.x - 5 < 0) {
//反轉(zhuǎn)速度
ballSpeed *= (-1);
}
}
}
測試你的影片,我希望你能從這里學(xué)到一些新的東西。記住如果你有任何問題,請毫不猶豫地到論壇提問。

下面附代碼供學(xué)習(xí)研究。var boxes:Array = new Array();
var ballSpeed:Number = -4;
for (var i = 0; i < 9; i ) {
var box:Box = new Box();
box.y = 150;
box.x = box.width * i * 1.5 40;
boxes.push(box);
addChild(box);
}
var ball:Ball = new Ball();
ball.x = 320;
ball.y =155;
ball.buttonMode = true;
addChild(ball);
ball.addEventListener(MouseEvent.CLICK, ballClicked);
function ballClicked(e:Event):void {
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
function enterFrameHandler(e:Event):void {
ball.x = ballSpeed;
for (var i = 0; i < boxes.length; i ) {
var box:Box = boxes[i];
var distX:Number = ball.x - box.x;
if (distX < 50 && distX > 0 && ballSpeed < 0) {
box.y -= 2;
}
else if (distX < 50 && distX < 0 && ballSpeed < 0) {
if (box.y <= 150) {
box.y = 2;
}
}
if (distX < 0 && distX > -50 && ballSpeed > 0) {
box.y -= 2;
}
else if (distX < 50 && distX > 0 && ballSpeed > 0) {
if (box.y <= 150) {
box.y = 2;
}
}
if (ball.x 5 >stage.stageWidth || ball.x - 5 < 0) {
ballSpeed *= (-1);
}
}
}

分享:Flash AS入門教程第七課:影片剪輯第五節(jié)_拖動與碰撞檢測
本文由中國教程網(wǎng) sanbos 原創(chuàng),轉(zhuǎn)載請保留此信息! 本系列Flash教程由中國教程網(wǎng)Flash互助課堂專為Flash新手制作,更多教程和練習(xí)請點(diǎn)擊這里,在這里有系列的教程、練習(xí),并有老師對練習(xí)

來源:中國教程網(wǎng)//所屬分類:Flash教程/更新時(shí)間:2008-11-26
相關(guān)Flash教程