Free ActionScript

Flash AS2 & AS3 Tutorials, Game Code, Effects, Source Files & Sample Downloads

Drag, Drop and Snap Inventory System

10 Comments »

My latest AS3 Game Inventory System.

Features

  • Drag, drop and snap
  • Create/destroy inventory containers
  • Create/destroy level containers
  • Move items between different movieclip containers
  • Create/destroy items in level
  • Create/destory items in inventory – new
  • Find empty inventory slot – new
  • Added item selected/diselected state – new
  • Added slot full/empty state – new
  • Code optimization and more comments – new

Preview – v1.1.2

Download Fla Sample

Download Fla Sample


Wishlist

  • Stackable Items – items of the same type can be placed in the same slot
  • Auto-pickup item – items placed in bag automatically, if bag has space
  • Multiple Level containers – different game levels
  • Multiple Inventory containers – bag, bank, store
  • Destroy items
  • Sell items
  • Show Item Tooltips

Bug Fixes
v1.1.2

  • Fixed bug when releasing mouse over item, while holding nothing – Thanks 19greg96!

v1.1.1

  • Fixed “slot full” bug after removing item from slot – Thanks 19greg96!
  • Fixed publishing issue for Flash Player 10

AS2
Older AS2 version is available here:
http://www.freeactionscript.com/2008/11/drag-drop-snap-inventory-system-v2/

 

 

10 Responses

19greg96 from Twitter wrote:
I don’t know if you are still working on the inventory system, but I found a bug: it’s that the slots aren’t reusable.

I’ll have to fix that.

-PR

  • ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at com.freeactionscript.inventorySystem::DisplayManager/dropItem()
    at com.freeactionscript.inventorySystem::DisplayManager/onItemDropped()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at com.freeactionscript.inventorySystem.display.item::DefaultItem/onMouseUpHandler()

    - start to drag on the side of the inventory (ex. press down on the “inventory” text) and drop on to a full slot..
    - thx for the mention, and np..

  • Interesting bug Greg, thanks! No idea how you caught that :)

    All fixed now in v1.1.2. It was happening because when you release the mouse over an item, it dispatches an “item dropped” event, even if you aren’t holding anything. I added a flag to make sure an item was being dragged before dispatching said event.

    Thanks again,
    PR

  • Hey PRadvan! Thx for your script! it’s very useful and well done!!
    I’m using it, modifiing it, improving it…
    But I found some bugs (Still.. I’m a novice so it might not be bugs.)
    I posted an error on FlashKit forum where you posted this inventory script.
    http://board.flashkit.com/board/showthread.php?t=824574

    and for improvement I change something about the registration point of item when hitTesting tempslot while draggin item.
    Your hitTesting for placing object is glitchy.

    Look at: com.freeactionscript.inventorySystem.DisplayManager
    around line 266 in the DropItem function:

    // if item hitting slot, and slot is empty
    if ($item.hitTestObject(tempSlot) && !tempSlot.hasItem())
    {
    // set flag for item to be placed in slot
    placingInSlot = true;
    selectedSlot = tempSlot;
    }

    I Replaced the hitTestObject by hitTestPoint:

    if (tempSlot.hitTestPoint($item.x, $item.y , true) && !tempSlot.hasItem())
    {
    //blabla}

    The result is much better!

    Thx for your work my friend I’m learning very fast thx to your CLEAR scripts !! +++ amazing

  • Thanks again, JessiJames! Great tips!!!

    Much appreciated,
    PR

  • Strange bug that causes the items to “jump” when clicked before dragging. No one else mentioned this so maybe it’s something related to different versions of Flash player/plugin OR maybe it’s suppose to do that?

    Seems related to the “newX” and “newY” variables in the DisplayManager.as. They are declared initially as:

    var newX:int = $slot.width / 2;
    var newY:int = $slot.height / 2;

    $item.x = newX;
    $item.y = newY;
    $item.lastX = newX;
    $item.lastY = newY;

    Once you start dragging the item jumps to the correct location. I think initially on mousedown it “jumps” to the location of “slot” height and width. the items consistently jump to the same spot relative the items original location. when clicked in the “left box” they always jump to x20 and y20 which is the location of the slot. In the level container they jump on the X. Haven’t figured out that location, but it seems related to the storage container slots.

    Cool though. Very cool. Just started learning AS3 and working on a “simple” game that can use this. Trying to hack through this bug on my own. In the last week or so I finally got the hang of classes in AS3. Been YEARS since I even used Flash and actionscript (Flash MX). BIg changes.

  • Hi, this script is awesome. 1 q: can the inventory grid be updated on the fly? say by default starts as 6×3, and I put an event listener, and if I press + then in changes into 6×4? so after I press + the Inventory visually becomes 6×4 with the extra slots already usable. And if I press – (minus), then back to 6×3, etc

    thanks!

  • i want to use function
    1.findEmptySlot(inventory.as)
    2.DestroyInventory and DestoryLevel
    all function use in Main.as
    help me please

  • i want use
    function findEmptySlot
    in function Main.as

    Do you help me please?

  • Leave a Reply

    You must be logged in to post a comment.