如何将外部.swf预加载器与实际的.swf结合起来?

时间:2013-12-21 22:04:53

标签: actionscript-3 flash

我有一个.swf加载外部.swf文件,这是游戏。有没有办法将swfs合二为一?这是加载外部游戏的.swf中的代码:

import flash.display.*;
import flash.net.*;
import flash.events.*;

var array:Array = new Array ("Loading Bugs..","Everyone hates loading screens..","Lovely day, isn't it?", 
"Want instant updates? Follow us on Twitter!", "A game where you can kill bugs, pretty cool...huh?"); //create an array of possible strings
var randomIndex:int = Math.floor ( Math.random () * array.length ); //generate a random integer between 0 and the length of the array
loading_txt.text = array [ randomIndex ]; //put the random string in your text field
var myRequest:URLRequest = new URLRequest("insectGame.swf");

var myLoader:Loader = new Loader();
myLoader.load(myRequest);

function showProgress(evt:ProgressEvent):void 
{

    txtPreloader.text = Math.round((evt.bytesLoaded/evt.bytesTotal)*100) + "%";


    mcPreloaderBar.width = Math.round(evt.bytesLoaded/evt.bytesTotal * 200);    
}

function showLoaded(evt:Event):void 
{
    myLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,showProgress);
    myLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,showLoaded);
    removeChild(txtPreloader);
    removeChild(mcPreloaderBar);
    removeChild(mcPreloaderFrame);
    removeChild(Mosquito);
    removeChild(loading_txt);
    mcPreloaderBar = null;
    mcPreloaderFrame = null;
    txtPreloader = null;
    loading_txt = null;
    addChild(myLoader);
}

myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoaded);

1 个答案:

答案 0 :(得分:0)

您可能希望在主swf文件的第一帧中实现预加载器。所以你应该拥有实际的.swf的来源。如果您使用的是Flash Builder,请查看mxmlc compiler -frame参数。

这种方法的核心思想是将预加载器指定为应用程序的主类,向swf文件添加1个帧,并且不直接对gameClass实例化。当内容完全加载(framesLoaded == totalFrames)时,您可以使用以下代码创建游戏类的实例:

var gameClass:Class = Class(getDefinitionByName("GameClass"));
if(gameClass)
{
 var app:Object = new gameClass();
 addChildAt(app as Sprite, 0);
}

这里有用的链接: http://www.drewing.de/blog/2009/09/23/building-a-preloader-with-actionsscript-3-using-mxmlc-and-the-frame-pragma/