如何从实例化子类将外部 swf 加载到主舞台?

发布于 2024-08-29 00:08:47 字数 2216 浏览 4 评论 0原文

我试图获取一个类的实例来加载外部 swf 并显示它。 到目前为止我有以下内容: 1)我编写了一个类,它使用Loader类加载外部swf“loadExtSWF”。 2)我有一个名为“MainSWF.fla”的fla,它使用文档类“MainSWF.as”。 3) 我有MainSWF.as 文件,它实例化“loadExtSWF”并调用loadExtSWF.startLoad 来加载swf。

这几乎有效。 loadExtSWF 实例加载外部 swf,但不显示 swf。

如果我用 loadExtSWF (它有一个空的构造函数)而不是 MainSWF 替换 fla 的文档类,并运行 loadExtSWF.startLoad,则将加载并显示外部 swf。

看来我最初的做法是将 swf 加载到错误的阶段(?)。

有什么想法吗?感谢您的帮助。

再见, 拉姆伊

如果将 test_tsscreen 的文档类从 test_tsscreen.as 替换为 TSScreen.as,并删除 test_tsscreen 构造函数内的注释,则将加载 swf。

我的代码是:

文件 test_as3.swf

一个外部 as3 swf 文件。

文件 test_tsscreen.fla

fla 为空并引用 test_tsscreen.as 作为其文档类。

文件 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);
  }
 }
}

文件 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);
  }


 }
}

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);
  }


 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2024-09-05 00:08:47

有时,提出问题可以找到答案。再次阅读手册后,我发现一旦在 AS3 中添加新的 swf,您需要将新的 DisplayObject 作为子项添加到现有 DisplayObjectContainer 中的现有 DisplayObject。

所以,我遇到的问题是我正确加载了外部 swf,但没有将其添加到 DisplayList,因此它没有显示。

这是我之前引用的代码块的固定版本

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 {
导入 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);
    }


}

如果

您想了解更多信息,请参阅“FriendsofED Foundation Actionscript 3.0 With Flash CS3”一书的第 04 章,其中包含对此主题的有用介绍。

希望这可以帮助其他遇到同样问题的人。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文