简体   繁体   English

将 SWF 加载到 ActionScript 3 项目 (Flex Builder)

[英]Loading a SWF into an ActionScript 3 project (Flex Builder)

We're developing a small Flash gamelet for an European client of ours, who require us to develop our Flash game in such fashion that they can load our SWF game into their ActionScript 3 project, dynamically.我们正在为我们的一个欧洲客户开发一个小的 Flash gamelet,他们要求我们以这样的方式开发我们的 Flash 游戏,以便他们可以动态地将我们的 SWF 游戏加载到他们的 ActionScript 3 项目中。

Basically they have a console that will load many other Flash games, amongst our own, depending on which button the player presses on a "Choose Your Game" screen.基本上,他们有一个控制台,可以加载许多其他 Flash 游戏,包括我们自己的游戏,具体取决于玩家在“选择您的游戏”屏幕上按下的按钮。

Basically, we have a FLA project that we're authoring in Flash Professional CS4 and everything is basically a straightforward Flash game.基本上,我们有一个 FLA 项目,我们正在 Flash Professional CS4 中创作,一切基本上都是一个简单的 Flash 游戏。 When we test the game via Ctrl+Enter or run the compiled SWF file by double clicking it, all works well, the game executes perfectly and everybody is a happy bunny.当我们通过 Ctrl+Enter 测试游戏或双击运行编译后的 SWF 文件时,一切正常,游戏运行完美,每个人都是快乐的兔子。

What we need to grasp is loading our SWF into our clients AS3 project, which is basically an external ActionScript 3 project created in Flex Builder 3. The client posted us the following code to load the SWF:我们需要掌握的是将我们的 SWF 加载到我们的客户 AS3 项目中,它基本上是在 Flex Builder 3 中创建的外部 ActionScript 3 项目。客户向我们发布了以下代码来加载 SWF:

var myGame:FunkyChickenGame = new FunkyChickenGame();
addChild(myGame);

...which gets executed in the ActionScript 3 application constructor of the clients app, or perhaps an event handler for a button pressed on the "Choose Your Game" screen. ...在客户端应用程序的 ActionScript 3 应用程序构造函数中执行,或者可能是在“选择您的游戏”屏幕上按下的按钮的事件处理程序。

We tried creating a blank AS3 project in Flex Builder and tried loading the SWF as per my snippet above.我们尝试在 Flex Builder 中创建一个空白的 AS3 项目,并尝试按照我上面的代码片段加载 SWF。 All our traces within the external SWF's document class are showing up as expected in the Flex Builder console view, so the code is running perfectly.我们在外部 SWF 文档 class 中的所有跟踪都按预期显示在 Flex Builder 控制台视图中,因此代码运行完美。

The problem we're experiencing is that despite calling addChild(myGame)...we're not seeing any graphics, just the default background color of the encapsulating AS3 project.我们遇到的问题是尽管调用了 addChild(myGame)...我们没有看到任何图形,只有封装 AS3 项目的默认背景颜色。

Note however, when we run the SWF by double clicking the Game.swf file in Windows, it all executes perfectly, the game works without flaw and glitches.但是请注意,当我们通过双击 Windows 中的 Game.swf 文件运行 SWF 时,一切都完美执行,游戏运行没有缺陷和故障。

Any help on this topic would be highly appreciated.对此主题的任何帮助将不胜感激。 Thank you in advance.先感谢您。

What you need to use is an instance of the Loader class, like so:您需要使用的是 Loader class 的实例,如下所示:

var loader:Loader = new Loader();

// you'll need to write a method named onLoaded to capture the COMPLETE event
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);

loader.load(new URLRequest("game.swf"));
addChild(loader);

Creating an instance of a class is as wage as it can get.创建 class 的实例是它能得到的工资。 Your client has a funny way of posting references.您的客户发布推荐信的方式很有趣。

However FunkyChickenGame may be a class created by the client that can launch your game.但是FunkyChickenGame可能是由可以启动您的游戏的客户端创建的 class。 Maybe the games are embedded in their flash app, but that would mean long loading time.也许游戏嵌入在他们的 flash 应用程序中,但这意味着加载时间很长。 If indeed many games are "loaded" this way.如果确实有很多游戏是这样“加载”的。

My advice would be that you don't use stage, root or whatever properties that could link to the main app instead your game, and leave the client to decide how he would like to load it.我的建议是您不要使用阶段、root 或任何可以链接到主应用程序而不是您的游戏的属性,并让客户决定他想如何加载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM