Skip to main content
Participant
August 27, 2009
Answered

Newbie question about CLASS

  • August 27, 2009
  • 3 replies
  • 435 views

I am trying to start my first flash website by using actionscript

and I have a problem with loading image with outside actionsctipt file.

here is the code:

package {
    import flash.display.MovieClip;
    import flash.display.Loader;
    import flash.net.URLRequest;


    public class Index_page extends MovieClip {
        private var req:URLRequest=new URLRequest("picture/index_page/image_logo.png");
        private var logoLoader:Loader = new Loader();
        logoLoader.load(req);
        addChild(logoLoader);
        }

    }

when I publish the swf file, it comes out the error message saying,

access to undefined property logoLoader as well as req and logoLoader.

and the method addChild also became undefined.

How could that be since I`ve already declared those variables at the beginning?

But, if I put them in a function like this, it works.


public function Index_page() {
            logoLoader.load(req);
            addChild(logoLoader);
        }

my point is,

Does it neccessary to make so many lines for only loading an image?

or there is a better way to do this. I am not really famiier with package

and class things...thanks for any help!

This topic has been closed for replies.
Correct answer

Code within a class must be within a method, or in the constructor. The example you gave:

public class Index_page extends MovieClip {
        private var req:URLRequest=new URLRequest("picture/index_page/image_logo.png");
        private var logoLoader:Loader = new Loader();
        logoLoader.load(req);
        addChild(logoLoader);
        }

as code outside of any method... you can't do that. In your second example you placed it in the constructor - the method that is automatically run when the class is instantiated - the method that has the same name as the class. Only variable declarations can go outside a method definition...

3 replies

Participant
August 28, 2009

Thanks for your kindly replys!

kinda start getting the idea

Correct answer
August 27, 2009

Code within a class must be within a method, or in the constructor. The example you gave:

public class Index_page extends MovieClip {
        private var req:URLRequest=new URLRequest("picture/index_page/image_logo.png");
        private var logoLoader:Loader = new Loader();
        logoLoader.load(req);
        addChild(logoLoader);
        }

as code outside of any method... you can't do that. In your second example you placed it in the constructor - the method that is automatically run when the class is instantiated - the method that has the same name as the class. Only variable declarations can go outside a method definition...

kglad
Community Expert
Community Expert
August 27, 2009

you can declare your variables outside your constructor  (function Index_page() ), but they should be defined in that constructor or in another function body.