dunnjo98
dunnjo98

Reputation: 33

drag and drop game only two end results, right or wrong AS3

I have been banging away on this issue for a long time now and I would love someone to whack me upside the head. I feel like I am so close, but you know how that is.

I have a drag and drop game, 8 options and 5 are correct. I want the user to drag 5 items from the 8 to the right of the screen. When they reach 5, it gives them a result. Right or Wrong (to keep the process moving I am not making them keep re-trying). Well, at this point in my code, everything is working well, except, when the end comes it only shows the right answer no matter what they drag. Please help.

I hope this is clear, thanks for any advice. I already appreciate you if you even read this.

import flash.display.MovieClip;

var cor_mc:correct_items_mc = new correct_items_mc;
var nopeAnswers:incorrect_items = new incorrect_items;
var policies:Array = [policy_0,policy_1,policy_2,policy_3, policy_4,policy_5,policy_6, policy_7];
var correct_choices:Array = [0,2,4,5,6];

var selected_items:Array = [];

for (var i:int = 0; i < policies.length; i++) {
    var mc:MovieClip = policies[i];

    mc.buttonMode = true;
    mc.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
    mc.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
    mc.home_x = mc.x;
    mc.home_y = mc.y;
    mc.is_correct = correct_choices.indexOf(i);
}

function fl_ClickToDrag(event:MouseEvent):void
{
    event.currentTarget.startDrag();
    setChildIndex(MovieClip(event.currentTarget), numChildren - 1);
}

function fl_ReleaseToDrop(event:MouseEvent):void
{
    var dropIndex:int = policies.indexOf(event.currentTarget);
    var target:MovieClip = event.currentTarget as MovieClip;
    target.stopDrag();

    if (target.hitTestObject(bucket_mc)) {
        selected_items.push(target);
        trace(selected_items);
        target.x = bucket_mc.x;
        target.y = bucket_mc.y + selected_items.length * 60;
    } else { // no match ... false
        target.x = target.home_x;
        target.y = target.home_y;
    }

    if (selected_items.length === correct_choices.length) 
    {
        for (i = 0; i < correct_choices.length; i ++)
        {
            var cur_choice = correct_choices[i];
            if (!selected_items.indexOf(cur_choice)) {
                addChild(nopeAnswers);
                nopeAnswers.x = 0;
                nopeAnswers.y = 100;
            } else {
                addChild(cor_mc);
                cor_mc.x = 0;
                cor_mc.y = 100;
            }
        }
    }
}

Upvotes: 1

Views: 395

Answers (1)

baarkerlounger
baarkerlounger

Reputation: 1226

From the Manual indexOf returns -1 if the element is not found rather than a boolean false.

Change the condition to:

if (selected_items.indexOf(cur_choice) < 0){
   ....
}

Also if any of your first 4 correct values have NOT been selected you still continue looping and so will still output cor_mc if the final correct item was selected. You need to stop the loop once you find a correct answer missing:

if (selected_items.indexOf(cur_choice) < 0) {
      addChild(nopeAnswers);
      nopeAnswers.x = 0;
      nopeAnswers.y = 100;
      break;
} else {
      addChild(cor_mc);
      cor_mc.x = 0;
      cor_mc.y = 100;
}

Upvotes: 1

Related Questions