一种罗嗦但终极的做法:
检测图形边界:检测图形边界的思路见:
http://space.flash8.net/space/?284926/action_viewspace_itemid_372562.html
如下是具体做法:(没完善,功能基本有,碰到边界反弹,虽然反弹不是很好!拖动不要太快!所设执行时间已经很快了,但拖动快还是会有出边想象!)
as:
var xy_array:Array = [];
var _array:Array = [];
var interval:Number;
for (y=_mc._y; y<_mc._height+_mc._y; y++) {
for (x=_mc._x-1; x<_mc._width+_mc._x; x++) {
if (_mc.hitTest(x, y, true) && !_mc.hitTest(x+1, y, true)) {
xy_array.push({x:x, y:y});
} else if (_mc.hitTest(x, y, true) && !_mc.hitTest(x-1, y, true)) {
xy_array.push({x:x, y:y});
}
if (_mc.hitTest(x, y, true) && !_mc.hitTest(x, y+1, true)) {
xy_array.push({x:x, y:y});
} else if (_mc.hitTest(x, y, true) && !_mc.hitTest(x, y-1, true)) {
xy_array.push({x:x, y:y});
}
}
}
y_mc.onPress = function() {
this.startDrag();
};
y_mc.onMouseUp = function() {
this.stopDrag();
};
function pd() {
for (i=0; i<xy_array.length; i++) {
if (y_mc.hitTest(xy_array
.x, xy_array.y, true)) {
y_mc.stopDrag();
//
y_mc._x += (y_mc._width/2)*(250-y_mc._x)/Math.abs(250-y_mc._x);
y_mc._y += (y_mc._width/2)*(175-y_mc._y)/Math.abs(175-y_mc._y);
}
}
}
interval = setInterval(_root, "pd", 0.0001);
未命名-2.fla (336 KB)