模拟球的运动:
代码如下:
//y轴方向的速度
var ymov:Number = 0;
//X轴方向的速度
var xmov:Number = 0;
//重力加速度
var gravity:Number = 0.88;
//复制一个透明的球,让这个球跟随原球,利用两球的距离计算球的速度
duplicateMovieClip("ball", "ball_follow", 1);
ball_follow._alpha = 0;
_root.attachMovie("hand", "Hand", 2);
Hand.startDrag(true);
_root.onEnterFrame = function() {
Mouse.hide();
Shadow._x = ball._x-11;
//用缓冲的方式设置复制的球跟随原球
ball_follow._x += (ball._x-ball_follow._x)*0.2;
ball_follow._y += (ball._y-ball_follow._y)*0.2;
//设置影子的透明度和宽度
Shadow._alpha = 100-(Shadow._y-ball._y);
Shadow._width = 59+(Shadow._y-ball._y);
ball.onPress = function() {
Hand.gotoAndStop(2);
x = ball._x-_xmouse;
y = ball._y-_ymouse;
leave = true;
};
ball.onRelease = function() {
Release();
};
ball.onReleaseOutside = function() {
Release();
};
//设置球的反弹
if (leave) {
ball._x = _xmouse+x;
ball._y = _ymouse+y;
} else {
ymov += gravity;
ball._y += ymov;
ball._x += xmov;
xmov *= 0.98;
}
//设置球与墙碰撞后的结果
if (ball._y>=Shadow._y-ball._height/2) {
ball._y = Shadow._y-ball._height/2;
ymov *= -0.85;
}
if (ball._x<ball._width/2) {
ball._x = ball._width/2;
xmov = -xmov;
}
if (ball._x>800-ball._width/2) {
ball._x = 800-ball._width/2;
xmov = -xmov;
}
};
function Release(){
Hand.gotoAndStop(1);
xmov = (ball._x-ball_follow._x)/3;
ymov = (ball._y-ball_follow._y)/5;
leave = false;
}