Search code examples
flashactionscript-3classflash-cs5document-class

Flash cs5 code location confusion


Whenever I place the following code on the first frame of my movie, it works fine:

var strGlobal:String = "Global"; 
function scopeTest() 
{ 
    trace(strGlobal); // Global 
} 
scopeTest(); 
trace(strGlobal); // Global

But when I remove it and place it in my document class, it errors out: "Call to a possible undefined method scopeTest" "Access to undefined property strGlobal"

I am new to actionscript 3 and was wondering what to change to make this simple example work from my main document class.


Solution

  • In the same directory as your .fla file create a file named ScopeTest.as and define the following class:

    package  {
        import flash.display.MovieClip;
        public class ScopeTest extends MovieClip  {
            var strGlobal:String = "Global"; 
            public function ScopeTest() {
                trace(strGlobal); // Global 
            }
        }
    }
    

    Then inside your .fla project (document class):

    enter image description here

    Then when you run it you should get "Global" traced out to confirm it works.