AS3: declaring variables in a for loop
- Started
- Last post
- 23 Responses
- CyBrain
Doesn't seem possible. I don't even know if I could do this in AS2.
for (var i:uint = 1; i<= 7; i++) {
private var ("MainBtn" + i + "_mc"):MainBtns;
}Of course I get an error "1084: Syntax error: expecting identifier before leftparen." I don't really know what that means but is there a way to dynamically declare variables? It would save a lot of typing and would be a good practice.
Thanks in advance.
- skt0
i'm not meaning to be a cunt... and i have just as much trouble with as3 as you appear to be having, but would it not be faster and less painful if, at this point, you just bought a book on how to code?
- or visit an actionscript forum. you might learn somethingmaximillion_
- No offense taken. Been going through Lynda.com's AS3 tutorials. I'll look into something more in depth.CyBrain
- acrossthesea0
I think you shouldn't worry about unique instance names for each button you're making. Instead use a property (not sure if property is the right term) of "index" and make that equal to i. Then push each button you made to an array. Now each button you created has an unique identifying index that you can use down the line.
Just like I did in this example: http://pastebin.com/f39422ea8
I gave each button an index in the loop and then you can see how I used that index (e.target.index) for that click function.
- skt0
this might help:
- Pupsipu0
haha yea you can't create variable names on the fly. You have to name it mainBtn in this case and stick it into an array with an index, or a dictionary with a key.
and you can't put private/public in front of a var you're declaring inside a function. That stuff is only for vars outside of functions, and loops are functions and are usually inside named functions.
also in your example, you would say var mainBtn:MainBtn = new MainBtn();
you really need to look up some tutorials, you're fucking lost.
- fiesta0
CyBrains PVN History
"AS3: declaring variables in a for loop
AS3 1084 Syntax Error
Actionscript Warning 5004
AS3 dynamic btn scripts
Actionscript Classpath Problem
AS3 Tween classes
AS3 problem
Actionscript 3: startDrag
Actionscript 3 delete enterframe?
Action Script 3 functions
Actionscript 3: Drag constraints "I think its time for a career change CyBrain
- Autokern0
To answer your question:
You can do your way only if you have the object already existing on the stageFor your purpose would better use the name property:
for (var i:uint = 1; i<= 7; i++) {
var btn:Mainbtns
btn.name = ("MainBtn" + i + "_mc")
}
and for retrieving certain instances by name you would use the
getChildbyName() function.See the DisplayObjectContainer help reference for more details.
- for reatrieving, the array will do as well.Autokern
- That's good to know. Thanks.acrossthesea
- Autokern0
ops, correct code:
for (var i:uint = 1; i<= 7; i++) {
var btn:Mainbtns = new MainBtn
btn.name = ("MainBtn" + i + "_mc")
}
- CyBrain0
Ok. You've all convinced me. I'm going back to AS2 for the project I'm working on. The client I'm doing this for will never know the difference. I'm just not up to this until I sit back with a book and get a grip on this unfathomable black magic you people this so highly of. I concede.
- tommyo0
Don't get frustrated man, it takes a little time. The biggest problem is changing your thinking from AS2 to AS3. Labeling dynamic objects so that you can find them after you create them is now mainly handled by assigning myObject.name. Before, in AS2, once you declared a variable it was accessible via it's variable name, but because of the access limiting that no longer applies unless the variable is defined inside of your Class declaration. So you have different options now for grouping and accessing dynamic objects: 1. Push the objects to an array and access them via their array position (the ol' myArray[0]), 2. Add the objects to a display object using addChild and then you can look them up with myMainObject.getChildAt or myMainObject.getChildByName, OR, and this one I really like, create a hashmap or I think it's officially called Dictionary (a lot like an array) where you assign objects along with a string to a Dictionary and then you can look them up by their string.
More on Dictionaries:
http://www.gskinner.com/blog/arc…Keep your chin up.
- tommyo0
Oh and ... quit asking questions here. There are more print heads and jokers here than anything else, and the gods of QBNland decided to make their forum so that pasted code gets completely fucked. The flash community is alive and well outside these walls. By posting here you're basically walking into your eye doctor and asking him to examine a lump on your balls. :)
- CyBrain0
I get about half of what you're saying, but I really need to take much more time than I have to do this project. I need to sit down with a book and read it for a while. I don't even care if I have to turn down work for a while. I went through most of Lynda's tutorials and they were great, but that's just getting your feet wet. I'm done underestimating AS3. It's not like using Photoshop CS4 after CS3. It's more like when Director fell out of favor and so many people had to learn actionscript instead. Thanks for all the help. I'll be back soon enough. I was also getting a lot of help on actionscript.org.
I think it's funny how some people here object to asking so many questions — as if I should contribute to more threads where you can see photos of Maggie Gyllenhall breast-feeding.
- Get Moock's book on AS3 http://www.amazon.co…Autokern
- Just ordered it.CyBrain
- That one might be a tad over your head, I know it confused the shit out of me. I'd also get the AS3 Cookbook. That booktommyo
- helped open my eyes a LOT. After I read through that one, then Moocks book is easier to digest.tommyo
- I have The Cookbook. That one seems more like reference than background understanding.CyBrain
- Yeah it definitely is purely reference. But it shows you a lot of usage which somehow helped me with Moocks book.tommyo
- instrmntl0
for( var i:uint = 1; i < = 7; i++ )
{
private var breastfeedingMaggieGyllenhall:Ma... = this["MainBtn" + i + "_mc"];
breastfeedingMaggieGyllenhall.wh...
}
- rounce0
@CyBrain: Just read the adobe livedocs AS3 reference getting started guide. It will tell you everything you need to know (possibly more).
- mikotondria30
Honestly, CyBrain, I feel your pain - I can only recommend like a 2 pronged approach to the problem, which you seem to have worked out already..
a) A chunky refernce like O'Reilly's Essential AS3, which you can just gulp a few dozen pages of at a time. Moock is careful and methodical about how he introduces both OO principals and the language to describe them, and I'd suggest reading, then rereading, then rerereading little chunks at a time, maybe on the toilet, just don't rush it. You can't hammer through it like a novel - When you get to listeners and things like bubbling phases and so on, just go real slow, get anal about it, force the details down, then go back and just keep going over it til it slips down like oiled wine.b) Faithfully follow along tutes like gotoandlearn's more basic as3 stuff - Brimelow is really a hero of mine, perfectly paced an weighted tutes that you really can follow in real-time and get very pleasing results..
that's it really - Hammer the theory down your think pipe until you can actually 'think' in as3, the way you really could with as2, and also bust out and have some 'fun' taking baby steps - soon enough the tutes will seem like child's play, even ones you haven't done before, and the book will be complete, ready for frequent references to, and rereadings of.
This is as much advice to myself, as anyone else..
- CyBrain0
I agree with you on Lee Brimelow. I've done a lot of his tutorials. I always get them, but always wish there were more. I end up being confused wondering how I would expand upon what he's doing. I expect this to take months, not weeks.
Meanwhile, I'm back to AS2, getting work done and it feels like getting my puppy back from the hospital. I can't believe how easy each task is. I'm beyond amazed how people look down on AS2 now.
- yeah, apart from _root - that was always ugly. And xml... Shit runs slow for fun as2 in the player 8 too..mikotondria3
- ifeltdave0
the leads are weak.
- Pupsipu0
well if you were an experienced programmer, switching from one language to another wouldn't be such a big problem. Maybe AS2 is magical somehow, it lets people code stuff without getting the main concepts.
When I switched from AS2, I didn't try any OOP stuff, any classes, private/public or any of that, since I didn't do anything of the sort in AS2. So the only difference in AS3 was adding things to arrays to reference them, and adding children took me a day to figure out.
Then when I started doing things the OOP way, it took me months to get used to it. I haven't coded for a year when I started that, and I still don't really feel comfortable there. Could be an age thing too.
But if you get AS3 in OOP style, you're set for decades. It's the way Java has been for decades already, it's what they'll turn JavaScript into. It's hard to imagine a drastically different syntax design.
- CyBrain0
I'm a designer/animator who got pretty good at AS2 after years. I'll do what it takes to learn, no matter how long it takes, but I think experienced programmers underestimate how drastically different this is.