dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Inventory system

  1. #1
    Truimagz.com everfornever's Avatar
    Join Date
    Sep 2006
    Location
    St. Louis
    Posts
    1,306

    Inventory system

    Hi guys well I just wipped up an inventory system, I think works well.

    Obviously it can be expanded on, but here it is for those needing such a thing.

    Let me know if you need more advanced options for it, I'll wip them up.

    Code:
    /* RE-USABLE INVENTORY FUNCTION */
    function createInventory(invName:String, totalSlots:Number, inventoryLength:Number, xPos:Number, yPos:Number) {
    	/* VARS */
    	var rows:Number = 10;
    	var pos:Number = 10;
    	var slots:Number = 0;
    	var spacing:Number = 0;
    	var borderSpacing:Number = 15;
    	/* INVENTORY CLIP */
    	this.createEmptyMovieClip(invName, this.getNextHighestDepth());
    	this[invName]._x = xPos;
    	this[invName]._y = yPos;
    	/* ATTACH INVENTORY SLOTS */
    	for (var i:Number = 0; i<totalSlots; i++) {
    		this[invName].attachMovie("slot", "slot"+i, this[invName].getNextHighestDepth());
    		this[invName]["slot"+i]._x = pos;
    		this[invName]["slot"+i]._y = rows;
    		this[invName]["slot"+i]._x += spacing;
    		/* ATTACH ITEMS THAT PLAYER HAS */
    		this[invName]["slot"+i].attachMovie(_root.inventoryItems[i], _root.inventoryItems[i], this[invName]["slot"+i].getNextHighestDepth());
    		this[invName]["slot"+i][inventoryItems[i]].onPress = function() {
    			/* REMOVE THE ITEM FROM OUR INVENTORY ARRAY */
    			_root.inventoryItems.removeItem(this._name);
    			//--and remove it from our inventory
    			this.removeMovieClip();
    			/* CALL WHATEVER FUNCTION YOU SETUP FOR YOUR ITEMS */
    			//--------------------------------------------------
    		};
    		pos = this[invName]["slot"+i]._x;
    		spacing = this[invName]["slot"+i]._width;
    		slots++;
    		if (slots == inventoryLength) {
    			spacing = 0;
    			slots = 0;
    			pos = 10;
    			rows += this[invName]["slot"+i]._height;
    		}
    	}
    	/* DRAW A BORDER AND FILL FOR OUR INVENTORY */
    	var h:Number = this[invName]._height+borderSpacing;
    	var w:Number = this[invName]._width+borderSpacing;
    	with (this[invName]) {
    		beginFill(0x999999);
    		lineStyle(3, 0x666666);
    		lineTo(0, h);
    		lineTo(w, h);
    		lineTo(w, -borderSpacing);
    		lineTo(0, -borderSpacing);
    		endFill();
    	}
    	/* CREATE A TEXT FIELD TO GIVE OUR INVENTORY A NAME */
    	this[invName].createTextField("inventoryName", this[invName].getNextHighestDepth(), 5, -borderSpacing, 50, 20);
    	var invFont:TextFormat = new TextFormat();
    	invFont.color = 0x666666;
    	invFont.bold = true;
    	invFont.font = "verdana";
    	this[invName].inventoryName.selectable = false;
    	this[invName].inventoryName.type = "static";
    	this[invName].inventoryName.text = this[invName]._name;
    	this[invName].inventoryName.setTextFormat(invFont);
    	/* ATTACH THE CLOSE BUTTON */
    	var closeInv = this[invName].attachMovie("X", "X", this[invName].getNextHighestDepth());
    	closeInv._x = this[invName]._width-this[invName]["slot1"]._width/2;
    	closeInv._y = -borderSpacing;
    	closeInv.onPress = function() {
    		this._parent.removeMovieClip();
    	};
    }
    /* INVENTORY ITEMS */
    var inventoryItems:Array = new Array();
    /* REMOVING AN INVENTORY ITEM FUNCTION*/
    Array.prototype.removeItem = function(item) {
    	for (var j:Number = 0; j<this.length; j++) {
    		if (this[j] == item) {
    			this.splice(j, 1);
    		}
    	}
    	return false;
    };
    you can see it live here, as well as download the live .fla

    http://www.creativededication.com/fo...topic.php?t=25

    you can also read more about how it works here
    Last edited by everfornever; 05-14-2007 at 04:38 PM.

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    i'm not saying this is bad, it works and does what you said, but its not a very self contained system. A copy/paste inventory system should have basic methods like so:

    add item, remove item, open inventory, close inventory.

    currently anyone wanting to add an item has to push it to an array (which they really shouldn't have to worry about knowing the name of) remove the movieclip, open the inventory.

    try creating the system as a prototype (if you want AS1 classes) or a normal class.

    to be extra fancy you could make the inventory extend an EventDispatcher class and dispatch events to custom objects when inventory actions are made.

    also some optimizations for you:

    //remove item

    var i:Number = this.length

    while(--i >= 0){

    if(this[ int(i) ] == item){

    this.splice(i, 1)
    return true
    }
    }

    return false
    }

    //shortcutting

    var clip = this.createEmptyMovieClip(invName, this.getNextHighestDepth());

    var slot = clip.attachMovie("slot", "slot"+i, clip.getNextHighestDepth());

    slot._x = pos;
    slot._y = rows;
    lather yourself up with soap - soap arcade

  3. #3
    Truimagz.com everfornever's Avatar
    Join Date
    Sep 2006
    Location
    St. Louis
    Posts
    1,306
    good ideas malle, Ill prolly be playing around with it more, I made on launch break, and just thought it was kinda cool, ill prolly spend time evryday on it, just to see what I can come up with.

    thank you for your helpfull tips as well

  4. #4
    2KHeroes / Sylvaniah designer luxregina's Avatar
    Join Date
    Jul 2001
    Location
    Somewhere between Kirlundin and Anskaven
    Posts
    1,273
    You might also consider making it a drag and drop interface ? I think people usually like that better ... I came up with mine a while ago, but I didn't really tried to be versatile code wise, wich is a bit stupid ...

    http://www.luxgames.net/FK/inventory.htm (no preloader, drag and drop on the slots, outside to discard - some objects can't be equipped on some slots ... )

  5. #5
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Any particular reason why this isn't in a class? Seperating it into a model-view-controller pattern would make it more expandable, easier to modify, and cleaner, plus allow anyone to change only the data storage/logic/interface as they need to.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  6. #6
    Truimagz.com everfornever's Avatar
    Join Date
    Sep 2006
    Location
    St. Louis
    Posts
    1,306
    well like i said it was just something i made up on break, because someone at another forum was having inventory troubles, so i thought Id write up the basics.

    But after hearing some responses here on it maybie I'll just play with it everyday on break and see how uber I can make it.

    Thanks again for the suggestions, all good ideas, Ill keep this thread updated as I make it more versatile.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center