zzxjoanw
zzxjoanw

Reputation: 384

AS3 doesn't recognize a variable I just declared

I'm trying to load a background image, but I'm getting an error saying "Error: Access of undefined property assetLoader." What's going on here?

import flash.display.Loader;
import flash.net.URLRequest;
class Inventory {
    private var assetLoader:Loader = new Loader();
    assetLoader.load(new URLRequest("image.png")); //error on this line
    addChild(assetLoader);
}

Upvotes: 0

Views: 75

Answers (3)

Benny
Benny

Reputation: 2228

  1. If you are using addChild() method you must inherits the features of DisplayObjectContainer. And if you are using your Inventory class as document class, you must extends Sprite or MovieClip.

  2. Document class must be defined by public access specifier.

  3. Only globally(Class property definitions) declared variables are allowed to use private and public. You are not allowed to use it locally(within functions). And Timeline also not allow you to use access specifiers.

    package 
    {
       import flash.display.Loader;
       import flash.net.URLRequest; 
       import flash.display.MovieClip;
    
       public class Inventory extends MovieClip 
       {
            private var assetLoader:Loader;
            public function Inventory() 
            {
                // constructor code
                assetLoader= new Loader();
                assetLoader.load(new URLRequest("image.png")); //error on this line
                addChild(assetLoader);
            }
       }
    }
    

Upvotes: 3

BotMaster
BotMaster

Reputation: 2223

The correct answer is that even tough you can instantiate instances at declaration time like here:

private var assetLoader:Loader = new Loader();

You are not allowed to work with those objects prior to the class instance existing. Any attempt to access assetLoader properties and methods prior to creating an instance of Inventory will fail. The constructor is the first piece of code that an instance of Inventory will run so it's the first place in the class code where you can start to use the class instance objects because at this point the Inventory instance exists. Vesper code example shows it correctly.

In theory this:

private var assetLoader:Loader = new Loader();

is equivalent to this:

private var assetLoader:Loader;

public function Inventory()
{
    assetLoader = new Loader();
}

but in fact the timing of the assetLoader creation differs slightly. It is always better to create those member instance in constructor.

To Benny: All classes have an access modifier that defaults to internal. The PO class is defined as internal and so has correctly an access modifier (the internal default since none is specified). The access modifier of the member variable is correctly defined and is not related to the PO problem.

Upvotes: 1

Vesper
Vesper

Reputation: 18747

You need to place these two lines in the constructor code, like this:

import flash.display.Loader;
import flash.net.URLRequest;
class Inventory {
    private var assetLoader:Loader = new Loader();
    public function Inventory() {
        assetLoader.load(new URLRequest("image.png")); //error on this line
        addChild(assetLoader);
    }
}

Upvotes: 2

Related Questions