Creating Multiple Values In For Loop [AS3] - actionscript-3

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));
}

Related

How to Save positions for 3 objects in Array to make random position between each other by AS3?

How to Save positions for 3 objects in Array to make random position between each other by AS3?
import flash.geom.Point;
var arry:Point = new Point();
arry[0] = arry[78,200];
arry[1] = arry[217,200];
arry[2] = arry[356,200];
//object called b1
b1.x = arry[0][0];
b1.y = arry[0][1];
//object called b2
b2.x = arry[1][0];
b2.y = arry[1][1];
//object called b3
b3.x = arry[2][0];
b3.y = arry[2][1];
//make objects swap positions between each other
var rand:Number = (Math.random()*arry.length);
//output to see random position [[78,200],[217,200],[356,200]]
trace(arry);
to get random with tween like this... https://www.youtube.com/watch?v=8m_m64plQ6E
At compile time you should get this Error I suppose : "ReferenceError: Error #1069"
Here is a way to store the positions (like in the link you provided from youtube) :
import flash.geom.Point;
var squareWidth:uint = 40;
var squareHeight:uint = 40;
var marginX:uint = 100;
var marginY:uint = 75;
var spacer:uint = 10;
var positions:Vector.<Point > = new Vector.<Point > (9);
function setPositions(v:Vector.<Point>):void {
var count:uint = 0;
var posx:uint;
var posy:uint;
for (var i = 0; i < 3; i ++)
{
for (var j = 0; j < 3; j ++)
{
posx = (j * squareWidth) + (spacer * j) + marginX;
posy = (i * squareHeight) + (spacer * i) + marginY;
v[count] = new Point(posx,posy);
count++;
}
}
}
setPositions(positions);
trace(positions);
// output :
// (x=100, y=75),(x=150, y=75),(x=200, y=75),(x=100, y=125),(x=150, y=125),(x=200, y=125),(x=100, y=175),(x=150, y=175),(x=200, y=175)
So here you have nine Points to place the clips like in the video.
You just have to add a function to swap the nine boxes stored in another Vector.
In your case.
For 3 positions do the following if I understand your question.
import flash.geom.Point;
var positions:Vector.<Point> = new Vector.<Point>(3);
var p1:Point = new Point(78,200);
var p2:Point = new Point(217,200);
var p3:Point = new Point(356,200);
positions[0] = p1;
positions[1] = p2;
positions[2] = p3;
trace(positions);
// output : (x=78, y=200),(x=217, y=200),(x=356, y=200)
So, you're still unclear!
Your issue is to find a random position?
This may help you if this is the problem you're facing :
import flash.geom.Point;
var positions:Vector.<Point > = new Vector.<Point > (3);
var numbers:Vector.<uint> = new Vector.<uint>();
var numbersAllowed:uint = 3;
var rndNmbrs:Vector.<uint> = new Vector.<uint>(3);;
var p1:Point = new Point(78,200);
var p2:Point = new Point(217,200);
var p3:Point = new Point(356,200);
positions[0] = p1;
positions[1] = p2;
positions[2] = p3;
trace(positions);
function populateRndNmbrs(n:uint):void {
for (var i:uint = 0; i < n; i++)
{
numbers[i] = i;
}
}
populateRndNmbrs(numbersAllowed);
function populateRandomNumbers(n:uint):void
{
var rnd:uint;
for (var i:uint = 0; i < n; i++)
{
rnd = numbers[Math.floor(Math.random() * numbers.length)];
for (var j:uint = 0; j < numbers.length; j++)
{
if (rnd == numbers[j])
{
numbers.splice(j,1);
}
}
rndNmbrs[i] = rnd;
}
}
populateRandomNumbers(numbersAllowed);
trace("rndNmbrs = " + rndNmbrs);
for (var i:uint = 0; i < numbersAllowed; i++)
{
trace("b"+ (i+1) + ".x = " + positions[rndNmbrs[i]].x);
trace("b"+ (i+1) + ".y = " + positions[rndNmbrs[i]].y);
// In place of trace, you should place the boxes at those random positions.;
}
//output:
//(x=78, y=200),(x=217, y=200),(x=356, y=200)
//rndNmbrs = 2,0,1
//b1.x = 356
//b1.y = 200
//b2.x = 78
//b2.y = 200
//b3.x = 217
//b3.y = 200
Is that what you want? Or do you want to know how to create a motion effect?
I'm not sure about what you really need...
This will help you to place all the boxes in a random position.
You may do this like here bellow, and add a function to check if the random positions are not the same.
With only 3 MovieClips, you will often have the same random positions as long they're stored in the "positions Vector"
var squares:Vector.<MovieClip> = new Vector.<MovieClip>(3);
function populateMCs(target:DisplayObjectContainer,n:uint):void{
for (var i:uint = 0; i < n; i++){
squares[i] = target["b"+(i+1)];
}
}
function swapMCs():void{
for (var i:uint=0; i<squares.length; i++){
squares[i].x = positions[rndNmbrs[i]].x;
squares[i].y = positions[rndNmbrs[i]].y;
}
}
populateMCs(this,numbersAllowed);
swapMCs();
I give you a last example to get a motion effect in AS3.
I'm not a translator AS2 -> AS3 and a video is not the best way to show your code :(
This will make your boxes move smoothly, but not the way you want.
Now, you have to learn AS3 and try to make the job by yourself.
Then, if you have another issue, just ask clearly what you want.
var friction:Number = 0.15;
setDestination(squares[0],squares[0].x,350,friction);
setDestination(squares[1],squares[1].x,350,friction);
setDestination(squares[2],squares[2].x,350,friction);
squares[0].addEventListener(Event.ENTER_FRAME,moveClip);
squares[1].addEventListener(Event.ENTER_FRAME,moveClip);
squares[2].addEventListener(Event.ENTER_FRAME,moveClip);
function setDestination(mc:MovieClip,x:uint,y:uint,friction:Number):void{
mc.destinx = x;
mc.destiny = y;
mc.f = friction;
}
function moveClip(e:Event):void{
var mc:MovieClip = e.target as MovieClip;
trace(mc.name)
mc.speedx = (mc.destinx - mc.x);
mc.speedy = (mc.destiny - mc.y);
mc.x += mc.speedx*mc.f;
mc.y += mc.speedy*mc.f;
if((Math.floor(mc.speedx)<1) && (Math.floor(mc.speedy)<1)){
mc.removeEventListener(Event.ENTER_FRAME,moveClip);
trace("STOP MOVE FOR " + mc.name);
}
}

How To Create An Array of Movie Clip with number

I have multi movieclips in my stage and they have instanceName e.g. k1,k2,... . I want to createar=[k1,k2,k3,k4, ...].
var i: int;
var ar: Array = new Array();
for (i = 1; i < 5; i++)
{
ar[i-1] = ["k" + i];
}
trace(ar);
ar[1].x = 100;
But the end of code does not perform.
What you want to do is to create an array of MovieClips, but instead of it you create an array of Arrays of Strings. To achieve your goal, you need to find an instance of a clip on stage by its name. Here is how you can try to do that:
const array:Array = new Array();
for(var i:int = 0; i < 5; i++) {
const childName:String = "k" + (i + 1);
const myMovieClip:MovieClip = stage.getChildByName(childName) as MovieClip;
array.push(myMovieClip);
}

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.

Creating variables in a systematical way using loops in AS3?

Is there? Like Let's say I need 5 variables in ActionScript 3.0 and would like to name them as follows:
var myVar_1 = "things";
var myVar_2 = "things";
var myVar_3 = "things";
var myVar_4 = "things";
var myVar_5 = "things";
But instead of having to type them 1 by 1, would it work in a loop? I can't seem to make it work and would really love some help/advice on this matter.
Yes, you can create a dynamic property name using [ ] array access:
var variables:Object = {};
for(var i:int = 0; i < 5; i++){
variables["myVar" + i] = "value " + i;
}
trace(variables.myVar3); // "value 3"
The variables object in this case could be replaced by any dynamic object, including MovieClips.
However, in most cases to store data by index it usually makes more sense to use an array. Example:
var variables:Array = [];
for(var i:int = 0; i < 5; i++){
variables.push("value " + i);
}
trace(variables[3]); // "value 3"
You should use Vector.<String>, Array, Object or Dictionary for that:
var variables:Vector.<String> = new <String>[];
for(var i:int = 0; i<5; i++)
{
variables[i] = "things";
}

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.