all groups > flash actionscript > march 2007 >
You're in the

flash actionscript

group:

Trying to save key strokes with a for loop


Trying to save key strokes with a for loop Beppo5
3/30/2007 9:57:54 PM
flash actionscript:
I have been trying to action script for years with no success. I to some time
off and learned javascript and some oop (ruby) so I thought I would take
another stab at it. I am still having problems. Here goes...

I have several movieclips on the stage that will be set up as buttons. They
all have the same effects associated with mouseover and mouseout so I thought I
could save some key strokes with a for loop. But I can't get it working right.
Could someone tell me what I am doing wrong please?


If anyone can help it would be appreciated.


for(var i=0; i<4; i++) {
trace(i)
var mc_ItemText[i]:MovieClip;
var inter[i]:Number;
var mc[i]:MovieClip = mc_ItemText[i]
//functions
grow = function(mc:MovieClip):Void {
if (mc._width<=55) {
mc._width += 1;
mc._height += 1;
} else {
clearInterval(int1);
}
}
shrink = function(mc:MovieClip):Void {
if (mc._width>=45) {
mc._width -= 1;
mc._height -= 1;
} else {
clearInterval(int1);
}
}

//events
mc[i].onRollOver = function():Void {
clearInterval(int1);
int1 = setInterval(grow,10,mc[i]);
}
mc[i].onRollOut = function():Void {
clearInterval(int1);
int1 = setInterval(shrink,10,mc[i]);
}
}
Re: Trying to save key strokes with a for loop kglad
3/30/2007 11:54:02 PM
Re: Trying to save key strokes with a for loop kglad
4/1/2007 12:00:00 AM
try:



for (var i = 0; i<4; i++) {
mc = this["mc_ItemText"+i];
mc.onRollOver = function():Void {
clearInterval(this.int1);
this.int1 = setInterval(grow, 10, this);
};
mc.onRollOut = function():Void {
clearInterval(this.int1);
this.int1 = setInterval(shrink, 10, this);
};
}
//functions
grow = function (mcLocal:MovieClip):Void {
if (mcLocal._width<=55) {
mcLocal._width += 1;
mcLocal._height += 1;
} else {
clearInterval(mcLocal.int1);
}
};
shrink = function (mcLocal:MovieClip):Void {
if (mcLocal._width>=45) {
mcLocal._width -= 1;
mcLocal._height -= 1;
} else {
clearInterval(mcLocal.int1);
}
};
Re: Trying to save key strokes with a for loop Beppo5
4/1/2007 5:02:55 AM
kglad,
Thanks for taking a look. In answer to your question. The movie clip
instance names are mc_ItemText0 ... mc_ItemText3.
[Q]those declarations at the top of your for-loop aren't doing anything
useful. what are the names of you movieclips that are on-stage?[/Q]

In reg\ards to the variable declarations, sorry my variable names are causing
confusion. I use the name "mc" for two different variables. Once for the FOR
loop and a separate time locally in the functions grow and shrink. The ones
set for the For loop are used in the 'onEvents' at the bottom of the script.
Sorry for the confusion.
Because the code works if I take it out of the FOR loop and replace the [i]'s
with the numbers that corruspond to the movie clip; I don't think the similar
names are conflicting because of the scope. To make the code more readable and
eliminate confusion I have posted the code with unique variable names.

I have searched the forum for FOR loop difficulties and found several cases
that seemed similar to mine, but I didn't understand what was happening.
Examples:

http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=28
8&threadid=1253110&highlight_key=y&keyword1=for%20loop

http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=28
8&threadid=1202338&highlight_key=y&keyword1=for%20loop

My code with unique variable names to end confusion:



for(var i=0; i<4; i++) {
trace(i)
var mc_ItemText[i]:MovieClip;
var inter[i]:Number;
var mc[i]:MovieClip = mc_ItemText[i]
//functions
grow = function(mcLocal:MovieClip):Void {
if (mcLocal._width<=55) {
mcLocal._width += 1;
mcLocal._height += 1;
} else {
clearInterval(int1);
}
}
shrink = function(mcLocal:MovieClip):Void {
if (mcLocal._width>=45) {
mcLocal._width -= 1;
mcLocal._height -= 1;
} else {
clearInterval(int1);
}
}

//events
mc[i].onRollOver = function():Void {
clearInterval(int1);
int1 = setInterval(grow,10,mc[i]);
}
mc[i].onRollOut = function():Void {
clearInterval(int1);
int1 = setInterval(shrink,10,mc[i]);
}
}
Re: Trying to save key strokes with a for loop Beppo5
4/4/2007 1:36:30 PM
When I run your code I get two errors:
"
**Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 2: Syntax error.
mc = this["mc_ItemText"+i];

**Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 11: Unexpected '}'
encountered
}
"
I am sure it has to do with how I have things set up. I am having problems
trouble shooting the issue because I don't understanding everything that is
going on.
Namely the "this" construction. what is the "this" keyword referring to?
the for loop?:
mc = this["mc_ItemText"+i];

How is this supposed to work?
also why is it having issues with the '}' in line 11

for (var i = 0; i<4; i++) {
mc = this["mc_ItemText"+i];
mc.onRollOver = function():Void {
clearInterval(this.int1);
this.int1 = setInterval(grow, 10, this);
};
mc.onRollOut = function():Void {
clearInterval(this.int1);
this.int1 = setInterval(shrink, 10, this);
};
} <-- this is the '}' in line 11. It closes the for loop.

Any advice as to how to move forward... My deadline looms ominously. Yikes!
Re: Trying to save key strokes with a for loop kglad
4/4/2007 2:23:59 PM
there's no syntax error and i can't think of anything you could do that would
cause that code to generate a syntax error.

try creating a new fla and paste all frame 1 layers into the new fla. retest.
if it works ok just copy the rest of your main timeline to the new main
timeline and continue your project in the new fla. the original fla may have
become corrupted.

p.s. "this" on line 2 refers to the timeline that contains that code. the
"this" in this.int1 refers to mc.
Re: Trying to save key strokes with a for loop Beppo5
4/4/2007 2:47:07 PM
kglad,
Thanks for all the help and attention here. The problem was I had strick
typed the variable mc in my code but didn't use the var keyword. But still I
can't get the code to work. The on events aren't being triggered for some
reason.
I have distilled the code down to the for loop and a trace statement that
executes onRollOver and onRollOut and nothing happens.

Just to go over what I have done. In the authoring enviroment I have created
four movieclips named mcItemText0 ... 3.
I gave each an instance name mc_ItemText0 .. 3.
They all live on the same frame and the same layer.
On a sepprate layer but on the same frame I added my action script (attached
below)

I am doing all of this in it's own file to get it right before I move it into
my project (just to limit confusion as well as avoiding corruption of the file)

Maybe it will be easier to find what I am doing wrong with less going on in
the code.

Thanks again.

ps. thanks for clearing up the 'this' mystery. It makes perfect sense to me
know.



for (var i = 0; i==3; i++) {
var mc:MovieClip = this["mc_ItemText"+i];
mc.onRollOver = function():Void {
trace("boogie")
};
mc.onRollOut = function():Void {
trace("woogie")
};
Re: Trying to save key strokes with a for loop Beppo5
4/4/2007 2:56:56 PM
One more thing.
do I use the 'this' construction when refering to actionscript Objects created
in the action script I am writting, or just for movieclips on the stage?

For example if I wanted to use a for loop to load strings from a text file
would I write:

var this["objLoadVars"+i]:LoadVars;
this["objLoadVars"+i] = new LoadVars();

or is the 'this' not needed because the loadVars object doesn't sit in the
timeline... or does it.

It is these kinds of issues that I have been having for a long time. I
finally feel comfortable enough understanding and writting code that I think I
can figure it all out finally and start doing cool stuff with flash.

Thanks
AddThis Social Bookmark Button