Skip to main content
Known Participant
July 14, 2010
Question

Flash Matching Question

  • July 14, 2010
  • 1 reply
  • 1227 views

Hello,

I have a flash matching exercise that allows users to drag and drop words into 2 locations: adjective and noun. However, right now I am only ever able to drop one word on adjective and one on noun. If I try to drop others on adjective or noun they don't register in the ActionScript.

Link to file (try dropping both on adjective for instance and notice you don't always get the feedback):

http://distance.uaf.edu/projects/flash-tutors/jensmatching.html

Here's the script I'm currently using:

var dict = new Dictionary ();

// =================== Edit =====================

dict[box_a] = cloudy;
dict[box_n] = artichoke;

// ===================== END  ====================

var hits = 0; // counts succesful hits
var max = 0;  // used to compute dictionary length

// For each item in the dictionary we add event listeners
// "for each" will loop through the values ... not the keys

for each (var item in dict)
{
    item.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    item.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    max = max + 1;
    item.buttonMode = true; //needed for the hand cursor
}


// Define a mouse down handler (user is dragging)
function mouseDownHandler(evt:MouseEvent):void {
    var object = evt.target;
    // we should limit dragging to the area inside the canvas
    object.useHandCursor = true;
    object.startDrag();
}

function mouseUpHandler(evt:MouseEvent):void {
    var obj = evt.target;
    // obj.dropTarget will give us the reference to the shape of
    // the object over which we dropped the circle.
    var target = obj.dropTarget;
    // If the target object exists the we ask the test_match function
    // to compare moved obj and target where it was dropped.
    if (target != null)
    {
        test_match(target, obj);
    }
    obj.stopDrag();
}

function test_match(target,obj) {
    // test if the pairs match
    if (dict[target] == obj)
    {
        // we got a hit
        hits = hits+1;
        textField.text = "Yes ! You got one !";
        // make the object transparent
        obj.alpha = 0.5;
        // kill its event listeners - object can't be moved anymore
        obj.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        obj.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
        // Test if we are done
        if (hits == max)
        {
            textField.text = "Excellent!";
        }
    }
    else
    {
        textField.text = "Try Again :(";
    }
}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
July 14, 2010

when an object is dropped on the target is it removed from the display list or, at least, moved below the drop target?

Known Participant
July 14, 2010

The object gets greyed out (if it was a correct answer) and is no longer drag and droppable - but the one that hasn't been placed on either noun or adjective is still drag and droppable.

I guess the problem is that it's only allowing one object to be the correct answer of any one location when what i would prefer is if multiple objects could be the correct answer for any one location..

Does that make any sense?

Ned Murphy
Legend
July 14, 2010

I couldn't get the problem to occur when I tried it.  But if you leave the first dropped object on top of the drop target, it will likely be blocking other objects from being detecting the target.  You probably want to move the dropped object behind the target when it is successfully planted... so you may want to have your real target be an object with an alpha value of 0, and just have some graphic behind it so that it still looks like what you drop is sitting atop the target area.