Array query

Hiya all,

I’m currently making a game in which the main objective is to catch falling ingredients from the sky in order to create a meal. I have the falling catching and moving mechanics finished and I’m now working on the HUD elements.

I have a working version of the HUD but to me It’s seems like I have achieved this in very cheap messy work around and I would rather be doing things the correct way rather than bodge it.

Here is my code so far:

stop();

var mealIngredients        :Array = ["carrot","mushroom","onion"];
var mealConstruction    :Array = [];
var mealsCompleted        :Number = 3;
var roundOver            :Boolean = false;
var carrotCount            :Number;
var onionCount            :Number;
var mushroomCount        :Number;
var maxIngredients        :Number = 2;

function onEnterFrame(){
    
    ingredientFalling()
    hudAnim()

    if(mealConstruction.length >= mealIngredients.length){
         mealConstruction = [];
         mealsCompleted--
    }
    if(mealsCompleted <= 0){
        roundOver = true;
    }
}

function ingredientFalling(){

    if(Key.isDown(49) && !roundOver){
        mealConstruction.push("carrot");
        carrotCount++
    }

    if(Key.isDown(50) && !roundOver){
        mealConstruction.push("mushroom");
        mushroomCount++
    }
    if(Key.isDown(51) && !roundOver){
        mealConstruction.push("onion");
        onionCount++
    }
    if(carrotCount >= maxIngredients){
         mealConstruction = [];
         carrotCount = 0;
    }
    if(onionCount >= maxIngredients){
         mealConstruction = [];
         onionCount = 0;
    }
    if(mushroomCount >= maxIngredients){
         mealConstruction = [];
         mushroomCount = 0;
    }
    
}

I’m using the .keyDown function to simulate the catchTest of the falling ingredients for the time being.

When I first started work on this I assumed I would be able to write something like this:

if(mealConstruction.["carrot"] >= maxingredients){

Which was not the case so I ended up writing what seems like a pointlessly long an inefficient method.

Is there a way I can say: if there is more than one “carrot” in the array mealConstructor clear it?

Or for example if an item makes it into the mealConstructor array which is not in the mealIngredients array then clear it?

Thanks for any help and advice.