Flash 动画:
主场景中建两个mc,分别是 太阳(earth_mc)和月亮(moon_mc)处,样式自己定,实例名必须是 earth_mc 和 moon_mc , 主场景第一帧中写入下面的代码即可。
_root.onEnterFrame = enterRoot;
//为了让大球碰撞小球时同样生效,有此一句。
moon_mc.onPress = function() {
startDrag(this);
//小球开始拖拽。
delete this.onEnterFrame;
//删除掉小球的 onEnterFrame 事件。 初始时,该事件并不存在,flash 可以自动忽略它。
_root.onEnterFrame = enterRoot;
//指定主场景的 onEnterFrame 的事件处理程序。
showText = "请拖动球体进行碰撞";
};
moon_mc.onRelease = function() {
stopDrag();
};
moon_mc.onReleaseOutside = function() {
stopDrag();
};
function checkPSN() {
//球体间的碰撞检测是通过半径之和 与两球间的距离进行比较。
var radius = 125 * 125;
var distance = (moon_mc._x - earth_mc._x) * (moon_mc._x - earth_mc._x) + (moon_mc._y - earth_mc._y) * (moon_mc._y - earth_mc._y);
//不开根号,是为了提高运算速度。
if (distance <= radius) {
return true;
} else {
return false;
}
}
function enterRoot() {
if (checkPSN()) {
delete this.onEnterFrame;
//动态删除掉主场景的 onEnterFrame 事件处理程序。
moon_mc.stopDrag();
//小球停止拖拽。
angle = Math.atan2((moon_mc._y - earth_mc._y), (moon_mc._x - earth_mc._x));
//返回值是弧度,该弧度决定初始的碰撞位置。
moon_mc.onEnterFrame = function() {
angle = (angle + 5 * 3.14159 / 180) % (2 * 3.14159);
//角度每次自加5度。
moon_mc._x = earth_mc._x + 126 * Math.cos(angle);
moon_mc._y = earth_mc._y + 126 * Math.sin(angle);
//根据大圆的坐标来确定小圆旋转到的位置。
};
showText = "请把小球拖开";
}
}
//画蛇添个足,让大圆动起来。
earth_mc.onPress = function() {
startDrag(this);
};
earth_mc.onRelease = function() {
stopDrag();
};
earth_mc.onReleaseOutside = function() {
stopDrag();
};
回复Comments
作者:
{commentrecontent}