all groups > flash actionscript > april 2004 >
You're in the

flash actionscript

group:

Drag and drop


Drag and drop AmberP813
4/3/2004 5:54:56 PM
flash actionscript:
I am trying to create a drag and drop game for first graders and need some
help. I have been able to figure out the whole button/movie clip deal and
found some great examples of what actionscripts to use to get my
button/movieclips to drag to a dropzone section on my page. My problem now is
that all the items that are dropped automatically center in the dropzone and I
want them to be sporadically laid out within the dropzone. I am still very
much a novice at actionscripts and if anyone can recommend a good tutorial or
good book I'd be grateful. Currently, I am using the SAMS Teach Yourself
Actionscript in 24 hours.

Here's the game so far:

http://webpages.charter.net/amber.price/dropzone_ook.swf

And, here is the actionscript I have created:

onClipEvent (load) {
origX = this._x;
origY = this._y;
}

onClipEvent (mouseDown) {
if (this.hitTest(_root._xmouse, _root._ymouse)) {
this.startDrag();
}
}
onClipEvent (mouseUp) {
if (this.hitTest(_root._xmouse, _root._ymouse)) {
this.stopDrag();

// see if the dropZone conatins the center of this mc
if (_parent.dropZone0.hitTest(this._x,this._y,true)) {

// center it on the drop zone
this._x = _parent.dropZone0._x;
this._y = _parent.dropZone0._y;
} else {

// return it to its original location
this._x = origX;
this._y = origY;
}
}
}

Thanks for any help! Amber

Re: Drag and drop Jan-Paul K.
4/3/2004 6:27:15 PM
ok this part centers the mc's:
// center it on the drop zone
this._x = _parent.dropZone0._x;
this._y = _parent.dropZone0._y;
now what you may want to do is to add/substract some random pixels from the
position before each element is placed. Of course this is no guarantee that
there are no two items on the same position, but it might improve the situation
a bit. The more complex way would be to set up an position array with a certain
number of predefined position within the circle and place the first dropped
item in the first position, the second in the second and so on.
The most complex, and probably most advanced way would be to use the first
approach with the random position, but also implement a hit check if there is
allready another item at this random position, that might be overlapping with
the new one.

here is some sore for defining a random position for each new element:

this._x = _parent.dropZone0._x + random(20);
this._y = _parent.dropZone0._y + random(20);

in this case you have to modify the value in brakets to match the radius of
your droptarget minus half of the maximum height/width for each drag-item.

maybe something like this works:
this._x = _parent.dropZone0._x + random( _parent.dropZone0._width/2 -
this._width/2 );
this._y = _parent.dropZone0._y + random( _parent.dropZone0._height/2 -
this._height/2);

Hope this helps.
regards,
Paul




AddThis Social Bookmark Button