0
votes

I'm a newbie to Actionscript, so I'm not sure quite what the problem is with my code.

I'm creating a Flash/Actionscript game, where kids will drag and drop different food items onto the food pyramid - the modern one. All items drag and drop, however only the first item (banana_mc) creates feedback. I have no idea why this is happening, and there are no Compiler errors.

My actionscript is as follows:

    banana_mc.objName = "banana";
banana_mc.initX = banana_mc.x;
banana_mc.initY = banana_mc.y;
banana_mc.val = 0;

bread_mc.objName = "bread";
bread_mc.initX = bread_mc.x;
bread_mc.initY = bread_mc.y;
bread_mc.val = 0;

broccoli_mc.objName = "broccoli";
broccoli_mc.initX = broccoli_mc.x;
broccoli_mc.initY = broccoli_mc.y;
broccoli_mc.val = 0;

burger_mc.objName = "burger";
burger_mc.initX = burger_mc.x;
burger_mc.initY = burger_mc.y;
burger_mc.val = 0;

carrot_mc.objName = "carrot";
carrot_mc.initX = carrot_mc.x;
carrot_mc.initY = carrot_mc.y;
carrot_mc.val = 0;

cheese_mc.objName = "cheese";
cheese_mc.initX = cheese_mc.x;
cheese_mc.initY = cheese_mc.y;
cheese_mc.val = 0;

chips_mc.objName = "chips";
chips_mc.initX = chips_mc.x;
chips_mc.initY = chips_mc.y;
chips_mc.val = 0;

corn_mc.objName = "corn";
corn_mc.initX = corn_mc.x;
corn_mc.initY = corn_mc.y;
corn_mc.val = 0;

eggplant_mc.objName = "eggplant";
eggplant_mc.initX = eggplant_mc.x;
eggplant_mc.initY = eggplant_mc.y;
eggplant_mc.val = 0;

egg_mc.objName = "egg";
egg_mc.initX = egg_mc.x;
egg_mc.initY = egg_mc.y;
egg_mc.val = 0;


banana_mc.buttonMode = true;
bread_mc.buttonMode = true;
broccoli_mc.buttonMode = true;
burger_mc.buttonMode = true;
carrot_mc.buttonMode = true;
cheese_mc.buttonMode = true;
chips_mc.buttonMode = true;
corn_mc.buttonMode = true;
eggplant_mc.buttonMode = true;
egg_mc.buttonMode = true;

banana_mc.addEventListener(MouseEvent.MOUSE_DOWN, mousePress);
banana_mc.addEventListener(MouseEvent.MOUSE_UP, mouseRelease);
bread_mc.addEventListener(MouseEvent.MOUSE_DOWN, mousePress);
bread_mc.addEventListener(MouseEvent.MOUSE_UP, mouseRelease);
broccoli_mc.addEventListener(MouseEvent.MOUSE_DOWN, mousePress);
broccoli_mc.addEventListener(MouseEvent.MOUSE_UP, mouseRelease);
burger_mc.addEventListener(MouseEvent.MOUSE_DOWN, mousePress);
burger_mc.addEventListener(MouseEvent.MOUSE_UP, mouseRelease);
carrot_mc.addEventListener(MouseEvent.MOUSE_DOWN, mousePress);
carrot_mc.addEventListener(MouseEvent.MOUSE_UP, mouseRelease);
cheese_mc.addEventListener(MouseEvent.MOUSE_DOWN, mousePress);
cheese_mc.addEventListener(MouseEvent.MOUSE_UP, mouseRelease);
chips_mc.addEventListener(MouseEvent.MOUSE_DOWN, mousePress);
chips_mc.addEventListener(MouseEvent.MOUSE_UP, mouseRelease);
corn_mc.addEventListener(MouseEvent.MOUSE_DOWN, mousePress);
corn_mc.addEventListener(MouseEvent.MOUSE_UP, mouseRelease);
eggplant_mc.addEventListener(MouseEvent.MOUSE_DOWN, mousePress);
eggplant_mc.addEventListener(MouseEvent.MOUSE_UP, mouseRelease);
egg_mc.addEventListener(MouseEvent.MOUSE_DOWN, mousePress);
egg_mc.addEventListener(MouseEvent.MOUSE_UP, mouseRelease);

//Mouse Events

function mousePress(event:MouseEvent):void {
    var item:MovieClip = MovieClip(event.target);
    item.startDrag();

    var topPos:uint = this.numChildren - 1;
    this.setChildIndex(item, topPos);
}


function mouseRelease(event:MouseEvent):void {
    var item:MovieClip = MovieClip(event.target);
    item.stopDrag();

    switch (item.objName) {
        case "banana" :
            if (bottom_mc.hitTestObject(item)) {
                updateShape(item, bottom_mc);

            } else {
                info_txt.text ="WRONG!";

            }
            break;
        case "bread" :
            if (third_mc.hitTestObject(item)) {
                updateShape(item, third_mc);

            } else {
                info_txt.text ="WRONG!";

            }
            break;
        case "broccoli" :
            if (bottom_mc.hitTestObject(item)) {
                updateShape(item, bottom_mc);
            } else {
                info_txt.text ="WRONG!";

            }
            break;
            case "burger" :
            if (top_mc.hitTestObject(item)) {
                updateShape(item, top_mc);

            } else {
                info_txt.text ="WRONG!";

            }
            case "carrot" :
            if (bottom_mc.hitTestObject(item)) {
                updateShape(item, bottom_mc);

            } else {
                info_txt.text ="WRONG!";

            }
            case "cheese" :
            if (second_mc.hitTestObject(item)) {
                updateShape(item, second_mc);

            } else {
                info_txt.text ="WRONG!";

            }
            case "chips" :
            if (top_mc.hitTestObject(item)) {
                updateShape(item, top_mc);

            } else {
                info_txt.text ="WRONG!";

            }
            case "corn" :
            if (bottom_mc.hitTestObject(item)) {
                updateShape(item, bottom_mc);

            } else {
                info_txt.text ="WRONG!";

            }
        default :
            ; ;
    }
    function updateShape(item:MovieClip, bin:MovieClip):void {


    item.visible = true;
    info_txt.text ="CORRECT!";

    item.val = 1;
    resetShapes();
}

function resetShapes() {
    if ((banana_mc.val == 1)&& (bread_mc.val == 1) && (broccoli_mc.val == 1) &&(burger_mc.val == 1) &&(carrot_mc.val == 1) &&(cheese_mc.val == 1) &&(chips_mc.val == 1) &&(corn_mc.val == 1) &&(eggplant_mc.val == 1) &&(egg_mc.val == 1)){

        banana_mc.x = banana_mc.initX;
        banana_mc.y = banana_mc.initY;
        bread_mc.x = bread_mc.initX;
        bread_mc.y = bread_mc.initY;
        broccoli_mc.x = broccoli_mc.initX;
        broccoli_mc.y = broccoli_mc.initY;
        burger_mc.x = burger_mc.initX;
        burger_mc.y = burger_mc.initY;
        carrot_mc.x = carrot_mc.initX;
        carrot_mc.y = carrot_mc.initY;
        cheese_mc.x = cheese_mc.initX;
        cheese_mc.y = cheese_mc.initY;
        chips_mc.x = chips_mc.initX;
        chips_mc.y = chips_mc.initY;
        corn_mc.x = corn_mc.initX;
        corn_mc.y = corn_mc.initY;
        eggplant_mc.x = eggplant_mc.initX;
        eggplant_mc.y = eggplant_mc.initY;
        egg_mc.x = egg_mc.initX;
        egg_mc.y = egg_mc.initY;


        banana_mc.visible= true;
        bread_mc.visible= true;
        broccoli_mc.visible= true;
        burger_mc.visible= true;
        carrot_mc.visible= true;
        cheese_mc.visible= true;
        chips_mc.visible= true;
        corn_mc.visible= true;
        eggplant_mc.visible= true;
        egg_mc.visible= true;


        banana_mc.val= 0;
        bread_mc.val= 0;
        broccoli_mc.val= 0;
        burger_mc.val= 0;
        carrot_mc.val= 0;
        cheese_mc.val= 0;
        chips_mc.val= 0;
        corn_mc.val= 0;
        eggplant_mc.val= 0;
        egg_mc.val= 0;

    }
}

}

If anyone can please help me out, I'd really appreciate it! It's part of an assignment this week (not a coding one) so I'd really need a quick solution if possible...

2

2 Answers

0
votes

Your switch statements are configured to look for the exact string specified by your object's name. So casing matters :) Either do a item.objName.toLocalLowercase(), change your object's name to only include lowercase letters, or change your case's to match all your object's names by capitalizing everything except banana.

0
votes

Just a useful tip to reduce code amount...

In your function updateShape(item:MovieClip, bin:MovieClip):void at the bottom you have:

resetShapes(); which might be better as resetShapes( item );

If you provide a function parameter (aka function argument) like above then when you run resetShapes it only has to deal with current clicked item (provided as function parameter) so you can just simply write...

function resetShapes ( in_item : MovieClip ) : void
{
    if( in_item.val == 1)
    {
        in_item.x = in_item.initX;
        in_item.visible = true;
        in_item.val = 0;
    }
}

Also in your function mouseRelease(event:MouseEvent):void at the bottom you have default : ; ; this might cause problems when you try to compile so fix or remove it. What are you trying to achieve with this command? If we understand your code or logic, it's easier to give you advice.

check this Switch/Case tutorial article for clarity...