Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 ActionScript/Flash-加载外部程序创建的SWF?_Actionscript 3_Load_Flash_External - Fatal编程技术网

Actionscript 3 ActionScript/Flash-加载外部程序创建的SWF?

Actionscript 3 ActionScript/Flash-加载外部程序创建的SWF?,actionscript-3,load,flash,external,Actionscript 3,Load,Flash,External,到目前为止,我从未做过任何交叉脚本编写,我一开始就遇到了一个(可能真的很愚蠢)错误 外部SWF: 我在Flash Professional CS5中创建了一个新的ActionScript 3.0项目。在第一帧中,我添加了以下脚本: //Square.fla frame script import flash.display.Shape; import flash.events.Event; var s:Shape = new Shape(); s.graphics.beginFill(0x0

到目前为止,我从未做过任何交叉脚本编写,我一开始就遇到了一个(可能真的很愚蠢)错误

外部SWF: 我在Flash Professional CS5中创建了一个新的ActionScript 3.0项目。在第一帧中,我添加了以下脚本:

//Square.fla frame script

import flash.display.Shape;
import flash.events.Event;

var s:Shape = new Shape();
s.graphics.beginFill(0x0000FF, 1.0);
s.graphics.drawRect(-100, -100, 200, 200);
s.graphics.endFill();

s.x = stage.stageWidth / 2;
s.y = stage.stageHeight / 2;

addChild(s);

addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);

function enterFrameEventHandler(evt:Event):void
{
    s.rotation += 2;
}
保存、编译、完成。作为一个独立的swf,它可以很好地工作,只是在舞台中央显示一个旋转的蓝色正方形

主SWF: 我在Flash Professional CS5中创建了一个新的ActionScript 3.0文件,其中有一个名为CrossScriptTest的文档类:

错误: 我收到以下错误:

TypeError:Error#1009:无法访问null的属性或方法 对象引用。位于佛罗里达州广场::主时间线/frame1()

也许我误解了交叉脚本编写或加载外部swf文件的本质,但我似乎只能在舞台上手动绘制显示对象的情况下才能做到这一点,而外部swf的显示对象是由代码生成的


是否无法加载通过编程创建的外部主权财富基金并将其添加到主主权财富基金的显示列表中?

是的,可以这样做,但您的方式似乎不正确。您可以简单地使用加载程序:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener("complete", loader_complete);
loader.load(new URLRequest("yourfile.swf"));

function loader_complete(event:*):void {
    var loadedObject:* = event.target;
    // Access the properties of the loaded SWF here
}
编辑:

package
{
    //Imports
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Sprite
    import flash.display.Loader;
    import flash.events.IOErrorEvent;
    import flash.net.URLRequest;
    import flash.events.Event;

    //Class
    [SWF(width = "1000", height = "500", backgroundColor = "0x444444")]
    public class CrossScriptTest extends Sprite
    {
        //Constants
        private static const SQUARE_SWF_URL:String = "Square.swf";

        //Variables
        private var swfLoader:Loader;

        //Constructor
        public function CrossScriptTest()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;

            init();
        }

        //Initialize
        private function init():void
        {
            swfLoader = new Loader();
            swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
            swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteEventHandler);
            swfLoader.load(new URLRequest(SQUARE_SWF_URL));
        }

        //IOError Event Handler
        private function IOErrorEventHandler(evt:IOErrorEvent):void
        {
            trace(evt);
        }

        //Loader Complete Event Handler
        private function loaderCompleteEventHandler(evt:Event):void
        {
            evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
            evt.currentTarget.removeEventListener(Event.COMPLETE, loaderCompleteEventHandler);

            addChild(evt.currentTarget.content);
        }
    }
}
package
{
    //Imports
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.events.Event;

    //Class
    public class Square extends Sprite
    {
        //Constructor
        public function Square()
        {
            init();
        }

        //Initialize
        private function init():void
        {
            addEventListener(Event.ADDED_TO_STAGE, addedToStageEventHandler);
        }

        //Added To Stage Event Handler
        private function addedToStageEventHandler(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStageEventHandler);

            var s:Shape = new Shape();
            s.graphics.beginFill(0x0000FF, 1.0);
            s.graphics.drawRect(-100, -100, 200, 200);
            s.graphics.endFill();

            s.x = stage.stageWidth / 2;
            s.y = stage.stageHeight / 2;

            addChild(s);

            s.addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
        }

        //Enter Frame Event Handler
        function enterFrameEventHandler(evt:Event):void
        {
            Shape(evt.currentTarget).rotation += 2;
        }
    }
}
一般来说,尽量不要使用框架脚本,尤其是在SWF中加载SWF时,因为它们的工作方式并不总是显而易见的。例如,我不知道脚本中的根是什么-是SWF的根还是父SWF的根?(我不知道,因为为了避免这种问题,我从不使用框架脚本)。为了避免这种情况,您可以简单地使用文档类并将所有代码放在其中

也许可以尝试类似的方法,看看是否有效:

package some.unique.path {

    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.events.Event;

    public class YourClass extends MovieClip {

        public function YourClass():void {
            var s:Shape = new Shape();
            s.graphics.beginFill(0x0000FF, 1.0);
            s.graphics.drawRect(-100, -100, 200, 200);
            s.graphics.endFill();

            s.x = stage.stageWidth / 2;
            s.y = stage.stageHeight / 2;

            addChild(s);

            addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
        }

        function enterFrameEventHandler(evt:Event):void
        {
            s.rotation += 2;
        }

    }

}

事后的解决办法总是那么明显。当然,解决方案是分配一个
事件。将\u添加到外部swf文档类的构造函数或初始化方法中的\u STAGE
事件侦听器

在我的辩护中,这一点被忽略了,因为在创建标准(内部?)文档类时,它既不是必需的,也不是常见的

主SWF的文档类:

package
{
    //Imports
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Sprite
    import flash.display.Loader;
    import flash.events.IOErrorEvent;
    import flash.net.URLRequest;
    import flash.events.Event;

    //Class
    [SWF(width = "1000", height = "500", backgroundColor = "0x444444")]
    public class CrossScriptTest extends Sprite
    {
        //Constants
        private static const SQUARE_SWF_URL:String = "Square.swf";

        //Variables
        private var swfLoader:Loader;

        //Constructor
        public function CrossScriptTest()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;

            init();
        }

        //Initialize
        private function init():void
        {
            swfLoader = new Loader();
            swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
            swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteEventHandler);
            swfLoader.load(new URLRequest(SQUARE_SWF_URL));
        }

        //IOError Event Handler
        private function IOErrorEventHandler(evt:IOErrorEvent):void
        {
            trace(evt);
        }

        //Loader Complete Event Handler
        private function loaderCompleteEventHandler(evt:Event):void
        {
            evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
            evt.currentTarget.removeEventListener(Event.COMPLETE, loaderCompleteEventHandler);

            addChild(evt.currentTarget.content);
        }
    }
}
package
{
    //Imports
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.events.Event;

    //Class
    public class Square extends Sprite
    {
        //Constructor
        public function Square()
        {
            init();
        }

        //Initialize
        private function init():void
        {
            addEventListener(Event.ADDED_TO_STAGE, addedToStageEventHandler);
        }

        //Added To Stage Event Handler
        private function addedToStageEventHandler(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStageEventHandler);

            var s:Shape = new Shape();
            s.graphics.beginFill(0x0000FF, 1.0);
            s.graphics.drawRect(-100, -100, 200, 200);
            s.graphics.endFill();

            s.x = stage.stageWidth / 2;
            s.y = stage.stageHeight / 2;

            addChild(s);

            s.addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
        }

        //Enter Frame Event Handler
        function enterFrameEventHandler(evt:Event):void
        {
            Shape(evt.currentTarget).rotation += 2;
        }
    }
}
外部SWF的文档类:

package
{
    //Imports
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Sprite
    import flash.display.Loader;
    import flash.events.IOErrorEvent;
    import flash.net.URLRequest;
    import flash.events.Event;

    //Class
    [SWF(width = "1000", height = "500", backgroundColor = "0x444444")]
    public class CrossScriptTest extends Sprite
    {
        //Constants
        private static const SQUARE_SWF_URL:String = "Square.swf";

        //Variables
        private var swfLoader:Loader;

        //Constructor
        public function CrossScriptTest()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;

            init();
        }

        //Initialize
        private function init():void
        {
            swfLoader = new Loader();
            swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
            swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteEventHandler);
            swfLoader.load(new URLRequest(SQUARE_SWF_URL));
        }

        //IOError Event Handler
        private function IOErrorEventHandler(evt:IOErrorEvent):void
        {
            trace(evt);
        }

        //Loader Complete Event Handler
        private function loaderCompleteEventHandler(evt:Event):void
        {
            evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
            evt.currentTarget.removeEventListener(Event.COMPLETE, loaderCompleteEventHandler);

            addChild(evt.currentTarget.content);
        }
    }
}
package
{
    //Imports
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.events.Event;

    //Class
    public class Square extends Sprite
    {
        //Constructor
        public function Square()
        {
            init();
        }

        //Initialize
        private function init():void
        {
            addEventListener(Event.ADDED_TO_STAGE, addedToStageEventHandler);
        }

        //Added To Stage Event Handler
        private function addedToStageEventHandler(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStageEventHandler);

            var s:Shape = new Shape();
            s.graphics.beginFill(0x0000FF, 1.0);
            s.graphics.drawRect(-100, -100, 200, 200);
            s.graphics.endFill();

            s.x = stage.stageWidth / 2;
            s.y = stage.stageHeight / 2;

            addChild(s);

            s.addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
        }

        //Enter Frame Event Handler
        function enterFrameEventHandler(evt:Event):void
        {
            Shape(evt.currentTarget).rotation += 2;
        }
    }
}

哦,对不起,我没有意识到代码框正在滚动,我没有看到加载的代码。您是否在项目上启用了调试模式?这将告诉您错误发生在哪一行。调试模式没有帮助,因为我正在加载从帧脚本生成的swf。它只是说“无法在此位置显示源代码”,错误也是一样的。我已经更新了我的帖子。此外,调试也应该在子SWF上工作-只需在编译两个SWF时打开它即可。Flash应该向它们添加调试信息,您应该会得到更好的错误消息。我通常会(不惜一切代价)避免框架脚本。在这里使用框架脚本只是一个快速的失败测试。但是,我也尝试过创建文档类,但同样的错误仍然存在:
无法访问空对象引用的属性或方法,这次是在我的
init()
函数中查找错误,或者只查找构造函数。使用上面的代码实现这种方法,你运气好吗?这很奇怪,你的init()代码基本上看起来是正确的,我看不出有什么东西可以不定义。我只是想知道:您是否尝试过将变量“SWFLoader”重命名为其他变量
SWFLoader
是一个Flex类名,因此可能会混淆编译器。是的,DisplayObject的stage属性为空,直到该DisplayObject添加到DisplayList。