Movieclip.prototype.col = function(w, h, d){
var c=0;
var xs=0;
var ys=0;
var diff=0;
var hitFloorX=0;
var hitWallY=0;
//对所有在perim影片剪辑中的refer剪辑
for (i in this.perim) {
//将p.x和p.y设置成refer的x,y坐标
this.perim.localToGlobal(p={x:this.perim[i]._x,y:this.perim[i]._y})
//如果不在舞台内,而在舞台的下方
if (p.y > h) {
//当refer碰撞时设置其x坐标为hitFloorX
hitFloorX = p.x
//将影片剪辑放回舞台内。
this._y -= p.y - h;
}
//如果不在舞台内,而在舞台的右方
if (p.x > w) {
将refer碰撞墙壁点设为hitWallY
hitWallY = p.y;
//dy<0意味着剪辑正在缓慢的向上移动,剪辑向上运动一半
if (this.dy < 0) this.dy *= d;
//弹回来
this._x -= (p.x - w)*3;
}
//对到舞台的左面进行同样的处理。
if (p.x < 0) {
hitWallY = p.y;
if (this.dy < 0) this.dy *= d;
this._x -= p.x*3;
}
ys += p.y;
xs += p.x;
c++; //refer的数量
}
//如果撞到了地面
if(hitFloorX){
//diff=碰撞的中心偏移量
diff = (xs/c) - hitFloorX
this.r=(this.r + diff*(this.dy/350))/1.2
this.dy *= -1.4 * d;
//如果物体在旋转,则也在其旋转的方向上移动,
//相对于旋转量的移动量
this.dx *= d;
this.dx += this.r*2;
}
//如果碰撞了墙壁
if (hitWallY) {
diff = (ys/c) - hitWallY;
this.dx *= -1 * d;
}
}
ASSetPropFlags(MovieClip.prototype, "col", 1);
_global.gcGravity = 15;
_global.gcDamping = 0.5;
_global.gcStageHeight = 300;
_global.gcStageWidth = 400;
aClips = ["mclemon"];
for (i in aClips) {
thisClip = this[aClips[i]];
thisClip._x = random(gcStageWidth);
thisClip._y = random(gcStageHeight);
thisClip.onPress = function() {
this.r = 0;
this.startDrag();
this.oX = _root._xmouse;
this.pr = true;
};
thisClip.onRelease = thisClip.onReleaseOutside = function() {
this.stopDrag();
this.pr = false;
};
thisClip.onEnterFrame = function() {
this.col(gcStageWidth, gcStageHeight, gcDamping);
//ox是剪辑在前一帧中的X坐标
this.ox = this.x;
this.oy = this.y;
this.x = this._x;
this.y = this._y;
//如果单击了物体,则根据它在前一帧的位置来设置其在X轴方向的差值,
if (this.pr) {
this.dx = (this.x-this.ox)*7;
this.dy = (this.y-this.oy)*7;
} else {
//如果释放鼠标,由于重力的影响要加上dy的值
this.dy += gcGravity;
this.x += this.dx/10;
this.y += this.dy/10;
this._x = this.x;
this._y = this.y;
}
this._rotation += this.r;
//放慢物体的旋转
this.r *= .95;
}
}