draw empty mx:grid flex programmatically - actionscript-3

I want to draw 2*2 mx:Grid and put a button on any one of these cells which will be calculated dynamically. So I want to create an empty 2*2 grid, and replace any one cell with the button.
-----------------
| | |
------------------
| | btn |
------------------
How do I accomplish this?

You need to create grid and keep references to the cells (instances of GridItem). Consider following code as a part of some class.
private var cells:Array = [];
private function addGrid(parent:DisplayObjectContainer):void {
var grid:Grid = new Grid();
var row1:GridRow = new GridRow();
grid.addChild(row1);
var cell11:GridItem = new GridItem();
row1.addChild(cell11);
var cell12:GridItem = new GridItem();
row1.addChild(cell12);
cells.push(new Array(cell11, cell12));
var row2:GridRow = new GridRow();
grid.addChild(row2);
cell21 = new GridItem();
row2.addChild(cell21);
var cell22:GridItem = new GridItem();
row2.addChild(cell22);
cells.push(new Array(cell21, cell22));
parent.addChild(grid);
}
cells is two-dimensional array, which can be used to access the cells you need to change. You can use Vector class instead of Array. I've used Array only because of shorter code.

Related

how to save array in array randomly

I tried to show a random number in three movieclip boxes below(blue, yellow, red) when push button(grey box). to make random numbers I use math.ceil and I keep it in a var. I make 3 var (so I've 3 formula random number). after that, I want to call that 3 var into 3 new var randomly (hasil,hasil2,hasil3). last, i put the 3 new var into random dynamic text. the dynamic text is in three movieclip boxes.
these the formula random number
mtk = Math.ceil(Math.random()*10);
mtk1 = Math.ceil(Math.random()*20);
mtk2 = mtk+mtk1;
these the dynamic text
mc.jwb1.text = hasil.toString();
mc.jwb2.text = hasil2.toString();
mc.jwb3.text = hasil3.toString();
I tried to call the 3 var in 3 random numbers use 2 arrays, but I still confused about how to call them or you have better idea, Please tell me?
var mc:jwb = new jwb();
var mtk:Number;
var mtk1:Number;
var mtk2:Number;
var hasil:int;
var hasil2:int;
var hasil3:int;
btn.addEventListener(MouseEvent.CLICK, button);
function button(e:MouseEvent):void{
addChild(mc);
}
Batas_mc.addEventListener(Event.ENTER_FRAME, batas);
function batas(e:Event):void{
mc.x = 270;
mc.y = 130;
mtk = Math.ceil(Math.random()*10);
mtk1 = Math.ceil(Math.random()*20);
mtk2 = mtk+mtk1;
//Array object acak
var P:Array = [mtk, mtk1, mtk2];
var M:Array = [hasil, hasil2, hasil3];
while (M.length){
// Get the last MovieClip and remove it from the list.
var Batumc2:MovieClip = M.pop();
// Produce a random Point.
var anIndex:int = Math.random() * P.length;
var aPo = P[anIndex];
// Remove the selected Point from its list.
P.splice(anIndex, 1);
// Move the selected MovieClip to the selected Point coordinates.
Batumc2 = aPo;
}
mc.jwb1.text = hasil.toString();
mc.jwb2.text = hasil2.toString();
mc.jwb3.text = hasil3.toString();
}

Dynamic Y Position of TextField through iterations of a loop

I have a function called dataLoaded that has multiple loops through a nested array full of ingredient items. The aim is to insert each item from the array into a TextField which forms part of a scrolling list. My array structure is like so:
public var so:SharedObject = SharedObject.getLocal("mySharedObject");
public var meat_items_array:Array = new Array(so.data.meat1, so.data.meat2, so.data.meat3);
public var carb_items_array:Array = new Array(so.data.carbs1, so.data.carbs2, so.data.carbs3);
public var other_items_array:Array = new Array(so.data.other1, so.data.other2, so.data.other3);
public var mealtype_items_array:Array = new Array(so.data.mealtype1, so.data.mealtype2, so.data.mealtype3);
public var cuisine_items_array:Array = new Array(so.data.cuisine1, so.data.cuisine2, so.data.cuisine3);
public var veg_items_array:Array = new Array(so.data.veg1, so.data.veg2, so.data.veg3);
public var master_array:Array = new Array(meat_items_array, carb_items_array, other_items_array, mealtype_items_array, cuisine_items_array, veg_items_array);
Here is my dataLoaded function:
private function dataLoaded():void
{
for ( var masterCounter:int = 0; masterCounter < master_array.length; masterCounter++ )
{
for (var nestedCounter:int=0;nestedCounter<master_array[masterCounter].length;nestedCounter++)
{
var txt:String = master_array[masterCounter][nestedCounter];
if (txt==null) continue;
populateItem(txt,nestedCounter);
}
}
}
Originally all of the aesthetic positioning of items in the scrolling list below were done within the dataLoaded function but I have now decided to move all of these things into a new function called populateItems which is called during every iteration of the inner for loop in dataLoaded.
This all works well, except for the fact that the line _item.y = nestedCount * _itemPosition; no longer works as it should (originally items would be placed sequentially in a list, with the y value being calculated by multiplying a constant (_itemPosition) by the value of the counter for each iteration of the inner for loop). Now, 3 items are inputted into the list at once, and in the next iteration of the loop, another 3 items are placed right on top of the previous 3 items, obscuring them.
How do I change my below function so that my _itemPosition is more dynamic through the iterations of the dataLoaded function?
function populateItem(txt:String, nestedCount:Number):void {
_item = new Item();
_item.item_btn_over.alpha = 0;
_itemTextField = new TextField();
_itemTextField.x = _textFieldXPosition;
_itemTextField.y = _textFieldYPosition
_itemTextField.text = txt;
//adds textfield to displaylist//
_item.addChild(_itemTextField);
//vertical positioning//
_item.y = nestedCount * _itemPosition; // ????
//adds items to container displaylist//
_container.addChild(_item);
}
I'm not entirely clear as to what your trying to do, but assuming this code only runs once, this should do what you want:
_item.y = _container.height + padding; //this will always put the next item at the bottom of the container after the previous one (padding can be any number you want)
Here is how this works, let's say you have 3 objects that you are sequentially adding to container in a loop and you're giving each item the y value of container.height. Let's also assume each item has a height of 10:
Loop 1:
Container height is 0 (since there are presently no items in it)
So item1's y is 0
after container.addChild(item1) container
height becomes 10 (the height of item1)
Loop 2:
item2's y is set to container.height, which is presently 10
after container.addChild(item2) container height becomes 20
Loop 3:
item3's y is set to container.height, which is presently 20
after container.addChild(item3) container height becomes 30
If your code is being run multiple times (so _container already has objects in it), then simply run a function like this whenever you need to rearrange:
function orderItem():void {
var padding:int = 5; //5 pixel gap between items
var tmpY:Number = 0;
var tmpItem:DisplayObject;
for(var i:int=0;i<_container.numChildren;i++){
tmpItem = _container.getChildAt(i);
tmpItem.y = tmpY;
tmpY += tmpItem.height + padding;
}
}
This goes through every child of _container and stacks the items vertically.
As an aside, it would be much cleaner to take all this:
_item.item_btn_over.alpha = 0;
_itemTextField = new TextField();
_itemTextField.x = _textFieldXPosition;
_itemTextField.y = _textFieldYPosition
_itemTextField.text = txt;
//adds textfield to displaylist//
_item.addChild(_itemTextField);
and put it in the constructor of Item, then pass in txt as a constructor argument

How to point or reference a dynamically created item

I'm working on an invoice system. I need to add the invoice item dynamically by addchild method as below mentioned
1.select product(combo box)-- Quandity(text box)--Price(text box)--Total(text box)
2.select product(combo box)-- Quandity(text box)--Price(text box)--Total(text box)
My problem is i cant bring the sum amount of all the Total text boxes of each child element..
How to point or refernece a dynamically created item????
You can store all created item in a Array (or Vector), and then access each of them thru a cycle.
For example:
var allPrices:Array = [15.50, 20.24, 36.12];
var allElements:Array = new Array();
for (price in prices) {
// PriceText class is, for example, a movieclip with a textbox inside
var obj:PriceText = new PriceText(price);
addChild(obj);
allElements.push(obj);
}
Now you have all added elements in the allElements array.
Or You can create reference object , like :
class Bind {
public var target:Object;
public var key:String;
public function Bind(t:Object , k:String){
target = t;
key = k;
}
public function get value():* {
return target[key];
}
}
var myItem:SomeClass;
myItem.param = 100;
var bind:Bind = new Bind(myItem,"param");
trace("get myItem value:", bind.value);

Tracing MovieClips Name

I am creating some MovieClips like so:
var AClip:A = new A();
var A2Clip:A2 = new A2();
var A3Clip:A3 = new A3();
I then put the above into an array and am trying to trace out thier "name".
for(var i:int=0;i<theArray.length;i++){
trace(theArray[i].name);
}
This traces
instance99
instance77
instanceN...
What I want to accomplish is the trace is tracing out what I "initialized" them to
AClip
A2Clip
A3Clip
Is there a way to do that?
Thanks
Variable's name is not the same as MovieClip's name. You have to set its name manualy:
var AClip:A = new A();
var A2Clip:A2 = new A2();
var A3Clip:A3 = new A3();
AClip.name = "AClip";
A2Clip.name = "A2Clip";
A3Clip.name = "A3Clip";
Then you can get their name by calling trace(theArray[i].name);

ActionScript 3 - retrieving text values from TexInput created in component

I have a tile layout containing a list of TextInputs and text fields , i have created these fields in a custom component using the following code;
var newTextInputs:Array = [];
var newTextLabels = [];
var space:Number = 30;
var count:Number = 0;
for (var i:String in columnsData)
{
//create text labels
var label:Text = new Text();
label.name = "label" + count;
label.text = i;
newTextLabels[count] = label;
addChild(newTextLabels[count]);
// create text fields
var field:TextInput = new TextInput();
field.name = "field" + count;
field.width = 100;
field.height = 25;
field.text = columnsData[i];
newTextInputs[count] = field;
addChild(newTextInputs[count]);
count++;
}
users are allowed edit the values in each TextInput field, now i need to retrieve the newly udpated values however how can i access these fields? Because the identifiers are created dynamically i cant simply go componentName.InputFieldName, any ideas?
I think what you're looking for is getChildByName
later edit: tested with Flash and TextField and it works:
trace(TextField(getChildByName('textfield')).text);
You can add an event handler for the TileList CHANGE event; when it fires, I think the event.target property will have the specific TextInput field. Alternatively you can look at the TileList.SelectedItem property.
You may also be able to have a DataProvider bound to the TileList instead of your code as shown, which will handle this automatically for you. Try just assigning your NewTextLabels array as the dataProvider.