10

I have an actionscript 3 library item, "BG", that is linked to the class BGClass. BG contains a Sprite that has an instance name, "bg" and likewise BGClass has a public bg property. So the class looks like this:

public class BGCass extends Base {

    public var bg:Sprite;

    public function BGCass() {
        bg.width = 200
    }
}

Everything works fine. But if I wish to move the public bg into the Base class like this I get the error.

public class BGCass extends Base {
    public function BGCass() {
        bg.width = 200
    }
}

public class Base extends Sprite {

    public var bg:Sprite;

    public function Base() {
    }
}

I have tried using getter setters in Base and overriding them in BGClass and I still get the error. Is this a bug in Flash? Is there a clean solution or do I need to create some sort of proxy variable to finally get bg to Base? I know that turning off "automatically declare stage instances" in Flash will get rid of the error but I need to keep it on for the designers. Any solutions?

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
Sunny
  • 131
  • 1
  • 1
  • 6
  • So you have a movie clip that is called BG? And its base class is BGClass? The BGClass extends Base? and the BG movie clip contains a movie clip with an instance name of "bg"? I'm asking this just to make sure I've got the structure right, which I think can use some revising, along with revisions to the names you have chose. – Taurayi Apr 24 '11 at 11:46

3 Answers3

8

You have a couple of options.

  1. Rename your "bg" variable or the "bg" stage instance to something else so that they don't match.

  2. If you go to File > Publish Settings... > Flash tab and click the "Settings..." button next to Actionscript 3.0 you'll see an option checked off by default labelled "Automatically declare stage instances". If you disable this option, the error you're seeing will disappear, although you might see some other errors pop up as a result.

If you go with option 2, I believe that the variable will be automatically populated with a reference to the stage instance, if you leave their names the same.

Some background: When you create a library item with a base class, Flash creates a new class behind the scenes which extends the class you've chosen. By default, Flash is configured to give that class a set of member variables that match the children you've placed inside the MovieClip in the authoring environment.

Andrew Traviss
  • 249
  • 1
  • 7
  • I'm giving the Sprite in BG an instance name of "background" (instead of "bg") and then I set bg = background in the BGClass. Kind of sloppy but it seems to be the only solution. "Automatically declare stage instances" will not work for me because I need my scripts to work weather "Automatically declare stage instances" is set or not. It just seems like a bug as Flash does not give the error if bg is defined in the main class. The error only appears if bg is defined in the super. – Sunny Apr 25 '11 at 04:40
  • -1 This answer is completely wrong. Will put up an answer shortly explaining. – The_asMan Apr 29 '11 at 17:38
  • 2
    Odd, because his response directly above yours indicates that his problem is solved by following my advice, although he hasn't formally accepted my answer yet. – Andrew Traviss May 01 '11 at 00:17
  • Turning off "Automatically declare stage instances" works perfectly - as long as you know EXACTLY what the hell you are doing. If you are a good developer, and use `getChildByName('myVar')` instead of just trying to `this.myVar` it will suppress any erroneously raised var namespace override warnings in super-extended classes. – 1owk3y Oct 24 '17 at 05:56
0

just delete BG from extended MovieClip. BG come to extended MovieClip from base MovieClip.

-1

What you are forgetting to do is instantiate the bg sprite. All you did was type cast it to Sprite.
Basically, what you are doing is trying to access a null sprite

var bg:Sprite;
bg.width // bg is null at this point

var bg:Sprite;
bg = new Sprite()
//or
var bg:Sprite = new Sprite()

so if you instantiate you won't have the issue

public class Base extends Sprite {

    public var bg:Sprite;

    public function Base() {
        this.bg = new Sprite( );
    }
}

Also the error with your code that you posted does not relate to the error you posted. so this is just half of your problem if you still get the error after you make this change let me know.

The_asMan
  • 6,364
  • 4
  • 23
  • 34