Search code examples
actionscript-3

actionscript 3 - Error #2136 - simple issue


This is extremely basic, but to help me understand could someone please explain why this doesn't work. Trying to call a function from one as file to another, and get the following error.

Error: Error #2136: The SWF file file:///test/Main.swf contains invalid data.
at code::Main()[C:\Users\Luke\Desktop\test\code\Main.as:12]
Error opening URL 'file:///test/Main.swf'

Main.as

package code {

        import flash.display.MovieClip;
        import flash.events.*;

        import code.Enemy;  

    public class Main extends MovieClip 
    {
        public function Main()
            {
                var enemy:Enemy = new Enemy();
            }

            public function test():void
            {
                trace("Test");
            }
      }
}

Enemy.as

package code {

        import flash.display.MovieClip;
        import flash.events.*;

        import code.Main;

    public class Enemy extends Main {

        public function Enemy() {

            var main:Main = new Main();
            main.test();

        }
    }
}

Solution

  • Assuming Main is your document class, you can't instantiate it. That might explain the SWF invalid data error.

    What it looks like you are trying to do is access a function on Main from your Enemy. To do that you just need a reference to Main from inside your Enemy class. If you add the Enemy instance to the display list you can probably use root or parent to get a reference to Main. You could also pass a reference to Main through the constructor of your Enemy class:

    public class Main {
        public function Main() {
            new Enemy(this);
        }
        public function test():void {
            trace("test");
        }
    }
    
    public class Enemy {
        public function Enemy(main:Main) {
            main.test();
        }
    }