AS3.0 Using arrays for game inventory and how to track objects?

Started by
20 comments, last by jeteran 11 years, 7 months ago
Thanks prototypical for your response.

Sorry yes, I was new to objects and didn't explain myself clearly.

What I did was create many objects (TeddyBear for example) and that objects has it's properties: TeddyBear.id = 0; TeddyBear._image = (instance of the image) and so on.

Then, in the Store.as file. I'm catching the objects I will use in the current mission (say mission 1) and put them in an array called storeMission1.

So far so good. I was able to add the TeddyBear and even I can show the image in scene.

But now I have a small problem.

I have another array called currentInventory where I want to catch the objects I click from storeMission1.

I'm trying: storeMission1[0].addEventListener ( ... ) but I get an error: says that addEventListener is not a function.

What could be wrong?

I tried changing store to a vector instead of a array, but didn't help.

Any idea?

With that, I can rest TeddyBear._cost and add the image to the inventoryArray and do the rest of the store code. This is the only part that I'm trapped !!

Any suggestion will help !!

Thanks prototypical !!
Advertisement

Hey guys, it’s good to be around here.

I’m facing a little problem that has to be with arrays in the game. This is what I want to do and how I plan to do it, and my results. I want to be as clear as possible, really hate when people just say “help” and paste their code :S

I’m trying to make a very simple inventory system. Keep in mind that I already check some of them on the Internet, but it really bothers me to just “copy-paste” code that it’s not mine. So I’m using them for learning purpose. I want to go step by step, so for now I will concentrate only in the inventory (not the buying system)

Ok now, I have 2 scenes in the game. The briefing scene where you can buy goods and save them in your inventory, and the other one is the game scene where you use the objects you bought.

In the briefing scene, I want to save the objects I bought in my inventory, for this I think is best use a fixed array. That array has always 4 slots.

What I want to do is save that array and passed it to the game scene and put them in another array.

With this (I hope) clear explanation, I have some questions:
- Can I use only one array for that inventory? So I save the objects in the briefing scene and call it in the game scene?
- With a future buy system, (that I think it will be another array) what can be the best method to pass them to the inventory array? With just push (or slice?)?


Now, about the game scene:
With the objects in the inventory, I want to be able to drag them to objects in the game scene (table, computer, etc) and, if the inventory objects hitTest the table AND the character, drop it. I’m working this in a different file than the briefing scene BTW.

Now I have some Qs:
[s]- I have 3 objects inside an array called invObjects. To put them in the scene, I used a simple FOR that tells the program to put them in X order. They do. Just after this, I added an event listener to know where the invObject is clicked. In that function, I did: invObjects.startDrag() but says that the object is null, why?[/s]
- When that object is used correctly in the scene, I want it to pop(). If I pop it, those the other objects stay where they are? Pop it is the best method to be used?


I’ll appreciate any suggestion you give me. Anyway I’m working on all this.

Thanks so much guys for your help !!


Hello!
Sorry, I didn't read all of your posts. But I can answer you on first questions:
First, can you use objects for storing player items? Of course, yes. ActionScript has one specific feature: all objects are passed by reference. So, if you created inventory item, that item will always mean the same item.
The best way is to instantiate each player inventory into special object and store each inventory item as instance of custom class.
When you use item as custom item instance, you can do what you want - trade, store, destroy item and etc.
What do you mean about slice or push? Item must have owner (null if it is dropped item), slicing arrays for moving item? Nop, not a good idea.

Method pop() is stack method. If you don't know, what does it mean - read manuals about stack architecture. When you pop item, you take last pushed item from array. Why you need it for player inventory? If you want to change item owner - it is not best way. If you want to implement "Use item", stack.pop() it is not a best way too.
You need just a custom Inventory class and custom Item class. Then you'll be able to do with items what do you want.

I have a practice in server-side game development for managed languages, and in that case, items should have owners - its just much easier to manipulate with such items, than moving items from one array to another.
WOW thanks Deft for your reply, was very informative.

The thing is that most of what I written is done. I'm only stuck in the last reply I did in this post.

I really wish you have any suggestion, is breaking me appart !!

Thanks so much !!
Ok, I'm in :)

I'm trying: storeMission1[0].addEventListener ( ... ) but I get an error: says that addEventListener is not a function.
What could be wrong?
[/quote]
Because of arrays does not have an event listener implementation. You should understand, that not all objects has event system.
Arrays is performance-dependent classes (memory & speed), so, they shouldn't have event listeners. Moreover, arrays are more system resources. If you want have event listeners on your inventory or item, use custom class with event listeners implementation (for this you need to read manuals how to do such thing, if you don't know :)).
I'm interested in what type of event listener you want to add? With custom event listener you can add specific events (for example, onItemUse or onItemExchange and etc.).

And if storeMission1 is game level, do not use it as array, use more OOP techniques, for example, just define getter method for getting some object from current level.

I hope, I understood your problem.
Hey Deft, thanks SO much for your reply !!

Ok I'm still working on this, but can't get farther.

I totaly undestood your point about arrays, thanks once again.


I'm interested in what type of event listener you want to add? With custom event listener you can add specific events (for example, onItemUse or onItemExchange and etc.).

I want to add MouseEvent.CLICK so when the user clicks the object, it adds to the inventory array, in the left of the screen. Any other way to do this?

storeMission1 is the array that I want to put all the objects I want to display only in mission1. The game level is called from another file called Missions where I call the function Mission1 and appears the main character and the levels.

ANY suggestion will be very welcome !! Truth is that I have been working on this game for the past days and can't see anything clearly :( But I will do my best to understand !

Any other way to do this?

Yeap, you can do this by implementing your own class for inventory item.
You need to extend your item from internal ActionScript Sprite class (that classes has event listeners by default). By extending it you gain opportunity to draw an object and use it events. And moreover you can implement your own data and methods for storing data about inventory item.
For example:
[source lang="delphi"]class InventoryItem extends Sprite
{
private:
var _itemId: int;
InventoryItem(itemId: int)
{
_itemId = itemId;
addEventListener(MouseEvent.CLICK, onItemClick);
}

function onItemClick(...)
{
// Your own code
}
}[/source]

Just like this smile.png
Awesome Deft, thanks so much for your message.

Ok, I already have a file called GameObjects.as where I have all the objects that will be used in the game.

Following your suggestion, what I'm going to do now is create a StoreCreation.as file that extends GameObjects instead of Sprite (right??) So I can use the game objects that I need to put in the store in different missions. F.i. inside StoreCreation will have StoreMission1() function that will have object 1 and 2, StoreMission2() that will have object 1, 2 and 3 and so on.

What do you say? I will start doing it right now.

Thanks so much Deft for your time !!

Following your suggestion, what I'm going to do now is create a StoreCreation.as file that extends GameObjects instead of Sprite (right??)

If your GameObjects class extends from Sprite - yes, StoreCreation will automatically be extended hierarchically from Sprite. But do not miss that thing that you should clean up your game object storage when objects does not needed anymore - that's performance important tip.

I think you on the right way :)
Hey Deft, thanks so much for your reply.

Yes indeed, GameObjects extends from Sprite and now StoreCreation extends from GameObjects.

So far so good.

I already created the StoreCreation and I can now eventListenerClick for the first object !! so far ;)

I think, as you say, I'm in the right track.

Ok now, I'm working on inserting the objects in the inventory.

To do this, I have mainInventory (right now an array) inside all the global vars of the game. Because the inventory will be global (and will keep the objects bought during all the game), I am working on adding the objects clicked in the StoreCreation to it.

Back in the StartGame.as, I'm looking the way to show those objects already inside mainInventory.

I'm on this right now, but ANY suggestion will be awesome !! smile.png

EDIT 1:
Ok I tried to put 1 object to the inventory array but couldn't. this is all inside StoreCreation.as:


Main._instanceMain.addChild(TeddyBear._image); //successfuly appears in screen

TeddyBear._image.addEventListener(MouseEvent.CLICK, TeddyClick);

function TeddyClick(event:MouseEvent)
{
statistics.mainInventory.push(TeddyBear); //Ok i have mainInventory as global inside Statistics.as.

trace(statistics.mainInventory); // Each time I click that object in the store, I can see that each time, an object is added [TeddyBear] [TeddyBear]
Main._instanceMain.addChild(statistics.mainInventory[0]._image); //This works great, but...
(statistics.mainInventory[0]._image.x = 100; //When I try to put it in his location, it moves the image in the store, as if they were the same !!
}

I'm missing something? This makes complete sense but don't know why is not adding to mainInventory and can't access that instance. It acts like if it were an instance of the instance :S

EDIT 2:
WOW it's been a long night.

Ok so far:
- I was able to move the hero between the rooms :D
- I have a GUI.as now, that stores things like game time, a nice menu when you press ESC. and a group of empty images of game objects. :D
- I create a tempo object inside GUI.as that works as an inventoryObject (taken from GameObjects.as) and I put it in the first empty image of game objects. :D
- I can detect when the hero is in front of the room2's in-game object (table) (for now, will work on the rest with same formula) and the inventoryObject is left over: hero AND table. :D

Now what's left:
URGENT: - Finish the inventory / store system so I can implement the inventory array in the GUI (Please help of EDIT 1 above)
- Make the others hitTests for the rest of the in-game objects. Easy.
- Make the hero look left when user hits left key. Tricky, I was some time doing this and couldn't figure out, tomorrow I will give it another try (later, actually)
- Make the hero's animation (I have the animation in the MC, I just need to play() it the hero moves)
URGENT: -Finish the game design, if player ends game, how to call again to call the MissionSelection function (I will work on what you told me Deft about it) :D

And that's pretty much what's next.

If I get this done fast, I could work on the rest 7 missions. Crap I will have only like 10 more hours before deadline.

If I get it done or note, I am in debt with you guys. Thanks so much for all your support in this desperate moments !!

After this, I will more close to this community, I want to help too. I learn A LOT.

See you in some hours, gonna take a quick nap :)





Thanks so much !!

URGENT: - Finish the inventory / store system so I can implement the inventory array in the GUI (Please help of EDIT 1 above)
- Make the others hitTests for the rest of the in-game objects. Easy.
- Make the hero look left when user hits left key. Tricky, I was some time doing this and couldn't figure out, tomorrow I will give it another try (later, actually)
- Make the hero's animation (I have the animation in the MC, I just need to play() it the hero moves)
URGENT: -Finish the game design, if player ends game, how to call again to call the MissionSelection function (I will work on what you told me Deft about it)

I don't know, was your problem with array push released or not, but anyway: I'd said a few posts later about that thing that Flash engine transfer variables (non-scalar types, such as custom objects) as links, and not as values.
This is means that when you call addChild and use image1, you put some object on the scene. When you trying to add second object on the scene by using same image1 sprite, actually you just adding the same object that already on the game stage. The goal is create second instance of your sprite and that's all.

Turn left/right can be done by reversing source hero image by x axis smile.png In ActionScript it can be done by replacing hero image each time key pressed.

Yeap, playing hero animation is very simple.

This topic is closed to new replies.

Advertisement