View Full Version : Making/Refering to Instances
Dan0
July 25th, 2007, 01:08 PM
Hey,
Sorry to ask such a noob question but...
I know how to make instances of objects (obviously) but if I am dynamically making them through a for() loop how can I keep references to them, simply and efficently. Making tones of variables isnt going to help. I don't know what I should search for as the questions is a bit general.
In AS2 I would:
- make a holder
- attach some movies in it through a for() loop
- refer to them by set name like (mc1, mc2... etc)
In AS3 I am:
- making my for() loop and instances with it [ var a = new Thing ] [ a.someFunction() ] [ a is erased from memory at end of loop ]
- wondering how I should refer to them...
Thanks for the help,
Dan :h::blush:
Dan0
July 25th, 2007, 01:20 PM
I just found that using an array to store the variable works. Kinda messes up my thinking tho. In some ways OOP is awsem cos you don't have to store a referer, in other ways you have to make an array to do so if you want it. Which is cool, but screws my brain up cos Im not used to this.
Please give me some other suggestions or tell me if I'm doing it wrong, thanks :D
public class Main extends Sprite {
var ins = new Array();
function Main() {
for (var i=0; i < 10; i++) {
var a=new ProjectThumb;
a.randomPos();
addChild(a);
ins.push(a);
}
addEventListener(Event.ENTER_FRAME,mouseCheck);
}
function mouseCheck(e:Event) {
for (var k=0; k < itemNum; k++) {
trace(ins[k].x);
}
}
}
BTW, if I have 2 loops say in the Main() function which both use the variable "i" as the counter, why is a conflict created even though I am using the var keyword?
Dan0
July 25th, 2007, 06:23 PM
Anyone know what I'm on about? :h:
CarlLooper
July 25th, 2007, 06:47 PM
The scoping in AS 3 is either incorrectly implemented or uses different rules from the "norm". So separate declaration of i from it's use:
var i:uint; // declare just once per function
for(i=0;i<max;i++) { ... } // use freely anywhere in the function
"Normally" (in other languages) the scope of i would be limited to the for block, but in AS3 the for block isn't constraining the scope of i - it's scope finds it's boundary at the function block level.
Carl
CarlLooper
July 25th, 2007, 07:00 PM
In particular:
var i:uint = 42;
trace(i); // 42
correct(); // 0 1 2 3 0 1 2 3
trace(i); // 42
function wrong2()
{
//these i conflict with each other
var i:uint = 5;
for(var i:uint = 0; i<4; i++) trace(i);
}
function wrong1()
{
// these i conflict with each other
for(var i:uint = 0; i<4; i++) trace(i);
for(var i:uint = 0; i<4; i++) trace(i);
}
function correct()
{
var i:uint;
for(i = 0; i<4; i++) trace(i);
for(i = 0; i<4; i++) trace(i);
}
Dan0
July 26th, 2007, 05:29 PM
Thanks for the brilliant loop help guys :hugegrin::eye:
How about my instance storing. Is it Ok? How do you guys to it ? :rabbit:
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.