PDA

View Full Version : [AS2] How to Keep & Display score for Drag & Drop Game??


suriaz20
07-13-2007, 02:56 AM
I need your kind help urgently.

I need to create a simple drag and drop game. If the object is dropped to the correct location, the score will be displayed (e.g. 3 out of 4 right!).

I managed to create the drag and drop and snapped it at the right location.

But, am unable to keep and display the score.

I have minimal AS knowledge so the code below was taken from other sites. (I do know the meaning of instance names, and some basic actions though)

Anyone can help?????
-------------------------------------------------------------------------
This is my actionscript applied for the object called "paper" -

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 contains the center of this mc
if (_parent.plastic.hitTest(this._x,this._y,true)) {

// center it on the drop zone
this._x = _parent.plastic._x;
this._y = _parent.plastic._y;

// this part here, I did on my own...just an assumption that it might
work but didn't
var total=1;
total++;
score.text="You scored " + total +" out of 4";
}
else {
// return it to its original location
this._x = origX;
this._y = origY;
}

}

Noct
07-13-2007, 01:27 PM
Welcome aboard,
Try this: (untested)
onClipEvent (load) {
var origX:Number = this._x;
var origY:Number = this._y;
var total:Number = 0;
var dZ:MovieClip = this._parent.plastic;
}
on (press) {
this.startDrag();
}
on (release) {
this.stopDrag();
// see if the dropZone contains the center of this mc
if (this.hitTest(dZ)) {
// center it on the drop zone
this._x = dZ._x+dZ._width/2-(this._width/2);
this._y = dZ._y+dZ._height/2-(this._height/2);
total++;
this._parent.score.text = "You scored "+total+" out of 4";
} else {
// return it to its original location
this._x = origX;
this._y = origY;
}
}

Oh, and if you don't want the user to be able to drag the same clip again, stick a disable script on the line below this._parent.score.text = "You scored "+total+" out of 4";"
this.enabled=false

suriaz20
07-16-2007, 08:59 AM
Thanks!!!

Your code looks much simpler to understand and it works flawlessly! :)