Skip to main content
July 11, 2014
Question

I have nearly completed the basics of AS3 and was just fiddling with the script and got struck. here is the problem.

  • July 11, 2014
  • 1 reply
  • 265 views

I have 30 frame and two layers flashCS6 file . in layer I have a dynamic text field- ' info_txt;'

In layer 2 on frame 1 -           var count:Number = 1;

                  frame 2                info_txt.text = String(count);

                   frame 14               count++;

when I test this movie the text field displays 1 and there is no increment when the playhead loops again.

now if I add another script after fame 14 which is

say on frame 15   -

if(count > 4) {

gotoAndStop("home");

} else {

  gotoAndPlay("loop");

}

the script works perfectly well after i add the conditionals at frame 15( there is increment of the numbers in text field.

there are no errors reported in the output and the file keeps playing in both the cases and I also have the labels 'loop' in frame 2 and 'home' in frame 30.

why there has to be a conditional statement after 'count++' for the text field to increment every time the play head moves through frame 14?

Am I missing some rule in the AS3?

and if, after adding the conditional script in the frame after the 'count++'  , I move the label  'loop' from frame 2 to frame 1 even then there is no increment of number in the text frame when i test the movie.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
July 11, 2014

there are, at least, two potential issues.

1.  you're resetting count every time you enter frame 1.  i'm not sure you want to do that.  if not, just declare count  where you have it assigned and assign its value in an if-statement that uses a boolean to execute once.  eg

var alreadyExecuted:Boolean;

var count:Number;

if(!alreadyExecuted){

alreadyExecuted=true;

count=1;

}

2.  if info_txt fails to exist on some frame that plays and you use info_text instead of count, you will have a problem.

July 11, 2014

Thanks kglad

cheers man!  I could not figure out that I was resetting the variable again to 1 in frame 1. and I didnot know that we can add the 'alreadyExecuted' statement haven't read about it yet.

Your code is working just fine. only that i get 'NAN' in the text field in the first loop and then the number starts increasing.

You reply has been very useful.

But why even if i add the

if(count > 4) {

gotoAndStop("home");

} else {

  gotoAndPlay("loop");

conditional the code works fine. Why is it that the count variable is not set to 1 again in this case.

kglad
Community Expert
Community Expert
July 11, 2014

assign your textfield's text property after the if(!alreadyExecuted) statement to prevent your initial NaN error.

the count is reset to 1 if you enter frame 1, otherwise it's not.

in addition, you may have other code executing between that frame 1 code and your if(count>4) code.