Hi All,
I would really appreciate some help.
I have created a drag and drop matching activity and the way it is coded presently, I have given the cards that are dragged the instance names card1, card2, card3…and so forth, and I have named their drop targets zone1, zone2, zone3…and so forth. The code then verifies that the match is correct if card1 hitTest is zone1 and so forth.
This has worked in the past, however in my latest project there are duplicate correct answers.
I will give an example.
Suppose there is a bowl of fruit and the user is asked how many pieces of each type of fruit are in the bowl. They then drag a card with a number to the drop zone by the type of fruit.
If there are 2 oranges and 2 apples the user should be correct no matter which card with a 2 they drag to either piece of fruit.
The way I currently have it coded they have to place the 2 card with the instance name of card1 on zone1 and the 2 card with the instance name of card3 on zone3.
I would really appreciate any help I can get with this.
Here is the code I am currently using:
// Function when card is pressed
function drag(card:MovieClip):Void{
card.startDrag(); // Start dragging the movie clip
card.swapDepths(swapper); // Keep the card above all other things on the stage
}
// function after the card is released
function drop(card:MovieClip):Void{
card.stopDrag(); // stop dragging of card
card.swapDepths(swapper); // put card back to its starting level
checkZoneHit(card); // check to see if the card hit a zone
}
// function to see if the card hit a zone
function checkZoneHit(card:MovieClip):Void{
for(var i:Number = 1; i <= _root.num_cards; i++){
var zone:MovieClip = this[“zone”+i]; // get the zones in order
if(card.hitTest(zone)){
checkCardHit(card, zone); // see if card has hit another card
return; // exit function
}
}
// No hits
resetCard(card); // reset the card
}
function checkCardHit(card:MovieClip, zone:MovieClip):Void{
for(var i:Number = 1; i <= _root.num_cards; i++){
var checker:MovieClip = this[“card”+i]; // get the cards in order
if(card != checker){ // make sure you aren’t checking the same card
if(card.hitTest(checker)){
resetCard(card); // reset the card
return; // exit function
}
}
}
// No cards hit
setCard(card, zone); // set the card to the zone
}
// function to set a cards coords to a zones coords
function setCard(card:MovieClip, zone:MovieClip):Void{
card._x = zone._x; // set x coord
card._y = zone._y; // set y coord
}
// function to set a cards to its starting position
function resetCard(card:MovieClip):Void{
card._x = card.startx; // reset x coord
card._y = card.starty; // reset y coord
}
//## End of functions for the placements of the cards ##//
//## Start of functions to check the answers ##//
// function to see if all the cards are placed
function checkPlacements(num:Number):Void{
var num:Number = _root.num_cards; // get the number of cards in the activity
for(var i:Number = 1; i <= num; i++){
var card:MovieClip = this[“card”+i]; // card reference
if(card._x == card.startx && card._y == card.starty){
// wrong answer
playAnswer(“error”); // let user know not all cards are placed
return; // exit from the function
}
}
checkAnswers(num); // all cards placed - See if the answers are correct
}
// function to see if the answers are correct
function checkAnswers(num:Number):Void{
_root.attempts++; // activity attempted - add one to attempts
var wrong:Number = 0; // number of wrong answers
for(var i:Number = 1; i <= num; i++){
var card:MovieClip = this[“card”+i]; // get card reference
var zone:MovieClip = this[“zone”+i]; // get zone reference
if(card._x != zone._x || card._y != zone._y){
wrong++; // wrong answer
}
}
if(wrong){
if(_root.attempts >= _root.tries){
incorrect(num); // end activity
} else {
tryAgain(num); // reset activity for another try
}
} else {
correct(num); // send to correct function
}
}
// function to set all the cards in the correct state
function correct(num:Number):Void{
for(var i:Number = 1; i <= num; i++){
var card:MovieClip = this[“card”+i]; // get card reference
setState(card, “correct”); // set the state
}
playAnswer(“correct”); // play correct answer
disableCheck(); // disable the check answer button
}
// function to do reset the activity to try again
function tryAgain(num:Number):Void{
for(var i:Number = 1; i <= num; i++){
var card:MovieClip = this[“card”+i]; // get card reference
var zone:MovieClip = this[“zone”+i]; // get zone reference
if(card._x == zone._x && card._y == zone._y){
setState(card, “correct”); // set correct
} else {
resetStartingCoords(card, card._x, card._y, _root.dir, _root.offset); // get the new starting coords for the card
slide(card, _root.offset, _root.dir); // slide the card
}
}
playAnswer(“tryagain”); // play incorrect answer
}
// function to show the correct answers
function incorrect(num:Number):Void{
for(var i:Number = 1; i <= num; i++){
var card:MovieClip = this[“card”+i]; // get card reference
var zone:MovieClip = this[“zone”+i]; // get zone reference
if(card._x == zone._x && card._y == zone._y){
setState(card, “correct”); // set correct
} else {
setState(card, “incorrect”); // set incorrect
slide(card, _root.offset, _root.dir); // slide the card
}
}
playAnswer(“incorrect”); // play incorrect answer
displayCorrectAnswers(num); // put the right answers on the screen
disableCheck(); // disable the check answers button - activity over
}
// function to play the correct answer
function playAnswer(st:String):Void{
_root.answer.gotoAndPlay(st); // play the passed parameter - correct - incorrect - tryagain
}
// function to set card in correct state
function setState(card:MovieClip, st:String):Void{
card.bg.gotoAndStop(st); // set correct state
card.answer.gotoAndStop(st); // set correct answer state
card.enabled = false; // disable the card
}
// function to reset the starting co-ords of a card
function resetStartingCoords(card:MovieClip, xpos:Number, ypos:Number, dir:String, offset:Number):Void{
switch(dir){
case “up”:
card.startx = xpos; // set x coord
card.starty = ypos - offset; // set y coord
break;
case “down”:
card.startx = xpos; // set x coord
card.starty = ypos + offset; // set y coord
break;
case “left”:
card.startx = xpos - offset; // set x coord
card.starty = ypos; // set y coord
break;
case “right”:
card.startx = xpos + offset; // set x coord
card.starty = ypos; // set y coord
break;
default:
break;
}
}
// function to put the correct answers on the screen
function displayCorrectAnswers(num:Number):Void{
for(var i:Number = 1; i <= num; i++){
var card:MovieClip = this[“card”+i]; // card reference
var zone:MovieClip = this[“zone”+i]; // zone reference
var temp_name:String = “right”+i // name for refernce
card.duplicateMovieClip(temp_name, zone.getDepth(), {_x:zone._x, _y:zone._y}); // duplicate the movie clip and put it on the stage
var newcard:MovieClip = this[“right”+i]; // get new card reference
newcard.bg.gotoAndStop(“correct”); // put green border on card
newcard.enabled = false; // disable card
}
}
// function to disable the check answers button
function disableCheck():Void{
var checker:MovieClip = _root.checkanswers; // check answers button instance
checker.enabled = false; // disable the button
checker._visible = false; // hide the button
}
//## End of functions to check the answers ##//