Flash教程:用AS3代码制作躲避碰撞的盒子,本例介绍运用Flash的AS3代码制作躲避碰撞的盒子效果,译者详细解释了代码的运用,喜欢本效果的朋友可以到论坛提交作业~~
【 tulaoshi.com - Flash 】
本文由 中国 sanbos 编译,转载请保留此信息! 
本系列Flash教程由中国Flash互助课堂专为Flash新手制作,更多教程和练习请点击这里,在这里有系列的教程、练习,并有老师对练习进行点评与指导,欢迎朋友们的光临!
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/flash/)
效果演示:(请用鼠标点击小球观看效果)
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/flash/)在这个Actionscript 3教程中,我将为你展示怎样创建一个躲避被一个球碰撞的盒子。 看上面的效果(单击那白色的球使它运动).让我们马上开始吧!
设置环境
1.创建一个新的Flash Actionscript 3影片 (340x200). 
2.在舞台上画一个矩形.设置大小为20x20. 
3.将矩形转换为MC.给它取一个你喜欢的名字,将注册点移到中心! 
4.设连接属性中MC的类名为"Box".如果你对MC连接属性感到陌生的话,请查看 Actionscript 3 扩展类教程. 
5.现在在舞台上画一个10x10的园. 
6.将园转换为MC.给它取一个你喜欢的名字,将注册点移到中心! 
7.设连接属性中MC的类名为"Ball". 
8.从舞台上删除园和矩形. 
进入 Actionsctipt9. 在第一帧输入下列Actionscript代码. 
//这个数组包含所有的盒子 
var boxes:Array = new Array(); 
//设置球的速度 
var ballSpeed:Number = -4; 
//循环添加8个盒子到舞台 
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 { 
//在整个动画过程中添加ENTER_FRAME 
addEventListener(Event.ENTER_FRAME, enterFrameHandler); 
} 
//在每一帧这个函数被调用 
function enterFrameHandler(e:Event):void { 
//将球左移2像素 
ball.x += ballSpeed; 
for (var i = 0; i < boxes.length; i++) { 
//从数组获得一个盒子 
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) { 
//反转速度 
ballSpeed *= (-1); 
} 
} 
} 
测试你的影片,我希望你能从这里学到一些新的东西。记住如果你有任何问题,请毫不犹豫地到论坛提问。
下面附代码供学习研究。
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); 
} 
} 
}
来源:http://www.tulaoshi.com/n/20160129/1485859.html
看过《Flash教程:用AS3代码制作躲避碰撞的盒子》的人还看了以下文章 更多>>