Be careful with "stop()" method

This topic is related to AS2.0 .

Recently I made a discovery after a research on a very strange "return value missing" problem.
The situation is like this:

I have a class A which extends MovieClip and calls "stop()" method to stop the play head in its construction method. Everything seems normal except I can't get return value from any method who create instance of A.
For example, I have the following code:

In file A.as:

class A extends MovieClip{
  function A(){
    stop();
  }
}

In file test.fla:
Make an empty movieclip, set its linkage id to "testa" and AS2.0 class to "A".
Write following code in timeline:
function strangeMethod(){
  _root.attachMovie("testa","test",_root.getNextHighestDepth());
  var x=_root.test;
  return 1;
}
var a=0;
a=strangeMethod();
trace(a);

Everyone can see that the result should be "1".
But guess what's the result of the above code?
It's "0" !!!
But if I change the code into:
var a=strangeMethod();
trace(a);

The result is normal.

When I first found this problem, the class I used was much more complicated than the above A.as. So It took me a lot of time to spot the cause of this phenomenon. Finally I got it. The source is "stop()"! When I change "stop()" into "this.stop()", the problem disappears.
I guess when you call "stop()" in a class, actually you are not calling the stop method of a movieclip. Maybe it's the ancient timeline "stop()" method who is taking control. And it looks like the method does more than just stop the play head. It seems it also stopped the passing of return value or at least interfered with the value passing.

So my advice is that you should always use "this.stop()" instead of "stop()" in a class definition. I didn't try gotoAndStop or gotoAndPlay, but I think they may act the similar way, so you'd better use this.gotoAndStop and this.gotoAndPlay instead.

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.