00:00
00:00
Newgrounds Background Image Theme

TheADHX just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

[as2] Inventory Help (array System)

4,481 Views | 36 Replies
New Topic Respond to this Topic

This is for a Point and Click Game!

Ok guys i really need help with this. I have read every topic regarding inventories here on NG but i just dont get it...

I can easily do the on(press) system but that isn't very smart when i'm planing using a lot of objects i think.

I understand how array works but i can't make it work (i'm really new to AS).
Can someone help me with code? I know it seems i am going the lazy way but its not the case, i just can't make it work.
A working fla would be wonderful and would really help.

Thanks by the way :)

Response to [as2] Inventory Help (array System) 2013-02-19 13:13:10


Yes it makes you looks like lazy.

Anway, first since you are new to actionscript, I not only suggest but highly recommend changing to AS3. Second, do not code inside symbols. Third start by the simplest things, an inventory is not so easy of cake for beginning with.

Play a bit with arrays, follow the Actionscript 3.0 Language Reference and any doubts you might come in just post here in the forum, people will be willing to help someone who made the code themselves.

Response to [as2] Inventory Help (array System) 2013-02-20 04:03:28


At 2/19/13 01:13 PM, Spysociety wrote: Yes it makes you looks like lazy.

Anway, first since you are new to actionscript, I not only suggest but highly recommend changing to AS3. Second, do not code inside symbols. Third start by the simplest things, an inventory is not so easy of cake for beginning with.

Play a bit with arrays, follow the Actionscript 3.0 Language Reference and any doubts you might come in just post here in the forum, people will be willing to help someone who made the code themselves.

Yea man but the problem is that this is a project i promised myself i would do and i already have lots of things done. After this i will move on to AS3 .
I needed something like this http://www.newgrounds.com/dump/item/d4133964b68c00e7712fd4bd 11603e54

Now, i know in theory what i need. I know i need to create a certain number of inventory boxes, name the items and then make an array so that i can do something like
[sword,1]
[pencil,2]
[thing,3]

Right?
Like i said, i know it sounds lazy but its not the case. I really need help with this and a fla. would be great (believe it or not that is how i learn better, seeing the thing done and then after understanding how it works make one for myself (I dont copy+paste)).

Response to [as2] Inventory Help (array System) 2013-02-20 12:36:49


var inventory = []; // inventory array
var inventory_max = 10; //limit spots to 10

function howMany(thing) { // Finds the number of things (sword,thing, etc) in your inventory
// Returns that number
var num = 0;

for(var i = 0; i < _root.inventory.length;i++) {

if(_root.inventory[i].name == thing) num += 1;

}

return num;
}

function addThing(name) { //Creates an empty movieclip, and attaches a movieclip from library
// using the parameter 'name' as the asset name in the library.

var hold = _root.createEmptyMovieClip(name + howMany(name), _root.getNextHighestDepth());
hold.attachMovie(name, "mc", hold.getDepth()+1);
_root.inventory.push(hold);
}

//adds newly created movieclip to your inventory.

addThing("sword"); //example

I didn't know exactly what you were looking for


BBS Signature

Response to [as2] Inventory Help (array System) 2013-02-20 12:38:15


var inventory = [];
var inventory_max = 10;

function howMany(thing) {

var num = 0;

for(var i = 0; i < _root.inventory.length;i++) {

if(_root.inventory[i].name == thing) num += 1;

}

return num;
}

function addThing(name) {

var hold = _root.createEmptyMovieClip(name + howMany(name), _root.getNextHighestDepth());
hold.attachMovie(name, "mc", hold.getDepth()+1);
_root.inventory.push(hold);
}

addThing("sword");
addThing("pencil");
addThing("thing");

sorry for the double post, looked shitty.


BBS Signature

Response to [as2] Inventory Help (array System) 2013-02-21 10:54:07


At 2/20/13 12:38 PM, LunarHawk69 wrote:

:lovelythingsshewrotebecauseshesawesome

Ok, i tried it but it seems it isn't working (I've uploaded an image).
I named the objects, i linked them (export to actionscript right? To give an identifier so that you can pull them from the library by the name) and i even tested some other functions to see if i was doing something stupid haha.

The way i'm trying to do is that by clicking in the object he goes to the inventory.
I did a this.Item1.onRelease = addItem(Item1);
Isn't that the right method?

[as2] Inventory Help (array System)

Response to [as2] Inventory Help (array System) 2013-02-21 13:24:46


At 2/21/13 10:54 AM, Filipus wrote: I did a this.Item1.onRelease = addItem(Item1);
Isn't that the right method?

You know nothing about As2. You're nowhere near anything in this project.
Stop copy&pasting horrible code that you don't understand.

Code this in As3.

Response to [as2] Inventory Help (array System) 2013-02-21 15:58:50


At 2/21/13 01:24 PM, milchreis wrote:
At 2/21/13 10:54 AM, Filipus wrote: I did a this.Item1.onRelease = addItem(Item1);
Isn't that the right method?
You know nothing about As2. You're nowhere near anything in this project.
Stop copy&pasting horrible code that you don't understand.

Code this in As3.

My comment would be a less harsh version of the above.

Do it in AS3, you do not have anything done yet. You'll also need to read up on the basics of coding; arrays and functions are really basic concepts you must first understand before making anything


- Matt, Rustyarcade.com

Response to [as2] Inventory Help (array System) 2013-02-21 16:15:42


Use Actionscript 1, it is the best one to use

Response to [as2] Inventory Help (array System) 2013-02-21 17:04:39


At 2/21/13 04:15 PM, cheese123 wrote: Use Actionscript 1, it is the best one to use

yeah, and _parent and _root are your best friends.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to [as2] Inventory Help (array System) 2013-02-22 03:48:07


At 2/21/13 03:58 PM, Rustygames wrote:
At 2/21/13 01:24 PM, milchreis wrote:
At 2/21/13 10:54 AM, Filipus wrote: I did a this.Item1.onRelease = addItem(Item1);
Isn't that the right method?
You know nothing about As2. You're nowhere near anything in this project.
Stop copy&pasting horrible code that you don't understand.

Code this in As3.
My comment would be a less harsh version of the above.

Do it in AS3, you do not have anything done yet. You'll also need to read up on the basics of coding; arrays and functions are really basic concepts you must first understand before making anything

I know perfectly well how arrays and functions work, i just can't implement them well in AS2.
I have already done things (that are something to me), that image isn't the project... It was just an inventory test.

Can you guys please help instead of trying to put a guy down? I just need help with this then i will go to AS3. Please?

Response to [as2] Inventory Help (array System) 2013-02-22 07:25:14


You must assign a function to an onRelease handler.

Function arguments have a name and a type.

I know perfectly well how arrays and functions work

No you don't. Stop lying.


If you're a Newgrounds OG who appreciates Flash games with depth, check out the game I made in 2024.

Response to [as2] Inventory Help (array System) 2013-02-22 11:07:05


At 2/22/13 07:25 AM, Jin wrote: You must assign a function to an onRelease handler.

Function arguments have a name and a type.

I know perfectly well how arrays and functions work
No you don't. Stop lying.

They have a type? Like this.Item1.onRelease = addItem(Item1:String) ?

Well, i tough i knew... Maybe i dont ),:
I'm reading lots of articles/tutorials, let's see if i understand what you meant

Response to [as2] Inventory Help (array System) 2013-02-22 12:21:12


I'm a newb too.

Are you looking for something that works like this?

http://www.newgrounds.com/dump/item/14194fd8efb885097be7ce14 cc4da0f7

Do you really know how arrays work?

for instance, if I have a dynamic text box with an instance name 'people':

names = ['toby','john','fred']
i = 0;
people.text = names[i]

What will the text box show me? If you can't answer that much, you need to go to basics.

Response to [as2] Inventory Help (array System) 2013-02-22 12:24:59


Response to [as2] Inventory Help (array System) 2013-02-23 08:31:33


At 2/22/13 11:07 AM, Filipus wrote:
At 2/22/13 07:25 AM, Jin wrote: You must assign a function to an onRelease handler.

Function arguments have a name and a type.

I know perfectly well how arrays and functions work
No you don't. Stop lying.
They have a type? Like this.Item1.onRelease = addItem(Item1:String) ?

Well, i tough i knew... Maybe i dont ),:
I'm reading lots of articles/tutorials, let's see if i understand what you meant

Okay if you want to use AS2 do it, I would really consider AS3 though as it's easier.

The problem you're having with your onRelease is that you're calling the function, so it's returning it's value and you're assining the onRelease to that, which is likely going to cause an error.

Lose the params and the function will call:

this.item1.onRelease = addItem;

However now you don't know which item was clicked. In AS3 it would be an event and you'd get the target (hence it's easier) but in AS2 you're going to have to use a custom delegate class (which is slow and nasty). You'll have to google for one which takes parameters because I don't have one to hand I'm afraid.

this.item1.onRelease = Delegate.create(this, addItem, 1);

Not that this time I'm calling a fuction and using it's return value as my onRelease but that's okay because Delegate.create returns a function. I know it's a little strange to think of a function as an object.

Let me know if you need anything else and I'll try to help if I see it, but honestly mate AS3 is going to help you out a lot here


- Matt, Rustyarcade.com

Response to [as2] Inventory Help (array System) 2013-02-24 15:44:41


At 2/22/13 12:21 PM, cheese123 wrote: I'm a newb too.

Are you looking for something that works like this?

http://www.newgrounds.com/dump/item/14194fd8efb885097be7ce14 cc4da0f7

Do you really know how arrays work?

for instance, if I have a dynamic text box with an instance name 'people':

names = ['toby','john','fred']
i = 0;
people.text = names[i]

What will the text box show me? If you can't answer that much, you need to go to basics.

Toby.
Arrays start at 0 if i remember correctly.
And that is exactly what i want! :D
The drag and combine thing i can handle.

----------------------------

@guyabove

Hm... I had never heard of that so i'll have to look into it. Thanks for the help :)

Response to [as2] Inventory Help (array System) 2013-02-24 17:58:58


Ok, I'll try and help you.

Do you know about 'while' loops?

Feel free to send me a PM. I won't really supply you with any code though (i cba), it's much better if you write it all out anyway.

Response to [as2] Inventory Help (array System) 2013-02-24 18:45:09


At 2/24/13 05:58 PM, cheese123 wrote: Ok, I'll try and help you.

Do you know about 'while' loops?

Feel free to send me a PM. I won't really supply you with any code though (i cba), it's much better if you write it all out anyway.

Cheese, you wrote the brilliant line of code.
"if (inventory[selectedSlot]._currentframe = 2){" -cheese123 2/21/12

I don't think you're in line to be helping people with inventory systems.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to [as2] Inventory Help (array System) 2013-02-24 19:05:06


At 2/24/13 06:45 PM, MintPaw wrote:
At 2/24/13 05:58 PM, cheese123 wrote: Ok, I'll try and help you.

Do you know about 'while' loops?

Feel free to send me a PM. I won't really supply you with any code though (i cba), it's much better if you write it all out anyway.
Cheese, you wrote the brilliant line of code.
"if (inventory[selectedSlot]._currentframe = 2){" -cheese123 2/21/12

I don't think you're in line to be helping people with inventory systems.

what's wrong with that

anyway no one else is really helping him. but seriously what's wrong with it.

Response to [as2] Inventory Help (array System) 2013-02-24 19:08:47


sometimes you seasoned programmers can be discouraging. could have at least told me why it was a bad line of code.

Response to [as2] Inventory Help (array System) 2013-02-24 20:03:07


At 2/24/13 07:08 PM, cheese123 wrote: sometimes you seasoned programmers can be discouraging. could have at least told me why it was a bad line of code.

It should be obvious, you used one equal sign instead of two, the condition will always be true regardless of what frame it's on. You don't even understand really basic things like variables and if statements.

People have tried helping you both, telling you not to use global variables, not to use a language that's over a 6 years old, it seems like newer programmers just want a one click solution that doesn't take any work and will ignore anything that seems hard.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to [as2] Inventory Help (array System) 2013-02-24 20:23:56


At 2/24/13 08:03 PM, MintPaw wrote:
At 2/24/13 07:08 PM, cheese123 wrote: sometimes you seasoned programmers can be discouraging. could have at least told me why it was a bad line of code.
It should be obvious, you used one equal sign instead of two, the condition will always be true regardless of what frame it's on. You don't even understand really basic things like variables and if statements.

People have tried helping you both, telling you not to use global variables, not to use a language that's over a 6 years old, it seems like newer programmers just want a one click solution that doesn't take any work and will ignore anything that seems hard.

Don't use a language over 6 years old. Really? I hope you realize how silly a comment that was :)

Besides being a little bit horrid in the last couple of posts, MintPaw has a good point. It makes me cringe when someone brand new starts "teaching" someone else.

Honestly fella's, read a book from cover to cover, you'll learn so much and it'll be worth it in the long run. This book is really fucking good: http://www.amazon.co.uk/Learning-ActionScript-3-0-Beginners-
Guide/dp/059652787X


- Matt, Rustyarcade.com

Response to [as2] Inventory Help (array System) 2013-02-24 20:35:48


At 2/24/13 08:03 PM, MintPaw wrote:
At 2/24/13 07:08 PM, cheese123 wrote: sometimes you seasoned programmers can be discouraging. could have at least told me why it was a bad line of code.
It should be obvious, you used one equal sign instead of two, the condition will always be true regardless of what frame it's on. You don't even understand really basic things like variables and if statements.

People have tried helping you both, telling you not to use global variables, not to use a language that's over a 6 years old, it seems like newer programmers just want a one click solution that doesn't take any work and will ignore anything that seems hard.

By what you posted I thought there was a problem with the way I worded the code (maybe there is?) but my code was working perfectly so I must have used 2 equal signs in the actual flash file.

Yea ok I don't have a 100% understanding of actionscript, but it's a bit harsh to say I am after a one click solotion. I didn't copy and paste any code at all for this inventory system. Granted I searched a tutorial (http://www.newgrounds.com/portal/view/448642) but that code didn't really work in my file - yes it was a one click solution at first, so I decided to try and improve on it.

The for loop seemed unnecessary, why would I need all the buttons? An array would do fine here. I just needed the while loop. Instead of messing about coding in movie clips I coded it all in the timeline. Ok you can call that copying and pasting to an extent, but I improved on something didn't I?

If I didn't understand variables how did I even build this inventory system. How was I able to make it so that flash stores what slot I selected, or refer to a specific comment (also stored in an array) when I picked up a certain item.

Ok this stuff is basic. Really basic and really easy. But I do understand variables (to an extent) I don't think I used a one button solution here. And ok maybe I don't understand if statements 100% (if (x = y) or if (x ==y) - why are these two different) but I understand them well enough to do what I want.

Response to [as2] Inventory Help (array System) 2013-02-24 20:38:09


At 2/24/13 08:23 PM, Rustygames wrote:
At 2/24/13 08:03 PM, MintPaw wrote:
At 2/24/13 07:08 PM, cheese123 wrote: sometimes you seasoned programmers can be discouraging. could have at least told me why it was a bad line of code.
It should be obvious, you used one equal sign instead of two, the condition will always be true regardless of what frame it's on. You don't even understand really basic things like variables and if statements.

People have tried helping you both, telling you not to use global variables, not to use a language that's over a 6 years old, it seems like newer programmers just want a one click solution that doesn't take any work and will ignore anything that seems hard.
Don't use a language over 6 years old. Really? I hope you realize how silly a comment that was :)

Besides being a little bit horrid in the last couple of posts, MintPaw has a good point. It makes me cringe when someone brand new starts "teaching" someone else.

Honestly fella's, read a book from cover to cover, you'll learn so much and it'll be worth it in the long run. This book is really fucking good: http://www.amazon.co.uk/Learning-ActionScript-3-0-Beginners-
Guide/dp/059652787X

I just had a working inventory system, I wanted to share it. It's like 'here you go, I made one work'

Yea my comment was a bit pretentious though, I can't beleive I said I would teach him :P That's not how it was meant to come across. Also actionscript isn't something serious for me, I'll just use it for a basic game from time to time. So what if I am not perfect?

Response to [as2] Inventory Help (array System) 2013-02-24 20:41:36


Wait... I didn't say I would teach him, rather that I would help him.

But yea my comment still looks kinda pretentious. It's not how I meant it. Just wanted to help...

Response to [as2] Inventory Help (array System) 2013-02-24 21:37:08


At 2/24/13 08:41 PM, cheese123 wrote: Wait... I didn't say I would teach him, rather that I would help him.

But yea my comment still looks kinda pretentious. It's not how I meant it. Just wanted to help...

Don't worry dude it's cool, I think we all misunderstood a little. Things comes across as a little heated sometimes but the intentions are good from all parties :)

That book I linked you to is really cool though, I learnt a lot about the display list and event system when I moved over to AS3; I'd recommend it to anyone, even if it is just a hobby.


- Matt, Rustyarcade.com

Response to [as2] Inventory Help (array System) 2013-02-24 21:57:01


At 2/24/13 08:23 PM, Rustygames wrote: Don't use a language over 6 years old. Really? I hope you realize how silly a comment that was :)

Meh, I probably could of rephrased that "Don't use a language that was last relevant over 6 years ago."
The only reason you'd use AS2 at this point is you're too lazy to get up-to-date, it costs nothing but time and effort, I've almost completely resolved to not take the time to help people who won't take the time to help themselves.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to [as2] Inventory Help (array System) 2013-02-24 22:03:43


At 2/24/13 08:35 PM, cheese123 wrote: The for loop seemed unnecessary ... I just needed the while loop

You know that the only significant difference between a while loop and a for loop is syntax, right?
They're both loops and can break your application if used incorrectly.

At 2/24/13 08:35 PM, cheese123 wrote: And ok maybe I don't understand if statements 100% (if (x = y) or if (x ==y) - why are these two different) but I understand them well enough to do what I want.

If you don't understand the difference between the assignment operator and the equality operator then you can't know them well enough to be able to use them to do what you want. They are very different operators and cannot be used interchangeably.

I will, however, provide an answer your question in that part of the post: why are these two different?:

The first argument you provided, (x = y), is assigning the value of "y" to "x" and then returning the value of "y". If you were to put that into an if statement it would always be evaluated as true if "y" is any non-zero value (e.g. a literal 0, false, an empty string, et cetera). In AS3 whenever you assign a value to a variable the value that was assigned will be returned; this means that you can use an assignment in an if statement and the value that is assigned is what will be evaluated in the statement.

The second argument, (x == y), is the equality operator. As the name suggests it determines if two given values are to be considered equal. This is generally the operator you will want to be using in your if statements, opposed to the assignment operator. There are circumstances where you will want to use the assignment operator in an if statement, typically as part of a loop, but I won't go into that since it's not something you're likely to need to do.

And for the sake of learning I'll touch on an operator you did not mention, which is the identity operator. It is three equals signs that looks like (x === y). It is used to determine if "x" and "y" are the exact same object, or, in other words, are identical. It not only determines that they are equal, but, essentially, that they are the occupy same piece of memory as well. Before showing what this operator does it is worth clarifying that in AS3 that pretty much all, if not all, variables you use are references. Sometimes you might want to determine if two variables reference the same piece of memory, even if it's possible that the two variables are referencing separate, but equal, memory locations. That's where the identity operator is used.
Take the following code for example:

var foo:Thing = new Thing(42);
var bar:Thing = foo;
var baz:Thing = new Thing(42);

trace(foo === bar); // Output: true
trace(foo === baz) // Output: false;

"foo" and "bar" are both references to the same memory, and "baz" is a reference to a separate piece of memory, so that is why when you use you identity operator on "foo" and "baz" you get a false result, because the two memory locations are not identical, even though they contain equal data; the memory locations themselves are what differ which is what is important with the identity operator.

Response to [as2] Inventory Help (array System) 2013-02-25 04:05:02


Ok i PM you cheese.

restnothelping@

I already said i will move to AS3 after this. But i want to finish this AS2 project now. Its an inventory guys, it should't be that much work. You are all making a big fuss out of it. "AS2 is outdated, i wont help because you'r stupid if you use it!".