PDA

View Full Version : Array query



Signy
January 4th, 2008, 11:22 AM
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.

ArmoredSandwich
January 4th, 2008, 12:22 PM
im not quite sure what you want to achieve with this
mealConstruction.["carrot"];

if mealContstruction is an array then whats inside of the brackets [] must be a number, not a string.

you can compare things in arrays like this.



ingredientsArray = new Array ("carror", "salt", "potatoes");
compContentArray("salt", ingredientsArray);

function compContentArray (compareString:String, compareArray:Array):Void
{
for (var i = 0; i < compareArray.length; i ++)
{
if (compareArray[i] == compareString)
{
trace ("The array already has : " + compareString);
}
}
}
So what you need is this (I think, for Im NOT sure what it is exactly that you ask me)



// you have 2 arrays, ingredients, and what you have.
whatYouHave = new Array ();
ingredientsArray = new Array ("carrot", "salt", "potatoes", "meat");

// u catch something
catchedItem = "carrot";
catched();
catched();

function catched():Void
{
// check if u need it
if (compContentArray(catchedItem, ingredientsArray))
{
trace ("u need the item u catched for the meal");
// check if u allready have the item.
if (!compContentArray(catchedItem, whatYouHave))
{
trace ("u didnt had the item, so its now added");
whatYouHave.push(catchedItem);
}
else
{
trace ("u allready had the item, so u dropped it");
}
}
else
{
trace ("u do not need the item u catched for the meal");
}
}
function compContentArray (compareString:String, compareArray:Array):Boolean
{
for (var i = 0; i < compareArray.length; i ++)
{
if (compareArray[i] == compareString)
{
return true;
}
}
return false;
}


ask if you dont understand something

Signy
January 4th, 2008, 01:03 PM
http://img178.imageshack.us/img178/5387/testic2.jpg

The ingredients falling from the sky are being spawned from an array each ingredient is given its own set of properties.

To complete the round you have to catch certain types of ingredients in certain quantities. Each time you catch and ingredient it is stored in an array.

For example the little chef dude catches a carrot in his pot then "carrot" would be sent to the mealConstruction array.


mealConstruction = [];

//carrot is then caught

mealConstruction = ["carrot"]The mealConstuction array has to match the mealIngrediants array in order for the player to complete one meal.

In the first level for example you have to catch a carrot, onion and mushroom but only one of each.

What I wanted to be able to write was if one ingredient appears in the mealConstruction array twice then clear the array. But instead i ended having to create and ingredientCounter which went up in increments. Which is annoying because it meant I had to rewrite it for each ingredient.

Ideally what I would like to achieve is everytime an ingredient shows up in the array more than one reset the array.

Every time the array contents of mealConstructor and mealIngredients match then clear the array and decrease the amount of meals needed by 1.

Every time an ingredient appears in mealConstructor which is either not in the mealIngredients array clear the array. Or alternatively every time an ingredient from a badIngredient array appears in the mealConstructor array then reset mealConstructor.

But if you can only reference array places and not the contents of the array then i have a problem because you can catch ingredients in any order you wish just as long as what your catching is in the mealIngredients array.

Hope this helps explain the trouble I'm having.

ArmoredSandwich
January 4th, 2008, 02:40 PM
I c, im working on it ;)

ArmoredSandwich
January 4th, 2008, 02:57 PM
// you have 2 arrays, ingredients, and what you have.
mealIngrediants = new Array ();
mealConstructor = new Array ("carrot", "onion", "mushroom");

// u catch something
catchedItem = "meat";
catched();
catchedItem = "carrot";
catched();
catched();
catched();
catchedItem = "onion";
catched();
catchedItem = "mushroom";
catched();

function catched():Void
{
// check if u need it
if (compContentArray(catchedItem, mealConstructor))
{
trace ("u need the item (" + catchedItem + ") u catched for the meal");
// check if u allready have the item.
if (!compContentArray(catchedItem, mealIngrediants))
{
trace ("u didnt had the item (" + catchedItem + "), so its now added");
mealIngrediants.push(catchedItem);
if (checkDone(mealIngrediants, mealConstructor))
{
trace ("mealIngrediants is equal mealConstructor, ur done");
trace ("mealIngrediants = " + mealIngrediants);
trace ("mealConstructor = " + mealConstructor);
}
else
{
trace ("ur not done just yet");
}
}
else
{
trace ("u allready had the item (" + catchedItem + "), so now ur mealConstructor array is reseted");
mealIngrediants = new Array();
}
}
else
{
trace ("u do not need the item (" + catchedItem + ") u catched for the meal, so now ur mealConstructor array is reseted");
mealIngrediants = new Array();
}
}
function checkDone(array1:Array, array2:Array):Boolean
{
if (array1.length == array2.length)
{
array1.sort();
array2.sort();
for (var i = 0; i < array1.length; i ++)
{
if (array1[i] != array2[i])
{
return false;
}
}
return true;
}
else
{
return false;
}
}
function compContentArray (compareString:String, compareArray:Array):Boolean
{
for (var i = 0; i < compareArray.length; i ++)
{
if (compareArray[i] == compareString)
{
return true;
}
}
return false;
}
Is this what u need? There might be an easier way to compare if two arrays are the same, but this will do the trick.

Let me explain whats happening:

mealIngrediants = new Array ();
mealConstructor = new Array ("carrot", "onion", "mushroom");
nothing to explain there, you can assign mealConstructor to a random pre made mealContructor (I expect thats what you did).

catchedItem = "carrot";
catched();
catched();
catched();
catchedItem = "onion";
catched();
catchedItem = "mushroom";
catched();
If your chef dude catches an item, it should check what itemType it is. That should be catchedItem, since I do not have that I just did it manually.
Catched(); is the function that sees what should happenend. The reason why i did catched() 3 times after catchedItem = "carrot"; Is because now you can see whats happening if you catch something that you allready caught. The function catched is enough commented to find out whats happening there on your own.

The function checkDone checks if you have an array that is identical to another array. If both arrays are the same lenghts they get sorted. If theyr both equal theyre sorted the same. You can then check with an easy for loop if they have the same value. Returns true or false.

compContentArray compares if a string is already present in the array. Returns true or false.

I hope you understand, I also hope this is what youre asking for. Again, if you need anything else, or if I misunderstood, just say the word.

sorry for the double post, admins feel free to delete above post.

Signy
January 6th, 2008, 08:49 AM
Thanks alot dude. It's not quite what i wanted but it's certainly pointed me in the right direction.