RPG Item Gathering / Pick Ups

October 7th, 2008

This is a slight continuation from the last tutorial of the RPG series, and it was requested by Chris.

In the last tutorial, we have set up our Intentory menu and allowed our Hero to use selected items and drop the items once the item’s quantity was zero. Now let me share with you how we gather/pick up the items that are laid across the level.

I will admit upfront, this way of creating item gathering is very different from what you might expect, buts its quite useful and easier to work with once you get the concept of it.

To begin, rename your movie clip “gold” in your Library to a more suitable name like “items”. So instead of creating dozens of movie clips for each item in your game, we can place them all in one movie clip. Double click on the movie clips new name to bring it on to the stage. Now in each frame (or every other 5 frames preferred) we will draw an item. Also, for each item we create, we label that frame name to the corresponding name of that item. For example, in Frame 1 you could draw a gold coin and label Frame 1 “Gold”. In Frame 5 you could draw a potion bottle and label Frame 5 “Potion”. You get the idea. Remember when doing this, to make sure you label the item name as it would appear in the inventory menu. Add don’t create items that don’t exist unless you create them in the “InventoryImages” movie clip and added it to the “game_item” variable on the main timeline.

This is what my “items” movie clip looks like:

Next double click on the “Level” movie clip to open it up on the stage. Click on the movie clip where the “gold” used to be and rename the instance name like so: “item_Potion_1”. Copy and paste the same movie clip at a another location on the Level and name it: “item_Key_1”. The reason for the weird naming will be explained very soon.

Go back to your main timeline and open your Actions Panel. Find the function game_begin and update the function with these lines of code:

// begins the game by setting up the mc’s
function game_begin():void {

// add game mc to stage

// add level to game.mc
var lvl:Level = new Level();
lvl.name = “gamelevel”;

// set the items in the stage

// add hero to game.mc, place in center of game screen
hero.mc.x = game.sw / 2;
hero.mc.y = game.sh / 2;

// add menu
var igm:InGameMenu = new InGameMenu();
igm.name = “ingamemenu”;

// add event listeners
stage.addEventListener(Event.ENTER_FRAME, game_loop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keys_down);
stage.addEventListener(KeyboardEvent.KEY_UP, keys_up);


The only real difference from these new lines of code from the old lines is that we added line 13, a function set_level_items. Also note, when we execute this function, we are passing the level movie clip that we added to the stage to the function. Lets create the function write now. Right above the game_begin function, add these lines of code:

// set the level items in the level that was attached, passing the level movie clip
function set_level_items(lvl_mc:MovieClip):void {

// get the number of children in the level
var num_children:int = lvl_mc.numChildren;
for(var i:int = 0; i < num_children; i++) {

// holder of movieclips
var mc = lvl_mc.getChildAt(i);

// if the movieclip has the name “item” in it, then it must be
if(mc.name.indexOf(“item”) != -1) {

// split it so we get get in return, ‘item’, ‘*item name*’, ‘*item quanity*’
var values:Array = mc.name.split(“_”);

// we just want to tell it to stop on that item name




This function will loop through all the movie clips within the level, and if the movie clip has an instance name of “item” in it, it will tell that movie clip to gotoAndStop on that frame – the item. Remember the unusual instance name we gave our items in the “Level” movie clip, “item_Potion_1”? We use this to determine what kind of movie clip it is (an item), what kind of item it is (a Potion), and what is the quantity when the Hero picks it up (just 1 in this case, but can be any number).

If you go ahead and test your movie, everything should be in working order: your items placed on your stage should show the correct image/frame. But if you try having your Hero walk over the item, he will not pick up! Lets fix this real quick. Go inside the function hero_move and go down to the lines where it checks for the movie clips name to be “gold”. Lets update that part of the code to be like so:

} else if(mc.name.indexOf(“gold”) != -1) {

hit_mc = hit_test(hero.mc, mc, true);

if(hit_mc) {


} else if(mc.name.indexOf(“item”) != -1) {

// will return either true or false depending if our hero hit the item
hit_mc = hit_test(hero.mc, mc, true);

// if true, lets remove the item and it to our inventory
if(hit_mc) {

// split it so we get get in return, ‘item’, ‘*item name*’, ‘*item quanity*’
var values:Array = mc.name.split(“_”);

// add to inventory
inventory_action(“add”, values[1], int(values[2]));

// refresh the inventory menu IF the inventory menu is open… this helps keep the inventory status up to date
if(game.mc.getChildByName(“ingamemenu”).currentLabel == “inventory”) {

// remove it

// break out of the for loop so we don’t cause an error when looping through the children of the level movieclip



1. Checks to see if the movie clip instance name has “index” in it
4. Returns either true or false depending if the Hero is hitting the item/movie clip
7. If true continue inside the if block statement
10. Split the movie clip instance name into an array. This helps us keep track of the item name and item quantity
13. Execute the function inventory_action passing the values “add”, values[1], and values[2] (which again, keeps track of the item name and quantity)
16 -18. Here we check to see if the Inventory menu is open, and if it is, refresh it self. (the next frame just tells it to go back on the same frame – which basically loops itself, to refresh the items and quantity count)
24. We use break to prevent any errors.

All that is left to do is alter the inventory_actions function to add items to the Hero’s inventory. In the inventory_actions function add these lines of code in continuation of if and else if:

// if we are going to add an item
} else if(action == “add”) {

// checks if we found the item or not when looping
var found:Boolean = false;

// loop to check if we have the item, if we have the item to already increment the count
for(i = 0; i < 20; i++) { if(hero.inventory[i]) { if(hero.inventory[i][0] == item) { hero.inventory[i][1] += count; found = true; } } } // if found is still false, we add it, IF hero.inventory.length is less than 20 if(hero.inventory.length < 20) { if(!found) { hero.inventory.push(new Array(item, count)); } } } [/as] Here is the final result: [swfobj src="/wp-content/uploads/rpg/part.2.2.swf" width="600" height="400"] And here is the final to download

AS3 > Games > RPG

2 Responses to “RPG Item Gathering / Pick Ups”

  1. Eric Smith Says:

    Great work on this site, Gregory. You have some nice flash pieces. Keep writing, I’ve subscribed. Connect me with your friend Dhryn, and maybe the three of us can collaborate on a project.

  2. Chris Palmer Says:

    Thankyou! Was much needed =)

Leave a Reply

Enter code snippets inside of <pre> and </pre>