View Full Version : Drag Drop Game
sethmac
06-17-2008, 06:28 AM
I'm creating a drag drop game. When a movieclip is placed on a HitArea, it should remain there. My problem is, I would like to disable the hitArea if it is in contact with a movie clip already so I don't have two movieclips overlapping. Here is a sample of my code.
function released() {
this.stopDrag();
checkTarget(this);
//displayFeedback(this);
}
function checkTarget(drag){
for(var i:Number=0; i<myArray.length; i++) {
if (drag.hitTest(home["step"+i].HitArea_mc)) {
drag._x=(home["step"+i]._x)+ home["step"+i].step_txt._width;
drag._y=home["step"+i]._y;
}
}
}
personnaly I use dynamic variables for this kind of job, here look at this simple example and see what I mean (just copy and paste in the action panel):
var base:MovieClip = this.createEmptyMovieClip("base_mc",1);
var drag:MovieClip = this.createEmptyMovieClip("drag_mc",2);
var drag2:MovieClip = this.createEmptyMovieClip("drag2_mc",3);
base.beginFill(0x000000);
drag.beginFill(0xFF0000);
drag2.beginFill(0x0000FF);
drawRectangle(base, 50, 50)
drawRectangle(drag, 50, 50)
drawRectangle(drag2, 50, 50)
base.endFill();
drag.endFill();
drag2.endFill();
base._x = Stage.width/2;
base._y = Stage.height/2;
drag._x = 50;
drag._y = 150;
drag.Xpos = drag._x;
drag.Ypos = drag._y;
drag2._x = 50;
drag2._y = 250;
drag2.Xpos = drag2._x;
drag2.Ypos = drag2._y;
base.occupied = false;
drag.onPress=function(){
this.startDrag();
}
drag2.onPress=function(){
this.startDrag();
}
drag.onRelease=function(){
this.stopDrag();
if(this.hitTest(base) && base.occupied == false){
this._x = base._x;
this._y = base._y;
base.occupied = true;
}else{
this._x = this.Xpos;
this._y = this.Ypos;
}
}
drag2.onRelease=function(){
this.stopDrag();
if(this.hitTest(base) && base.occupied == false){
this._x = base._x;
this._y = base._y;
base.occupied = true;
}else{
this._x = this.Xpos;
this._y = this.Ypos;
}
}
function drawRectangle(_mc:MovieClip, theWidth:Number, theHeight:Number ):Void{
_mc.moveTo(-theWidth, -theHeight);
_mc.lineTo(theWidth, -theHeight);
_mc.lineTo(theWidth, theHeight);
_mc.lineTo(-theWidth, theHeight);
_mc.lineTo(-theWidth, -theHeight);
}
The target (base) gets a dynamic variable named "occupied" whiich is set to false. Then when I drop a MC on it I set it to true and I'm not able to drop another MC anymore.
sethmac
06-17-2008, 03:33 PM
Thanks for the help. I have one last question... I would like either the overlapping object to replace any previous overlapping object (this would negate the need for tracking something being occupied probably) or the ability to set the drop area back to unoccupied when you drag a movie clip off off it. Any ideas?
Thanks!
Quite simple. When you release over the target area, set a dynamic variable with the name of the dropped MC, then when you drop another MC on top of it remove the current MC and set the new one as the current MC (as you said you probably don't need the occupied variable anymore):
drag.onRelease=function(){
this.stopDrag();
if(this.hitTest(base)){
base.currentMC._x = base.currentMC.Xpos;
base.currentMC._y = base.currentMC.Ypos;
this._x = base._x;
this._y = base._y;
base.currentMC = this;
}
}
sethmac
06-17-2008, 05:32 PM
Thanks! That works great!
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.