一句注释都没写:
// ActionScript 1.0 & 2.0 :
- var target_array = [a_mc, b_mc, c_mc];
- var to_where_obj = {};
- var initial_position_obj = {};
- var destination_array = [x1_mc, x2_mc, x3_mc];
- var is_occupied_obj = {};
- for (var i = 0; i < target_array.length; i++) {
- var _mc = target_array[i];
- initial_position_obj[_mc._name] = {_x:_mc._x, _y:_mc._y};
- _mc.onPress = function()
- {
- delete is_occupied_obj[to_where_obj[this._name]];
- delete to_where_obj[this._name];
- this.startDrag();
- this.onMouseMove = function()
- {
- updateAfterEvent();
- };
- };
- _mc.onRelease = _mc.onReleaseOutside = function ()
- {
- var is_hit;
- for (var i = 0; i < destination_array.length; i++) {
- var _mc = destination_array[i];
- if (!is_occupied_obj[_mc._name] && this.hitTest(_mc)) {
- is_occupied_obj[_mc._name] = this;
- to_where_obj[this._name] = _mc._name;
- this._x = _mc._x;
- this._y = _mc._y;
- is_hit = true;
- }
- }
- if (!is_hit) {
- this._x = initial_position_obj[this._name]._x;
- this._y = initial_position_obj[this._name]._y;
- }
- stopDrag();
- delete this.onMouseMove;
- };
- }
// ActionScript 3.0 :
- var target_array:Array = [a_mc, b_mc, c_mc];
- var to_where_obj:Object = new Object();
- var initial_position_obj:Object = new Object();
- var destination_array:Array = [x1_mc, x2_mc, x3_mc];
- var is_occupied_obj:Object = new Object();
- for (var i:Number = 0; i < target_array.length; i++) {
- var _mc = target_array[i];
- initial_position_obj[_mc.name] = {x:_mc.x, y:_mc.y};
- _mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
- _mc.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
- _mc.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
- }
- function mouseDown(event:MouseEvent):void
- {
- var _mc = event.target;
- delete is_occupied_obj[to_where_obj[_mc.name]];
- delete to_where_obj[_mc.name];
- _mc.startDrag();
- }
- function mouseReleased(event:MouseEvent):void
- {
- var is_hit:Boolean = false;
- var des_mc:MovieClip = new MovieClip();
- var tar_mc = event.target;
- tar_mc.stopDrag();
- for (var i:Number = 0; i < destination_array.length; i++) {
- des_mc = destination_array[i];
- if (!is_occupied_obj[des_mc.name] && tar_mc.hitTestObject(des_mc)) {
- is_occupied_obj[des_mc.name] = tar_mc;
- to_where_obj[tar_mc.name] = des_mc.name;
- tar_mc.x = des_mc.x;
- tar_mc.y = des_mc.y;
- is_hit = true;
- }
- }
- if (!is_hit) {
- tar_mc.x = initial_position_obj[tar_mc.name].x;
- tar_mc.y = initial_position_obj[tar_mc.name].y;
- }
- }
- function mouseMoveHandler(event:MouseEvent):void
- {
- event.updateAfterEvent();
- }
enjoy!
源文件下载download