/ Published in: ActionScript
CS4
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include "mc_tween2.as" // if you don't have mc_tween, comment it out. var startX:Number = 0; // original X position of item picked up var startY:Number = 0; // original Y position of item picked up var drawCollision:Boolean = true; // for debugging box1_mc.onPress = function() { pickup(this, 1); }; box1_mc.onRelease = function() { dropoff(this, 1, box2_mc); }; //box1_mc.onRollOver = function() { rollover(this, 1); }; //box1_mc.onRollOut = function() { rollout(this, 1); }; function pickup(which_mc:MovieClip, intMC:Number):Void { which_mc.swapDepths(this.getNextHighestDepth()); // bring picked-up object to foreground which_mc.startDrag(); startX = which_mc._x; startY = which_mc._y; } function dropoff(which_mc:MovieClip, intMC:Number, target_mc:MovieClip):Void { which_mc.stopDrag(); if (isColliding(target_mc, which_mc)) // try removing the second parameter here. See function for more details. { status_txt.text += "\ncollided"; which_mc.slideTo(85, undefined, .5); } else { status_txt.text += "\nmissed"; which_mc.slideTo(startX, startY, .5); } } function rollover(which_mc:MovieClip, intMC:Number):Void { status_txt.text += "\nrolled over"; } function rollout(which_mc:MovieClip, intMC:Number):Void { status_txt.text += "\nrolled out"; } function isColliding(target_mc:MovieClip, which_mc:MovieClip):Boolean { // If you do not send a "which_mc" this will assume you want to check on the mouse position (which will probably be within the target when the item is dropped) obj1_x = target_mc._x; obj1_y = target_mc._y; if (which_mc) { obj2_x = which_mc._x - Number(which_mc._width / 2); obj2_y = which_mc._y - Number(which_mc._height / 2); } else { obj2_x = this._xmouse - 1; obj2_y = this._ymouse - 1; } obj1_x2 = target_mc._width + obj1_x; obj1_y2 = target_mc._height + obj1_y; if (which_mc) { obj2_x2 = which_mc._width + obj2_x; obj2_y2 = which_mc._height + obj2_y; } else { obj2_x2 = 2 + this._xmouse; obj2_y2 = 2 + this._ymouse; } if (drawCollision == true) { this.createEmptyMovieClip("rectangle_mc", 10); rectangle_mc._x = obj2_x; rectangle_mc._y = obj2_y; if (which_mc) { drawRectangle(rectangle_mc, which_mc._width, which_mc._height, 0x990000, 50); } else { drawRectangle(rectangle_mc, 2, 2, 0x990000, 50); } this.createEmptyMovieClip("rectangle2_mc", 11); rectangle2_mc._x = obj1_x; rectangle2_mc._y = obj1_y; drawRectangle(rectangle2_mc, target_mc._width, target_mc._height, 0x99FF00, 50); } if ((obj2_x > obj1_x) && (obj2_x2 < obj1_x2) && (obj2_y > obj1_y) && (obj2_y2 < obj1_y2)) { return true; } else { return false; } } function drawRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number, fillColor:Number, fillAlpha:Number):Void { with (target_mc) { beginFill(fillColor, fillAlpha); moveTo(0, 0); lineTo(boxWidth, 0); lineTo(boxWidth, boxHeight); lineTo(0, boxHeight); lineTo(0, 0); endFill(); } }
URL: http://dl.dropbox.com/u/316550/code-AS2_drag-drop-collision.zip