TwelvestoneFlash

referencing dynamically created movieclips in AS3


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
Big Ern
 
2008-12-04

right so eval no longer works in AS3...

my code in my Document class:

private var block:Block; private var totalBlocks:Number = 12;

//in the constructor for(var i:int = 0; i < blocksWide; i++) { block = new Block(); addChild(block);

block.name = "block" + i;
    disableBlocks();

}

//in a later function in the same class private function disableBlocks():void { for(var i:int = 1; i < totalBlocks; i++) { //How do I reference all my blocks here?!?! //It certainly isn't like this eval("block" + i + ".name") } }

I'm assuming it must be something like childAt(i); or summat... but I can't find the bleeding syntax! I hate the new AS3 help files... what happened to all the examples?!

Cheers!

JLM
 
2008-12-04

for (var i:uint = 0; i < numChildren; i++) { child = getChildAt(i); }

Big Ern
 
2008-12-04

Thanks JLM... however, doing it that way would reference ALL the children, not just the specific ones I wanted to grab by name.

I tried this bad boy and it worked:

var block = getChildByName("block" + i);

However, now that I've done that, this doesn't work:

block.alpha = 50;

but this does:

block.alpha = 0;

what gives?!

EDIT:-- nm, alpha is read from 0 to 1 now, not 0 to 100... Is there anything they haven't changed?! :mad: (thanks for the help anyway as per usual, JLM)

DontBogartMe
 
2008-12-04

what I'd usually do here is store references to the blocks in an array:

private var totalBlocks:Number = 12; private var arrBlocks:Array = new Array();

//in the constructor

// NOTE: no need to keep block as part of the class, it should be a local variable to save memory space. var block:Block;

for(var i:int = 0; i < blocksWide; i++) { block = new Block(); addChild(block);

// Add this newly made block to the array
arrBlocks.push(block);

// No real need for this next line now all the blocks are in the array...
block.name = "block" + i;

// Not sure this is meant to be here is it?
    disableBlocks();

}

//in a later function in the same class private function disableBlocks():void { var block:Block; for(var i:int = 1; i < totalBlocks; i++) { // Pull another block out of the array... block = arrBlocks[i]; } }

That said, this idea of using getChildAt is probably more efficient in memory terms, cos you don't need to store the array that way.

//edit - by "getChildAt" I meant "getChildByName"

Why do you call the function disableBlocks() in that for loop though? You'll be trying to do things to blocks you haven't even created yet the way this code is constructed.

Big Ern
 
2008-12-04

Originally posted by: DontBogartMe

That said, this idea of using getChildAt is probably more efficient in memory terms, cos you don't need to store the array that way.

Why do you call the function disableBlocks() in that for loop though? You'll be trying to do things to blocks you haven't even created yet the way this code is constructed.

You're right, I don't call disableBlocks() from there in my actual code, I just put it there for simplicity sake when showing it here. In my code it is called in another function that is triggered by a custom event (that's right, check me out!) k

Thanks for the advice, dude. I've moved the variable inside the constructor.

I was thinking of posting the entire project up here once it's complete to try and start a "this is the way I would've done it" type discussion... if anyone is interested, that is. It's hard being the only AS3 developer in a workplace, most of the people I've worked with recently are working in a mashed up version of AS1 and AS2...

DontBogartMe
 
2008-12-04

is your project online, or will it be when done?

You haven't managed to convince the others to follow your lead yet? Or are they still waiting to see if you crash and burn before going near AS3?

I think my biggest problem with working in Flash is the communication between me and the designers.... I can never explain what's going on well enough and they just never seem to get what I'm trying to tell them - e.g. how they need to build an FLA so that it'll work with the code I've written. Right now I'm on a project that so far has no design done at all, we've just discussed the brief, so I'm coding it all up using graphics I've done myself - it naturally looks horrible, but in theory it should be fairly simple to swap in the proper graphics. Time will tell!

Big Ern
 
2008-12-04

Originally posted by: DontBogartMe is your project online, or will it be when done?

I'll put it up on my site once it's in a state to play with.

You haven't managed to convince the others to follow your lead yet? Or are they still waiting to see if you crash and burn before going near AS3?

In my last few gigs, I was a contractor so the clients always want things done yesterday so I didn't get the time to learn AS3 myself, let alone show someone else how to do it. Also, so many people are still paranoid about Flash Player 9 not being installed on user machines. In my last place (which was very good), a couple of their clients asked us to build to Flash 7(!) because the clients were too fucking lazy (or stupid) to see just which player their employees had on their machines. Great place to work and the developers were awesome, but they constantly had their hands tied by the client's ridiculous specs.

The design agencies I used to work for last summer REALLY didn't give a shit about "the best way to do things", they just wanted them out quick... (The weetos-dot-co-dot-uk site (the SITE, not the game) which cost a fucking mint by the way, was done completely in AS1) I will never EVER work for another design agency again. As long as it looks pretty, they don't give a rats shit-assed fanny about anything else, LEAST of all employee workrates. If I never see another AD in my life, it will be too soon, I fucking HATE marketing and sales people (except my wife and DB's wife and my wife's best friend, obviously k )

Luckily, at the uni, we code to what the student machines on campus can handle and they've just finished installing 9 on all of them FUCK YEAH. (I love unis) I am the only flash guy in this department so basically, 12stoners and online tutorials are the only sources of AS3 info at the minute...

Getting there though, had a couple major breakthroughs today.

EDIT:-- God, I didn't mean for that reply to turn into a rambling rant, sorry about that k

DontBogartMe
 
2008-12-04

I guess that's one of the diffs between freelance and permanent - I work for design agencies all the time, but as I'm on my own here I mostly get to choose how I develop stuff, so I made the move to AS3 when I felt up for it.

You're right though, they couldn't care less about the code itself as long as it all works, but then they wouldn't understand the code even if they cared enough to look at it. In fact, it's probably best that way!

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

referencing dynamically created movieclips in AS3