dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Inventory system

Threaded View

  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.

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