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:

Actionscript:
  1. // begins the game by setting up the mc's
  2. function game_begin():void {
  3.  
  4.     // add game mc to stage
  5.     addChild(game.mc);
  6.  
  7.     // add level to game.mc
  8.     var lvl:Level = new Level();
  9.     lvl.name = "gamelevel";
  10.     game.mc.addChild(lvl);
  11.  
  12.     // set the items in the stage
  13.     set_level_items(lvl);
  14.  
  15.     // add hero to game.mc, place in center of game screen
  16.     game.mc.addChild(hero.mc);
  17.     hero.mc.x = game.sw / 2;
  18.     hero.mc.y = game.sh / 2;
  19.  
  20.     // add menu
  21.     var igm:InGameMenu = new InGameMenu();
  22.     igm.name = "ingamemenu";
  23.     game.mc.addChild(igm);
  24.  
  25.     // add event listeners
  26.     stage.addEventListener(Event.ENTER_FRAME, game_loop);
  27.     stage.addEventListener(KeyboardEvent.KEY_DOWN, keys_down);
  28.     stage.addEventListener(KeyboardEvent.KEY_UP, keys_up);
  29.  
  30. }

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:

Actionscript:
  1. // set the level items in the level that was attached, passing the level movie clip
  2. function set_level_items(lvl_mc:MovieClip):void {
  3.  
  4.     // get the number of children in the level
  5.     var num_children:int = lvl_mc.numChildren;
  6.     for(var i:int = 0; i <num_children; i++) {
  7.  
  8.         // holder of movieclips
  9.         var mc = lvl_mc.getChildAt(i);
  10.  
  11.         // if the movieclip has the name "item" in it, then it must be
  12.         if(mc.name.indexOf("item") != -1) {
  13.  
  14.             // split it so we get get in return, 'item', '*item name*', '*item quanity*'
  15.             var values:Array = mc.name.split("_");
  16.  
  17.             // we just want to tell it to stop on that item name
  18.             mc.gotoAndStop(values[1]);
  19.  
  20.         }
  21.  
  22.     }
  23.  
  24. }

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:

Before

Actionscript:
  1. } else if(mc.name.indexOf("gold") != -1) {
  2.  
  3.     hit_mc = hit_test(hero.mc, mc, true);
  4.  
  5.     if(hit_mc) {
  6.         lvl_mc.removeChild(mc);
  7.     }
  8.  
  9. }

After

Actionscript:
  1. } else if(mc.name.indexOf("item") != -1) {
  2.  
  3.     // will return either true or false depending if our hero hit the item
  4.     hit_mc = hit_test(hero.mc, mc, true);
  5.  
  6.     // if true, lets remove the item and it to our inventory
  7.     if(hit_mc) {
  8.  
  9.         // split it so we get get in return, 'item', '*item name*', '*item quanity*'
  10.         var values:Array = mc.name.split("_");
  11.  
  12.         // add to inventory
  13.         inventory_action("add", values[1], int(values[2]));
  14.  
  15.         // refresh the inventory menu IF the inventory menu is open... this helps keep the inventory status up to date
  16.         if(game.mc.getChildByName("ingamemenu").currentLabel == "inventory") {
  17.             game.mc.getChildByName("ingamemenu").nextFrame();
  18.         }
  19.  
  20.         // remove it
  21.         lvl_mc.removeChild(mc);
  22.  
  23.         // break out of the for loop so we don't cause an error when looping through the children of the level movieclip
  24.         break;
  25.  
  26.     }
  27.  
  28. }

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:

Actionscript:
  1. // if we are going to add an item
  2. } else if(action == "add") {
  3.  
  4.     // checks if we found the item or not when looping
  5.     var found:Boolean = false;
  6.  
  7.     // loop to check if we have the item, if we have the item to already increment the count
  8.     for(i = 0; i <20; i++) {
  9.         if(hero.inventory[i]) {
  10.             if(hero.inventory[i][0] == item) {
  11.                 hero.inventory[i][1] += count;
  12.                 found = true;
  13.             }
  14.         }
  15.     }
  16.  
  17.     // if found is still false, we add it, IF hero.inventory.length is less than 20
  18.     if(hero.inventory.length <20) {
  19.         if(!found) {
  20.             hero.inventory.push(new Array(item, count));
  21.         }
  22.     }
  23.  
  24. }

Here is the final result:

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>