-
Senior Member
as3 array loop
I'm having trouble building an array of 8 objects, loop them, and place them on a scene. I'm getting undefined cubes
any help would be greatly appreciated, thanks sincerely sybershot
[CODE] var arr:Array = new Array();
arr[0] = "var Cube0:Cube = new Cube(ML1, n1, n1, n1, n2, n2, n2)";
arr[1] = "var Cube1:Cube = new Cube(ML1, n1, n1, n1, n2, n2, n2)";
arr[2] = "var Cube2:Cube = new Cube(ML1, n1, n1, n1, n2, n2, n2)";
arr[3] = "var Cube3:Cube = new Cube(ML1, n1, n1, n1, n2, n2, n2)";
arr[4] = "var Cube4:Cube = new Cube(ML1, n1, n1, n1, n2, n2, n2)";
arr[5] = "var Cube5:Cube = new Cube(ML1, n1, n1, n1, n2, n2, n2)";
for(var i=0; i<6; i++) {
this['Cube'+i] = arr
this['Cube'+i].x = rad * Math.cos(angle);
this['Cube'+i].z = rad * Math.sin(angle);
angle += (360 / 6) * Math.PI / 180;
}
scene.addChild(Cube0);
scene.addChild(Cube1);
scene.addChild(Cube2);
scene.addChild(Cube3);
scene.addChild(Cube4);
scene.addChild(Cube5);
Cube0.addEventListener(MouseEvent.CLICK,onClick);
Cube1.addEventListener(MouseEvent.CLICK,onClick);
function onClick (event:MouseEvent):void{
}
CODE]
-
Code:
var s:String = "var pipe:Pipe = new Pipe()"; //ceci n'est pas une Pipe.
var pipe:Pipe = new Pipe(); //this IS a Pipe.
Now throw away your code and try again.
-
Sorry, that's too harsh. But I'm leaving it there because it's also perfect.
You are using Strings, expecting them to be magically interpreted. They won't be.
Code:
var angle:Number = 0;
var arr:Array = new Array();
for (var i:int = 0; i < 6; i++){
var c:Cube = new Cube(ML1, n1, n1, n1, n2, n2, n2);
arr.push(c);
c.x = rad * Math.cos(angle);
c.z = rad * Math.sin(angle);
angle += (360 / 6) * Math.PI / 180;
scene.addChild(c);
c.addEventListener(MouseEvent.CLICK,onClick);
}
-
Senior Member
thanks 5TonsOfFlax,
sorry I did not get back sooner, I had an emergancy to deal with.
then came back and had to shovel snow, no place to put it.
don't worry about being harsh, nothing is taken personally.
It acually made me smile.
I do have two question though, if you or someone can answer.
I want to be able target each cube for different functions + change ML1 on certain cubes to ML2, ML3,etc. How would I be able to do those using a single var c
-
If you want to handle them differently, then the single loop is probably not what you want. You shouldn't use c in the code above outside of the loop anyway (but you can use arr[3] to get the fourth Cube, for example). Do the common stuff in a loop, and the differences in other code.
-
Senior Member
thanks for helping 5TonsOfFlax
I build a single loop before but looping an array is driving me looppy, lol. sorry I'm trying to get this, but just can't seem to grasp the concept.
is this any closer
Code:
var Cube0:Cube = new Cube(ML1, n1, n1, n1, n2, n2, n2);
var Cube1:Cube = new Cube(ML2, n1, n1, n1, n2, n2, n2);
// plus 4 more cubes
var arr:Array = new Array();
for(var i=0; i<8; i++) {
arr.x = rad * Math.cos(angle);
arr.z = rad * Math.sin(angle);
angle += (360 / 8) * Math.PI / 180;
}
arr[0].addEventListener(MouseEvent.CLICK,onClick1)
arr[1].addEventListener(MouseEvent.CLICK,onClick2);
-
Still several things not quite right with this code. You don't create any Cubes inside the loop, you don't push anything into the array, you're trying to set x and y values on an array, and the comment says 4, but you're doing it 8 times. You do add eventListeners in what looks like a correct manner.
Code:
var arr:Array = new Array();
var Cube0:Cube = new Cube(ML1, n1, n1, n1, n2, n2, n2);
var Cube1:Cube = new Cube(ML2, n1, n1, n1, n2, n2, n2);
arr.push(Cube0); //arr[0] is now Cube0
arr.push(Cube1); //arr[1] is now Cube1
// plus 4 more cubes
var angle:Number = 0; //declare and initialize angle
for(var i=0; i<4; i++) { //changed 8 to 4 to match comment
var c:Cube = new Cube(ML1, n1, n1, n1, n2, n2, n2);
c.x = rad * Math.cos(angle);
c.z = rad * Math.sin(angle);
angle += (360 / 8) * Math.PI / 180; //you could calculate this constant outside the loop, but that's only optimization.
arr.push(c); //put c in the array of all Cubes
}
arr[0].addEventListener(MouseEvent.CLICK,onClick1);
arr[1].addEventListener(MouseEvent.CLICK,onClick2);
I assume that ML1, ML2, n1, n2, rad, onClick1, and onClick2 are all defined outside this snippet.
-
Senior Member
thanks a million 5TonsOfFlax
sorry for any confusion.
I understand much better now, I can't thank you enough. sincerely sybershot
ps. you assume correct all those varables are defined outside
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
|