How can I change first items of a DataProvider? - actionscript-3

I have a datagrid with 5 columns and I want to manipulate its dataprovider. I'm trying to change dp's first column dynamically. I tried this:
for (var i:uint = 0; i < dp.length; i++) {
var tempArr:Array = new Array(dp.getItemAt(i));
tempArr[0] = String(i);
dp.replaceItemAt(tempArr, i);
}
However this empties all cells in datagrid. How can I fix this?

Anyway, I fixed this.
for (var i:uint = 0; i < dp.length; i++) {
dataGrid.editField(i, "noCol", String(i + 1));
}

Related

adding X amount of addChild per maxvalue in loop

The assignment is the spawn several light and dark feathers according to score points from a quiz. The light feathers symbolize the correct points (light_feather), and the dark feather are the incorrect points (dark_feather) (Each are being tracked). All the feathers are supposed to line up on one line, meaning first light feathers, followed by the dark feathers. I got the quiz dynamics figured out, and the function I have posted here is only for when they press end quiz.
var light_feather:LightFeather = new LightFeather();
var dark_feather:DarkFeather = new DarkFeather();
var good_answers:uint = 0;
var bad_answers:uint = 0;
function avsluttFunc (evt:MouseEvent)
{
var sum_LightFeatherX:Number = 0;
for (var i = 0; i < good_answers; i++) {
addChild(light_feather);
light_feather.x += 12 + (i*16);
light_feather.y = 0;
trace("Lys X-verdi: " + light_feather.x);
sum_LightFeatherX += Number(light_feather.x);
return sum_LightFeatherX;
}
trace(sum_LightFeatherX);
dark_feather.x += sum_LightFeatherX;
for (var j = 1; j <= bad_answers; j++) {
addChild(dark_feather);
dark_feather.x += 12 + (j*16);
dark_feather.y = 0;
trace("Mørk X-verdi: " + dark_feather.x);
}
/*
//Resetter poengsummen
good_answers = 0;
bad_answers = 0;
*/
}
You can do what you are looking for using only one for loop, take a look :
var good_answers:uint = 2;
var bad_answers:uint = 4;
function avsluttFunc(evt:MouseEvent)
{
for (var i:int = 0; i < good_answers + bad_answers; i++) {
var feather:DisplayObject = i < good_answers ? new LightFeather() : new DarkFeather();
feather.x += 12 + i * (feather.width + 1);
feather.y = 0;
addChild(feather);
}
}
This code example will create 4 DarkFeather instances next to 2 LightFeather ones.
Edit :
How to add your objects to an array ?
// feathers array should be accessible for both codes (adding and removing objects)
var feathers:Array = [];
for (var i:int = 0; i < good_answers + bad_answers; i++) {
var feather:DisplayObject = i < good_answers ? new LightFeather() : new DarkFeather();
addChild(feather);
feathers.push(feather);
}
then to remove them from the stage, you can do for example :
for (var i:int = 0; i < feathers.length; i++) {
var feather:DisplayObject = DisplayObject(feathers[i]);
feather.parent.removeChild(feather);
}
Hope that can help.

What does 'for' do in Action Script 3?

Just as title implies, my question is, what does the 'for' piece of code do in ActionScript 3? I apologize if this seems to be a stupid question; I've very recently started coding.
It's a type of loop.
for (var i:int = 0; i < 10; i++)
{
// Do something.
}
This says:
1. Create an int called i and set it to 0.
2. Check to see if i < 10. If not, stop executing the for loop and move on.
3. Do something.
4. Add 1 to i.
5. Go back to #2.
for is used to create a loop. It can be a loop trough an array:
var array:Array = [1,2,3];
for(var i:int = 0; i < array.length; i++) {
// do something
}
Or it could be an object.
var object:Object = {};
for(var i:String in object) {
// do something
}
Or you could just have an loop like
for(var i:int = 0; i < 10; i++) {
// do something
}
A for loop through children on stage / Movieclip:
for(var i:int = 0; i < numChildren; i++){
// do something
}
So you can do many things with the for.

Sumarize code to loop

i have code
dict_a[box_1_a] = text_1_a;
dict_a[box_2_a] = text_2_a;
dict_a[box_3_a] = text_3_a;
dict_a[box_4_a] = text_4_a;
dict_a[box_5_a] = text_5_a;
dict_a[box_6_a] = text_6_a;
how to summarize the code looks like this
for (var i:int = 1; i <= 6; i++)
{
dict_a[box_(i)_a] = text_(i)_a;
}
Thanks before
If boxes and texts are class members, correct syntax should be
for (var i:int = 1; i <= 6; i++)
{
dict_a[this["box_" + i + "_a"]] = this["text_" + i + "_a"];
}
To do this, you should first save your box_1_a and text objects in an array, so you can iterate over them via:
for(int i=0; i<=5; i++) {
dict_a[box_a[i]] = text_a[i];
}
I don't know if the syntax is correct for AS/Flash, but that's how it will basically work.

Adding indexes in array

I am adding two matrices (or possibly many) in ActionScript 3.0.
Now my problem is how can I add indexes in array that is something like this?
array1[1,2,3,4] + array2[2,4,5,6] = answer[3,6,8,10]
This function adds up all the arrays that are passed to it:
function sumOfArrays(...args):Array
{
var sum:Array = [];
var arrays:Array = [];
var longestArrayLength:uint = 0;
for (var i:int = 0, n:int = args.length; i < n; i++)
{
if (args[i] is Array)
{
arrays.push(args[i]);
longestArrayLength = args[i].length > longestArrayLength ? args[i].length : longestArrayLength;
}
}
for (var j:int = 0; j < longestArrayLength; j++)
{
sum[j] = 0;
for (i = 0; i < n; i++)
{
sum[j] += isNaN(arrays[i][j]) ? 0 : arrays[i][j];
}
}
return sum;
}
It can be used like this:
var sum:Array = sumOfArrays(array1, array2);
That's not possible. Arrays only allows access via one index. You'd have to write a method on your own for this. But be aware of the fact, that null would be referenced on answer at, 0, 1, 2, 4, 5 and so on.

from left to right direction in Actionscript 3

i want to make my box animation come up from left to right direction, right now my box appear from top to buttom, how can i change my move direction of the box from left to right, i have nine box and three each other line, like picture below
here is my current code
function random_item()
{
var listItem:Array = new Array();
for (var i:uint=0; i<15; i++)
{
listItem.push(i);
}
ItemLeft = 0;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
var thisItem:FirstBox = new FirstBox();
thisItem.stop();
thisItem.x = x * IcardHorizontalSpacing + IboardOffsetX;
thisItem.y = y * IcardVerticalSpacing + IboardOffsetY;
var r:uint = Math.floor(Math.random() * listItem.length);
thisItem.cardface = listItem[r];
listItem.splice(r,1);
thisItem.addEventListener(MouseEvent.CLICK,clickItem);
thisItem.buttonMode = true;
addChild(thisItem);
ItemLeft++;
}
}
}
how do i make the animation from left to right, thanks before
Since your nested for loop has the y-position nested inside the x-position, three y positions (top, middle, bottom) will appear for each x-position.
You might be able to fix the behavior you are seeing by structuring your for loop like below, switching the order of the variables:
for (var y:uint=0; y<boardHeight; y++)
{
for (var x:uint=0; x<boardWidth; x++)
{
//Inner code remains the same
}
}
This way, three x-positions will be displayed for each y-position, hopefully creating the left-to-right behavior you're looking for.
//change
for (var y:uint=0; y<boardHeight; y++)
//to
for (var y:uint=boardHeight; y>0; y--)
Although, in your code you are nesting the y loop inside the x loop.
This means that for every iteration of the X loop the box will go from bottom to top
I don't know if this is the what you are looking for.
Please be more specific in your description of what you want.
You are looping first over x and then over y. This means that it will add 3 items for every column. Invert the loops to get it to add 3 items for every row.
function random_item()
{
var listItem:Array = new Array();
for (var i:uint=0; i<15; i++)
{
listItem.push(i);
}
ItemLeft = 0;
for (var y:uint=0; y<boardHeight; y++)
{
for (var x:uint=0; x<boardWidth; x++)
{
var thisItem:FirstBox = new FirstBox();
thisItem.stop();
thisItem.x = x * IcardHorizontalSpacing + IboardOffsetX;
thisItem.y = y * IcardVerticalSpacing + IboardOffsetY;
var r:uint = Math.floor(Math.random() * listItem.length);
thisItem.cardface = listItem[r];
listItem.splice(r,1);
thisItem.addEventListener(MouseEvent.CLICK,clickItem);
thisItem.buttonMode = true;
addChild(thisItem);
ItemLeft++;
}
}
}