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.
Related
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.
As I said: Is there any difference (in performance) between these two for loop?
Between this:
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
var currentObject:DisplayObject = displayObjects_[i];
currentObject.width = newWidth;
}
and this:
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
displayObjects_[i].width = newWidth;
}
I tested them before. The result said the first one was faster but I don't know if I did it right.
I know it's not really an answer to your question, but if you're looking for fastest way to iterate through this array you should do:
for each(var currentObject:DisplayObject in displayObjects_) {
currentObject.width = newWidth;
}
I tried this out on a bare-bones project using SDK 4.6. This is the code.
public class Main extends Sprite
{
public function Main()
{
var displayObjects_:Array = [];
for (var i:int = 0; i < 1000000; i++)
{
displayObjects_.push(new Sprite());
}
var start:int = getTimer();
for (i = 0; i < displayObjects_.length; i++)
{
var currentObject:Sprite = displayObjects_[i];
currentObject.width = 100;
}
var end:int = getTimer()
trace(end, start, end - start);
start = getTimer();
for (i = 0; i < displayObjects_.length; i++)
{
displayObjects_[i].width = 100;
}
end = getTimer()
trace(end, start, end - start);
}
}
These are the results.
Done(0)
[Starting debug session with FDB]
16703 16250 453
17141 16703 438
Like I said, it would be very surprising to see any difference between the two. You'll probably see more improvements through using Vector instead of Array. Otherwise, this stuff is too mundane to fuss over.
As I know, Actionscript compiler automatically moves variable definitions to the begin of the function. So, this loop doesn't declare variable each time, first sample the same that:
var currentObject:DisplayObject;
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
currentObject = displayObjects_[i];
currentObject.width = newWidth;
}
So, I think the difference only in one additional variable declare and it willn't affects performance. But 3vilguy's example is better.
Here, what I am doing is printing my javascript code by wrapping between "pre" tags like this :
<pre>
var sum = function(toSum) {
var j = 0;
for(var i=0; i<toSum.length; i++) {
j = j + toSum[i];
}
console.log("The sum of array is " + j);
};
sum([1,2,3,4]);
var multiply = function(toMultiply) {
var j = 1;
for(var i=0; i<toMultiply.length; i++) {
j = j * toMultiply[i];
}
console.log("The multiplication of array is " + j);
};
multiply([1,2,3,4]);
</pre>
But what I am actual getting is this :
var sum = function(toSum) {
var j = 0;
for(var i=0; i
Why is that so? How can I get "pre" tag working?
You should use < instead of <.
Similarly, use > for > and & for & when displaying in HTML.
Your for loop will be like:
for(var i=0; i<toSum.length; i++) {
...
}
I want to check for a straight combination in a poker game.
So, I have this array: var tempArr:Array = new Array;
I have this for sorting the array:
for (i = 0; i < 7; i++)
{
tempArr[i] = pValue[i];
}
tempArr.sort( Array.NUMERIC );
pValue is the value of the cards, it's have range from 2 to 14.
So, if I have this Array: tempArray = [2,3,3,4,5,5,6];
How can I check if I have a straight combination in my hand?
Set a bucket array to save if you got a card in hand
var t:Array = [];
//t[2] = 1;mean you have 2
// t[3] = 0;mean you don't have 3
//first initialize t
for(var i:int = 0; i < 15; i++)
{
t[i] = 0;
}
//then set the values from tempArray
for (var j:int = 0; j < tempArray.length; j++)
{
t[tempArray[j]] = 1;
}
//if you have a straight combination in your hand
//you will get a k that t[k] & t[k-1]& t[k-2] & t[k-3] & t[k-4] == 1
var existed:boolean = false;//the flag save if you got a straight combination
for (var k:int = t.length - 1; k >= 4; k--)
{
if (t[k] == 0)
{
continue;
}
if ((t[k] & t[k-1] & t[k-2] & t[k-3] & t[k-4]) == 1)
{
existed = true;
break;
}
}
I am trying to create multiple Numbers with for loop.
var sasutu1x : Number = Number(sasutu1.text);
var sasutu2x : Number = Number(sasutu2.text);
var sasutu3x : Number = Number(sasutu3.text);
var sasutu4x : Number = Number(sasutu4.text);
var sasutu5x : Number = Number(sasutu5.text);
var sasutu6x : Number = Number(sasutu6.text);
My Solution :
var i:int;
for (i = 0; i < 7; i++)
{
var this["sasutu" i + "x"] : Number = Number(["sasutu" + i].text);
}
Thanks for you help.
You can use a Vector, or if you want to get a reference to that number by name later, you can also use a Dictionary.
var i:int;
var sasutuDict:Dictionary = new Dictionary(true);
for (i = 0; i < 7; i++)
{
sasutuDict["sasutu" i + "x"] = Number(["sasutu" + i].text);
}
One advantage of a dictionary, is that you can even do something like this:
for (i = 0; i < 7; i++)
{
var sasutuTexField:TextField = this["sasutu" + i];
sasutuDict[sasutuTexField] = Number(sasutuTexField.text);
}
Meaning you can have the key of the dictionary be the text field itself.
A better solution would be to use a Vector instead.
var vec:Vector.<int> = new Vector.<int>();
for (var i:int = 0; i < 7; i++)
{
vec.push(Number(["sasutu"+i].text));
}