2
votes

In frame 1 of a very simple flash file, I have the following code:

stop();
this.addEventListener(MouseEvent.CLICK, function(e) { gotoAndStop(5); });
gotoAndStop(10);

When I run the flash file, I expect that it would automatically step to frame 5 (ED. we think he means frame 10), but it does not. However, clicking anywhere does cause it to step to frame 5. The question is, why does gotoAndStop work from within the event handler, but not on its own on the main timeline?

Furthermore, if I put the exact same code on any other frame, gotoAndStop fires in that frame without having to be clicked. It appears gotoAndStop only fails to work on frame 1.

Edit / Answer: The answer to this one is really stupid, but I'll put it here in case anyone comes across it in the future. The resulting flash file was 500k, which was just big enough, even locally, to cause the gotoAndStop command to fire before the file was completely loaded. As a result, it couldn't possibly go to frame 5 or 10 because they didn't exist yet.

I knew I had to put in a preloader, but it was last on the list of things to do, and apparently, that came back to bite me in the ass. I added the gotoAndStop to an Event.COMPLETE handler and it works perfectly. Problem solved.

3
It's probably a typo, but your expected result doesn't match the code you pasted (frame 5 vs frame 10). I tried your example and it works fine for me. There's probably some information you're leaving it that is causing the problem. Are those three lines the only AS code in the entire file? - Stiggler
Actually the frame number is irrelevant. I tried it with that being the only code in frame 1. There is some other code in frame 5, but I don't think it's relevant, as it doesn't touch the timeline at all (it just controls a netconnection/netstream for playing a video) - Chris
If you try creating a new FLA, adding a new keyframe at frame 10 and add a shape in that frame, and then put gotoAndStop(10) in the first frame, does it not work? - Stiggler
Man that used to bite me too when I coded on the timeline! It's because flash "streams" itself in if you have everything laid out on a timeline. Most developers just put everything in one frame these days. - Iain

3 Answers

0
votes

Could it be that you don't actually have a frame 10?

0
votes

You could try moving everthing along one frame? so don't put any code on frame 1, start from frame 2.

If you really want better control over Flash them I recomend coding in an external class file.

0
votes

For me, after just copying and pasting the above mentioned code in a blank flash file, the click was not working at all but the 'gotoAndStop (10)' was working .. However, when i assigned click listener to 'stage' instead of 'this', the click started to work.

the code i used was below:

  • stop();
  • stage.addEventListener(MouseEvent.CLICK, function(e) { gotoAndStop(5); });
  • gotoAndStop(10);

But since you are having problems with the 3rd line (i.e. gotoAndStop(10)), and you are not facing problems on clicking, you might want to try this.

  • stop();
  • var reference :MovieClip = this as MovieClip;
  • reference.addEventListener(MouseEvent.CLICK, function(e) { reference.gotoAndStop(5); });
  • reference.gotoAndStop (10);

Tahir.