Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 01-20-2012, 02:44 AM   #1
G.Ray.0
Registered User
 
Join Date: Jul 2010
Posts: 16
Default [AS3] Inventory with individual control on items

Hi,

I'm trying to make an inventory. My technique is probably not really good, but this is how I'm going (feel free to correct everything). I have an array of objects with some properties (name, price, quantity)

for (i:uint = 0;i <theArray.length; i++){

var itemName:TextField = new TextField();
itemName.y = i*22 + 320;
itemName.x = 85;
itemName.text = theArray[i].itmName;
addChild (itemName);

I do the same for the price and the quantity of the items so it look like an organised table.

now I have a button buy that increase the quantity of the item next to it when you click.

var buyButton:testButton = new testButton();
buyButton.y = i*22 + 320;
buyButton.x = 300;
addChild (buyButton);

buyButton.addEventListener(MouseEvent.CLICK, buyItem);
} So the loop ends here

how can I associate each button to its matching item in the table so it increase the right quantity. The array length is changing through time so I can't have a defined number of buttons and to refer to an instance of it associated with my object is a concept I don't understand well. I guess there is a way to work that through containers, but I feel like I'm missing something. What kind of approach should I take for a problem like that?

Thank you in advance for your help.
G.Ray.0 is offline   Reply With Quote
Old 01-20-2012, 05:19 AM   #2
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,408
Default

Use an associative array to build the list.
This example requires a *MovieClip in the library for the button: class export name "Btn".
* must be MovieClip; Button symbol will not work for this.

This script goes on frame1 of the main timeline.
ActionScript Code:
import flash.display.Sprite; import flash.text.TextField; import flash.events.MouseEvent; // container for the list var list:Sprite = new Sprite(); // list of quantity textfields var quantityArray:Array = new Array(); // list of items: name | price | quantity var itemsArray:Array = [ {name:"item1", price:1.99, quantity:0}, {name:"item2", price:2.99, quantity:0}, {name:"item3", price:3.99, quantity:0}, {name:"item4", price:4.99, quantity:0}, {name:"item5", price:5.99, quantity:0} ]; // build the lost; pass in the number of items buildList(itemsArray.length); function buildList(n:int):void {     var row:Sprite;     var btn:Btn;     var nf:TextField;     var pf:TextField;     var qf:TextField;     var padX:int = 5;     var padY:int = 20;     var rowY:Number = 0;     for (var i:int = 0; i < n; i++)     {         row = new Sprite();         // the button         btn = makeBTN(i);         // name field         nf = makeTF(itemsArray[i].name);         nf.x = btn.width + padX;         nf.y = btn.height/2 - nf.height/2;         // price field         pf = makeTF(itemsArray[i].price);         pf.x = nf.x + nf.width + padX;         pf.y = nf.y;         // quantity field         qf = makeTF(itemsArray[i].quantity);         qf.x = pf.x + pf.width + padX;         qf.y = nf.y;         // array of quantity fields         quantityArray[i] = qf;                 // construct the row, then add the row to the list         row.addChild(btn);         row.addChild(nf);         row.addChild(pf);         row.addChild(qf);         row.y = rowY;         list.addChild(row);         rowY = list.height + padY;     }         list.x = stage.stageWidth/2 - list.width/2;     list.y = stage.stageHeight/2 - list.height/2;     addChild(list); } function updateQuantity(e:MouseEvent):void {     // convert the text to Number, add 1, then assign to textfield as a String     var q:Number = Number(quantityArray[e.currentTarget.id].text);     q++;     quantityArray[e.currentTarget.id].text = q.toString(); } // utility function for making the textfields function makeTF(s:String):TextField {     var padY:int = 5;     var padT:int = 10;     var tf:TextField = new TextField();     tf.text = s.toString();     tf.width = tf.textWidth + padT;     tf.height = tf.textHeight + padY;     return tf; } // utility function for making the buttons function makeBTN(_id:int):Btn {     var b:Btn = new Btn();     b.id = _id;     b.buttonMode = true;     b.mouseChildren = false;     b.addEventListener(MouseEvent.CLICK, updateQuantity);     return b; }
[afz]snickelfitz is offline   Reply With Quote
Old 01-20-2012, 04:53 PM   #3
G.Ray.0
Registered User
 
Join Date: Jul 2010
Posts: 16
Default

This is some quality answer I got here. Thank you sir and have a great life!
G.Ray.0 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:23 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.