问题描述:

I am trying to get an instance of a class to the load an external swf and show it.

So far I have the following:

1) I wrote a class that uses the Loader class to load an external swf "loadExtSWF".

2) I have a fla named "MainSWF.fla" that uses a document class "MainSWF.as".

3) I have the MainSWF.as file that instances "loadExtSWF" and calls loadExtSWF.startLoad to load the swf.

This almost works. The instance of loadExtSWF loads the external swf, but the swf is not displayed.

If I replace the fla's document class with loadExtSWF (this has an empty constructor) instead of MainSWF, and run loadExtSWF.startLoad, then the external swf is loaded and displayed.

It seems that the way I initially do it, loads the swf to the wrong stage (?).

Any ideas? Thanks for the help.

Bye,

RaamEE

P.S.

If you replace the document class for test_tsscreen from test_tsscreen.as to TSScreen.as, and remove the comment inside the test_tsscreen's constructor, the swf will be loaded.

my code is:

file test_as3.swf

an external as3 swf file.

file test_tsscreen.fla

the fla is empty and references test_tsscreen.as as its document class.

file test_tsscreen.as

package {

import flash.display.MovieClip;

import TSScreen;

public class test_tsscreen extends MovieClip{

var tsScreen1;

public function test_tsscreen(){

// var tsScreen1:TSScreen = new TSScreen(10,10,100,100,0.5,0);

var tsScreen1:TSScreen = new TSScreen();

tsScreen1.startLoad(this.stage);

}

}

}

file TSScreen.as

package {

import flash.display.MovieClip;

import flash.display.*;

import flash.net.URLRequest;

import flash.system.ApplicationDomain;

import flash.system.LoaderContext;

import flash.display.Loader;

import flash.events.Event;

import flash.events.ProgressEvent;

public class TSScreen extends MovieClip implements ITSScreenable{

public function TSScreen():void{

// startLoad(this); //Look important comment in above text

}

function startLoad(_this:Stage) {

var mLoader:Loader = new Loader();

var mRequest:URLRequest = new URLRequest("test_as3.swf");

mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);

mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);

_this.parent.addChild(mLoader);

mLoader.load(mRequest);

trace(this.name);

trace(_this.name);

}

function onCompleteHandler(loadEvent:Event) {

addChild(loadEvent.currentTarget.content);

}

function onProgressHandler(mProgress:ProgressEvent) {

var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;

trace(percent);

}

}

}

网友答案:

As sometimes happens, asking the question leads to finding your answer. After reading the manual, again, I saw that once you add a new swf in AS3, you need to add your new DisplayObject as a child to an existing DisplayObject in an existing DisplayObjectContainer.

So, the problem I had is that I loaded the external swf properly but didn't add it to a DisplayList, so it wasn't displayed.

Here is a fixed version of the code blocks I quoted earlier

test_tsscreen.as

package {

import flash.display.MovieClip;
import TSScreen;

public class test_tsscreen extends MovieClip{
    var tsScreen1:TSScreen;

    public function test_tsscreen(){
        tsScreen1 = new TSScreen(10,10,100,100,0.5,0);
        //tsScreen1 = new TSScreen();
        this.addChildAt(tsScreen1,1);
    }
}

}

TSScreen.as

package { import flash.display.MovieClip;

import flash.display.*;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;

import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;  

public class TSScreen extends MovieClip {
    private var _xPos:int;
    private var _yPos:int;
    private var _width:int;
    private var _height:int;
    private var _xScale:int;
    private var _yScale:int;

    private var _visible:Boolean;
    private var _alpha:Boolean;
    private var _shadow:Boolean;
    private var _backgroundColor:uint;
    private var _rotation:Boolean;

    private var _displayTime:int;


    public function TSScreen():void{
        startLoad();
    }


    function startLoad() {
        var mLoader:Loader = new Loader();
        var mRequest:URLRequest = new URLRequest("C:/FlashProjects/test_as3.swf");

        mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
        mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
        addChild(mLoader);
        mLoader.load(mRequest);
    }

    function onCompleteHandler(loadEvent:Event) {
        addChild(loadEvent.currentTarget.content);
    }

    function onProgressHandler(mProgress:ProgressEvent) {
        var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
        trace(percent);
    }


}

}

If you'd like more information please chapter 04 of the book "FriendsofED Foundation Actionscript 3.0 With Flash CS3" which contains helpful introduction into this subject.

Hope this helps others with the same problem.

相关阅读:
Top