WEB开发网
开发学院图形图像Flash Flash教程:用AS3代码制作躲避碰撞的盒子 阅读

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

 2008-11-23 19:18:03 来源:WEB开发网   
核心提示: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 + 4

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);
}
}
}

上一页  1 2 

Tags:Flash 教程 AS

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接