PDA

View Full Version : Managing instances created by loop


boneyabba
12-19-2007, 11:07 AM
I am making a small item trading game where the player moves from store to store trying to buy low and sell high. The mechanism for this is a backpack system similar to the game Diablo... Basically a store is a 6x5 grid, the player's backpack is a 3x4 grid, and the items are either 1x1 or 2x2. The store, and player inventories are just arrays with item names.

When the player arrives at a store I read the inventory arrays, and use a loop to create instances of the various inventory item objects. The player sees the store grid, and the backpack grid with these instances. They can move items from one to the other freely, each time an object is dropped from the store to the backpack, or vice versa, the inventory arrays are updated.

I have my code working to read the arrays, and populate the store grid. But I can't figure out how to manage the instances. I globally define a variable that is referenced to a linked object at the start of my AS:

var _inventoryItemTypeA = InventoryItemTypeA_mc

Then, when the loop reads that the inventory array is full of, say 5 of these... I do like this:

var _itemsPlaced:Number = 0

if (_itemsPlaced < _storeInventory.length)
{
_inventoryItemTypeA.addEventListener(blahblah)
_inventoryItemTypeA = new InventoryItemTypeA_mc
addchild(_inventoryItemTypeA)
_inventoryItemTypeA.x = blah
_itemsPlaced = _itemsPlaced + 1
}

So, it places all my stuff. But it is clear that the addEventListeners and the removeChild stuff I haven't written needs to know how to tell the different instances apart... I can't figure out where to look.

Answers are good- but I'd be just as stoked if I was pointed to the right tutorials. I am the suck at finding the right tutorials.

Thanks,
T

Daedalus
12-19-2007, 11:56 AM
Why not try storing the instances in a multidimentional array. With the first dimention representing rows and the second columns so you can access any instance just by accessing the correct column and row in the array.

just use a for loop with another inner for loop to populate the array when you create the instances.

If you dont want to use multidimentional arrays you could just set the instance name in the for loop with the name of the column and row in which it is located and then just store or the instances in a single array and then find an instance by looping through each array element to find the instance with the desired name.

boneyabba
12-19-2007, 12:45 PM
Thank you for your reply!

I am not sure I completely understand you though. It *sounds* like my tentative plan is, at least vaguely, related to your suggestion. I actually truncated my post because I had to take someone to the airport and was rushing... The second I got in my car I realized I should have put what thought was a good solution for comment as well.

Since I already have this loop going and am tracking the objects placed... I was hoping I could do something where I append a number to my inventory item variable based on that... Something like:

addChild(_inventoryItemTypeA[_itemsPlaced])

But I can't get it to work. I think I am just not using the right syntax to change the name of the child? But maybe it is because I need to do that when I first declare the variable? Heck... Even if I could somehow tell it to populate whatever code generated ID it uses internally into an array that would work... I think part of my problem is that I don't really understand what Flash is doing to create and track the instances- regardless of if I want to interact with them.

boneyabba
12-19-2007, 01:44 PM
So, I tried to get the "put the name in an array" thing going by creating an array as a global, the putting

_testArray(_itemsPlaced) = _inventoryItemTypeA

into my code.

It works in that it puts a copy of the item in the array in a nice spot... But it uses the objectname of _inventoryItemTypeA rather than its internal name.

When I use the List Objects feature it shows things like:

_level10.instance101

I think I need to know what name like this Flash is assigning to my instances in such a way as that I can call them accordingly... Or influence this name... or something?

Yahivin
12-19-2007, 04:11 PM
Instead of keeping item names in the Arrays, why not keep actual item instances? You could have a class Item, and it can have a property 'name'. Initialize the items once in the beginning and put them into the Array. To display the name use someItem.name, and someItem is the reference to the instance.

Is this helpful, or am I off target?

boneyabba
12-19-2007, 07:14 PM
I don't know if you are on track, I didn't understand it! :) I should take this time to point out- I am terribly ignorant.

I think this is what I tried to do but when I traced the array I just got the same string back for each entry.

boneyabba
12-20-2007, 10:04 AM
Okay, I figured out how to put the object in an array then add the instances.

So, that is good. I now know how to refer to the specific children.

Now I am stuck trying to figure out how to pass that information into an event function.

This is the line that creates the event listener:

_iconWood1b[_objectsPlaced + 1].addEventListener(MouseEvent.CLICK, RemoveWood);

But the function RemoveWood doesn't know which instance to talk to. I've tried a bunch of variations with event.target but I just generate errors.

For my test I just want to removeChild(_iconWood1b[_objectsPlaced+1]) but "_objectsPlaced" has moved on by this point and no longer matches the state when the instance was created.