AS3 Question
- Started
- Last post
- 17 Responses
- ukit
I am trying to add a variable to a bunch of movieclips dynamically, for instance,
for (var i=0; i<numItems; i++){
["item"+i]num=i;
}This would have worked in AS2, but in AS3 I get an error, "trying to access undefined property." Can someone help me out as to what I am doing wrong here? Thanks!
- amullins0
are you trying to concatenate ["item"+i]num as the variable name?
- amullins0
well, assuming that you are - you must define the variable using the var keyword or use this[varname]. also, in your example, you have a syntax error. there should be a dot after the ] to reference the num property of the object. you may be looking for something like:
for (var i:uint = 0; i<numItems; i++) {
this["item" + i].num = i;
}
- ukit0
Thanks for your help. Basically what I am trying to do is cycle through a list of movieclips, all of which start with "item." So item1, item2, etc. And then add a variable, "num" to each of them.
How would I define the variable in this case since it's unique to each movieclip? Or, is there a better way to achieve this? Sorry if these are stupid questions, I'm just trying to wrap my head around AS3 quickly and this part of the syntax seems like a strange difference...
- sixfngers0
you have to use a dynamic class. Even if its just an empty class that has the dynamic callout.
search dynamic classes in help
your class can be as simple as
dynamic class Foo
{
function Foo()
{
}
}then you can add vars at runtime
- amullins0
you shouldn't need to create a class to add a variable to an mc...
- ukit0
Hmmm...I am getting mixed messages:) amullins, what do you think needs to be added to the code i have to make it work?
- amullins0
are the mcs already on stage? are you adding them dynamically?
- ukit0
That's the thing, they are already onstage
- amullins0
ok, so you've given all of them instance names... you may want to consider defining an array of looping through that directly.
var arr:Array = [this.clip1, this.clip2, this.clip3, this.clip4];
arr.forEach ( function(mc:*,index:uint,_arr:Ar... {
mc.num = index;
});you could also wrap the mcs in another mc and loop through the wrapper like:
for (var i:uint = 0; i<wrapper.numChildren; i++) {
wrapper.getChildAt(i).num = i;
}
- ukit0
And this will allow me to target the movieclips, whereas concatenating the name as I did above won't? Man, AS3 is making my head hurt:(
- amullins0
you can still concatenate the name, if you wish (as stated above):
for (var i:uint = 0; i<numItems; i++) {
this["item" + i].num = i;
}
- ukit0
I'm still getting an error "Error #1010: A term is undefined and has no properties."
- amullins0
try turning off strict mode in the actionscript settings (publish settings > flash > (settings - beside actionscript version)). if this doesn't work - send me the file and I'll see what I can do.
- amullins0
i tried sending you an email - bounced back
- MrOneHundred0
You geeks can make sense of all that code, but you’re email bounces back? What a crazy world! ;-0
- I know, normally I come here for the boobies and Fouty jokes.ukit
- When I started reading this thread, I assumed I had a font problem in my browser.MrOneHundred
- qbn should support bbcodeamullins
- ukit0
amullins, had my old e-mail in there - it's
- sixfngers0
there is a file on my server that does what you want.
there are three MovieClips on the stage and the program loops through and adds a variable" foovar" to each one with a number value = to the loop iteration.when you click they trace out the var