-
instantiation can be multiple movieclips with a loop?
I would like to know how I can instantiate multiple objects with a for
for example my code I created six squares of different colors and have instantiated with the name "cubeA, B, C, D, E, F"
apart in their xy position attribute I have given a value to each aparesca to me on the stage but in those cordenadas And always add +25 to make aparescan top of each other but can see each one.
Code 1:
Code:
var Cube1:CubeA = new CubeA();
Cube1.x = 40
Cube1.y = 140
addChild(Cube1)
var Cube2:CubeB = new CubeB();
Cube2.x = 65
Cube2.y = 140
addChild(Cube2)
var Cube3:CubeC = new CubeC();
Cube3.x = 90
Cube3.y = 140
addChild(Cube3)
var Cube4:CubeD = new CubeD();
Cube4.x = 115
Cube4.y = 140
addChild(Cube4)
var Cube5:CubeE = new CubeE();
Cube5.x = 140
Cube5.y = 140
addChild(Cube5)
var Cube6:CubeF = new CubeF();
Cube6.x = 165
Cube6.y = 140
addChild(Cube6)
I want to achieve is to instantiate all objects with a name but for each class must be contained in an array
something like this:
Code:
var McCubeClassName:Array;
McCubeClassName = [CubeA,CubeB,CubeC,CubeD,CubeE,CubeF]; // Mcs of Cube
for (var i = 0; i<=McCubeClassName.length; i++)
{
cube[i].x = 40
cube[i].y = (i * 16) + 10;
addChild(Cube[i])
}
what I want is to do the quotas instantiated one by one but this time using a for to make it faster and write fewer lines of code, because I have to do something simimar for a card game but not 54 letters are 100, they know that it is 100 times declare code 1, I'm going crazy!
-
.
Hi,
I think you might mean something like this?
PHP Code:
var mcCubeClassName:Array; mcCubeClassName = [CubeA,CubeB,CubeC,CubeD,CubeE,CubeF];
for (var i:int = 0; i < mcCubeClassName.length; i++) { var newClass:Class = mcCubeClassName[i] as Class; var cube = new newClass(); cube.x = 40; cube.y = (i * 16) + 10; cube.name = "cube" + (i + 1).toString();
addChild(cube); trace(cube.name); }
Last edited by fruitbeard; 05-30-2014 at 02:07 AM.
Reason: Typo
-
Originally Posted by fruitbeard
Hi,
I think you might mean something like this?
PHP Code:
var mcCubeClassName:Array;
mcCubeClassName = [CubeA,CubeB,CubeC,CubeD,CubeE,CubeF];
for (var i:int = 0; i < mcCubeClassName.length; i++)
{
var newClass:Class = mcCubeClassName[i] as Class;
var cube = new newClass();
cube.x = 40;
cube.y = (i * 16) + 10;
cube.name = "cube" + (i + 1).toString();
addChild(cube);
trace(cube.name);
}
thanks for replying!
and if the array has 2 dimensions?
Code:
var mcCubeClassName:Array;
mcCubeClassName = [[CubeA,CubeB,CubeC],
[CubeD,CubeE,CubeF],
[CubeG,CubeH,CubeI]];
for (var i:int = 0; i < mcCubeClassName.length; i++)
{
for (var j:int = 0; j < mcCubeClassName.length; j++)
{
var newClass:Class = mcCubeClassName[i][j] as Class;
var cube = new newClass();
cube.y = 40;
cube.x = (i * 16) + 10
addChild(cube);
}
}
-
.
Hi,
PHP Code:
var mcCubeClassName:Array;
mcCubeClassName = [
[CubeA,CubeB,CubeC],
[CubeD,CubeE,CubeF],
[CubeG,CubeH,CubeI]
];
for (var i:int = 0; i < mcCubeClassName.length; i++)
{
for (var j:int = 0; j < mcCubeClassName[i].length; j++)
{
var newClass:Class = mcCubeClassName[i][j] as Class;
var cube = new newClass();
cube.x = 40 + (Math.floor(i * 16) + 10);
cube.y = 40 + (Math.floor(j % mcCubeClassName.length) * 16);
addChild(cube);
}
}
-
.
Hi,
Double post I think.
PHP Code:
var mcCubeClassName:Array;
mcCubeClassName = [
[CubeA,CubeB,CubeC],
[CubeD,CubeE,CubeF],
[CubeG,CubeH,CubeI]
];
for (var i:int = 0; i < mcCubeClassName.length; i++)
{
for (var j:int = 0; j < mcCubeClassName[i].length; j++)
{
var newClass:Class = mcCubeClassName[i][j] as Class;
var cube = new newClass();
cube.x = 40 + Math.floor(i * 16);
cube.y = 40 + Math.floor(j % mcCubeClassName.length) * 16;
trace(cube + " x:" + cube.x + " y:" + cube.y);
addChild(cube);
}
}
-
Originally Posted by fruitbeard
Hi,
Double post I think.
PHP Code:
var mcCubeClassName:Array;
mcCubeClassName = [
[CubeA,CubeB,CubeC],
[CubeD,CubeE,CubeF],
[CubeG,CubeH,CubeI]
];
for (var i:int = 0; i < mcCubeClassName.length; i++)
{
for (var j:int = 0; j < mcCubeClassName[i].length; j++)
{
var newClass:Class = mcCubeClassName[i][j] as Class;
var cube = new newClass();
cube.x = 40 + Math.floor(i * 16);
cube.y = 40 + Math.floor(j % mcCubeClassName.length) * 16;
trace(cube + " x:" + cube.x + " y:" + cube.y);
addChild(cube);
}
}
thanks for reply!
thanks for replying!
I have a problem, in each row the x i takes a value
the result is this:
[object CubeA] x:50 y:40
[object CubeB] x:50 y:56
[object CubeC] x:50 y:72
[object CubeD] x:66 y:40
[object CubeE] x:66 y:56
[object CubeF] x:66 y:72
[object CubeG] x:82 y:40
[object CubeH] x:82 y:56
[object CubeI] x:82 y:72
should go like this:
[object CubeA] x:50 y:50
[object CubeB] x:50 y:70
[object CubeC] x:50 y:90
[object CubeD] x:50 y:110
[object CubeE] x:50 y:120
[object CubeF] x:50 y:140
[object CubeG] x:50 y:160
[object CubeH] x:50 y:180
[object CubeI] x:50 y:200
Code:
var mcCubeClassName:Array;
mcCubeClassName =
[
[CubeA,CubeB,CubeC],
[CubeD,CubeE,CubeF],
[CubeG,CubeH,CubeI]
];
for (var i:int = 0; i < mcCubeClassName.length; i++)
{
for (var j:int = 0; j < mcCubeClassName[i].length; j++)
{
var newClass:Class = mcCubeClassName[i][j] as Class;
var cube = new newClass();
cube.x = 40 + (Math.floor(i * 16) + 10);
cube.y = 40 + (Math.floor(j % mcCubeClassName.length) * 16);
trace(cube + " x:" + cube.x + " y:" + cube.y);
addChild(cube);
}
}
-
.
Hi,
Sure, it's quite alright for helping you Jennifer.
Is it necessary to quote each message?
You have the code there, mess around with the numbers and see.
but the first part is like so
cube.x = 50;
Last edited by fruitbeard; 06-03-2014 at 01:41 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|