A common activity in Flash-based interactives is dragging an object to a target area in order to trigger a specific effect. The sample to the right is a simple match game; must must drag the circles to the matching rings. Note how the solid circles snap in place if you release them within their rings. The "Done" button checks your accuracy—if all three circles in the proper place, you get a smiley face; otherwise you get a frowny face.
The key to accomplishing this is a "hit test," a method that Flash uses to determine whether two objects are in contact with one another.
Create your own matching game.
smiley._visible=false;
frowny._visible=false;
This will hide the feedback objects until needed. It also assumes that you named your feedback movie clips smiley and frowny. If you used different instance names, change the script to reflect the names you used.
// Get the movie clip's location and
// store the coordinates as variables
onClipEvent (load) {
origX = this._x;
origY = this._y;
}
As noted in the comment, this finds the starting x & y coordinates of the object and then stores those as variables called origX and origY. We'll use these variables further down...
onClipEvent(enterFrame) {
// Make the movie clip dragable
this.onPress = function () {
startDrag(this)
}
This bit makes the object dragable.
// Stop dragging the object
this.onRelease = this.onReleaseOutside = function () {
this.stopDrag();
// see if the dropZone contains the center of this mc
if (_parent.dropZone1.hitTest(this._x,this._y,true)) {
// center it on the drop zone
this._x = _parent.dropZone1._x;
this._y = _parent.dropZone1._y;
} else {
// if it's not in the drop zone,
//
return to the original location
this._x = origX;
this._y = origY;
}
}
}
This piece of code accomplishes several things. First it stops the dragging function. Next it checks to see if drag1 is centered over the appropriate drop zone object by using a hit test. If it is, it snaps to the drop zone's center. If not, it jumps back to its starting location (the origX & origY).
on (release) {
// assume all are correct
correct = true;
// check to see that drag mc locations match
// drop zone locations
if ((drag1._x != dropZone1._x) or (drag1._y != dropZone1._y)) {
correct = false;
}
if ((drag2._x != dropZone2._x) or (drag2._y != dropZone2._y)) {
correct = false;
}
if ((drag3._x != dropZone3._x) or (drag3._y != dropZone3._y)) {
correct = false;
}
// signal to the Output window
if (correct) {
smiley._visible=true;
frowny._visible=false;
} else {
frowny._visible=true;
smiley._visible=false;
}
}
This code creates a variable called "correct" and sets it to "true." Then it looks to see of the three dragable objects are in the drop zones. If any one is not in the right place, the variable "correct" is changed to "false," and frowny appears. If all are true then the variable is left as is, and smiley is made visible.
on (release) {
frowny._visible=false;
smiley._visible=false;
drag1._x = drag1.origX;
drag1._y = drag1.origY;
drag2._x = drag2.origX;
drag2._y = drag2.origY;
drag3._x = drag3.origX;
drag3._y = drag3.origY;
}
This script hides both smiley and frowny, and then moves the objects back to their starting positions.
// Stop dragging the object
this.onRelease = this.onReleaseOutside = function () {
this.stopDrag();
// see if the dropZone contains the center of this mc
if (_parent.dropZone1.hitTest(this._x,this._y,true)) {
// center it on the drop zone
this._x = _parent.dropZone1._x;
this._y = _parent.dropZone1._y;
// create a sound effect when it's dropped in the drop zone
mySound = new Sound();
mySound.attachSound("boing", true);
mySound.start();
} else {
// if it's not in the drop zone,
//
return to the original location
this._x = origX;
this._y = origY;
}
}
}
In this case I set the linkage name of the sound to "boing." Your script should reflect whatever linkage name you used (right-click on the sound in the library to set the linkage name).
this._x = origX;
this._y = origY;
to:
this._x = this._x;
this._y = this._y;
Basically, just says "stay put."
this.onRelease = this.onReleaseOutside = function () {
this.stopDrag();
// see if the dropZone contains the center of this mc
if (_parent.dropZone1.hitTest(this._x,this._y,true)) {
// center it on the drop zone
this._x = _parent.dropZone1._x;
this._y = _parent.dropZone1._y;
} else if (_parent.dropZone2.hitTest(this._x,this._y,true)) {
this._x = _parent.dropZone2._x;
this._y = _parent.dropZone2._y;
} else if (_parent.dropZone3.hitTest(this._x,this._y,true)) {
this._x = _parent.dropZone3._x;
this._y = _parent.dropZone3._y;
} else {
// return it to its original location
this._x = origX;
this._y = origY;
}
}
}
4 pts |
Portfolio quality design; great match game |
3 pts |
Good looking; above average work. |
2 pts |
It works, but it's nothing fancy. |
1 pts |
Something's not working right. |
0 pts |
Poor showing; mostly incomplete or full of errors. |