Preloader in actionscript 3 - Original error in getDefinition ()

I am writing a preloader:

package {
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.display.LoaderInfo;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.system.System;

    public class Preloader extends Sprite {
            private var t:TextField = new TextField();
            private var ver:String = "1000";
            public function Preloader() {
                    t.y = 570;
                    addChild( t );
                    t.text = ver;

                    addEventListener( Event.ADDED_TO_STAGE, init );
            }

            private function init( e:Event ):void {
                    this.root.loaderInfo.addEventListener( ProgressEvent.PROGRESS, onLoadingProgress );
                    this.root.loaderInfo.addEventListener( Event.COMPLETE, onLoadingCompleted );

                    // See if it already completed
                    var percent:int = int( root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal );
                    if ( percent == 1 )
                            onLoadingCompleted();
            }

            private function onLoadingProgress( event:Event ):void {
                    var percent:int = int(root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal * 100);
                    t.text = "Loading.. " + percent + "%";
            }

            private function onLoadingCompleted( event:Event = null ):void {
                    root.loaderInfo.removeEventListener( ProgressEvent.PROGRESS, onLoadingProgress );
                    root.loaderInfo.removeEventListener( Event.COMPLETE, onLoadingCompleted );

                    var mainClass:Class = loaderInfo.applicationDomain.getDefinition("Main") as Class;
                    var main:DisplayObject = new mainClass() as DisplayObject;

                    parent.addChild( main );
                    parent.removeChild( this );
            }
    }
}

with this as the main class:

package {
    import flash.display.Sprite;

    public class Main extends Sprite {

            public function Main() {
            }
    }
}

so it is close to the fact that i can do it like barebone.

However, he greets me:

ReferenceError: Error #1065: Variable Main is not defined.
at flash.system::ApplicationDomain/getDefinition()
at ...

I am using frame.frame to insert Main already. I am compiling with ant and linux SDK directly (mxmlc). I do not see anything obvious?

+3
source share
4 answers

preloader "", , , preloader , - . , , , , swf. getDefinition .

preloader, , . , .

, :

-frame start Main

, , :

-frame start com.grapefrukt.examples.Main

getDefinition.

EDIT:

, , , , , , :

var mainClass:Class = getDefinitionByName("com.grapefrukt.examples.Main") as Class;
addChild(new mainClass() as DisplayObject);

: , , - . , , loaderd. , :

if (currentFrame == totalFrames) onLoadingCompleted()

stop() onLoadingCompleted(), playhead , .

+4

, Game ( "" "game.Game" )?

, mainClass (this.root), mainClass() .

, , Event.INIT Event.COMPLETE?

+1

nextFrame(); onLoadingComplete nextFrame();

:

    public class Preloader extends MovieClip 
    {
    public function Preloader() 
    {
        stop();
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
        this.loaderInfo.addEventListener(Event.COMPLETE, startup);
        // Set up progress bar / graphics
    }

    private function onEnterFrame(e:Event):void 
    {
        // Display progress bar, loadedbytes, animation, etc...
    }

    private function startup(e:Event):void 
    {
        nextFrame();
        removeEventListener(Event.ENTER_FRAME, onEnterFrame);
        loaderInfo.removeEventListener(Event.COMPLETE, startup);
        var gameClass:Class = getDefinitionByName("Main") as Class;
        if (gameClass) addChild(new gameClass() as Sprite);
    }
    }

>

+1

, . . Adobe - :

, Event.COMPLETE "" ( , ), . Event.COMPLETE , swf - ( ) , RSL.

RSL, "" (, " " Flash Pro), , , - - , 2.

, 2 - , - . gotoAndStop(2) nextFrame() (.. eLouai ), , " ". - 1. , , - FlashPlayer. , . . play(), - .

- - swf , currentFrame == 2 ( ) ENTER_FRAME. nextFrame() gotoAndStop(2) ( ) ENTER_FRAME, , currentFrame == 2. , , , , , .

:

play() , 2 . gotoAndStop(2) nextFrame() ( ) , 2 . Event.COMPLETE , . , RSL - .

+1
source

Source: https://habr.com/ru/post/1723170/


All Articles