1

我知道以前有很多关于预加载器的主题,我已经尝试关注其中的每一个,但我仍然遇到同样的问题(他们帮助我从 80% 到 50%)现在它开始61450 / 125207于约为 50%。

这是我的Main Document(整个项目的默认类文件)类:

public class MainDocument extends MovieClip
{
    private var preloader:Preloader;
    private var sB:startButton;
    public function MainDocument() 
    {
        preloader = new Preloader();
        preloader.x = 300;
        preloader.y = 400;
        addChild(preloader);
        loaderInfo.addEventListener(Event.COMPLETE,addStartButton,false,0,true);
    }
    private function addStartButton(e:Event):void
    {
        sB = new startButton();
        sB.x = 300;
        sB.y = 450;
        sB.addEventListener(MouseEvent.CLICK,sMainMenu,false,0,true);
        addChild(sB);
        loaderInfo.removeEventListener(Event.COMPLETE,addStartButton);
    }
    private function sMainMenu(e:Event):void
    {
        sB.removeEventListener(MouseEvent.CLICK,sMainMenu);
        removeChild(sB);
        removeChild(preloader);
        sB = null;
        preloader = null;           
        var menuScreen = new MenuScreen();
        addChild(menuScreen);

        //I have heard that the following code might work better:
        //var menuScreen:Class = getDefinitionByName("MenuScreen") as Class;
        //addChild(new menuScreen() as DisplayObject);
    }
}

以及Preloader它所附的:

public class Preloader extends MovieClip
{
    public function Preloader()
    {
        addEventListener(Event.ENTER_FRAME,Load);
    }
    private function Load(e:Event):void
    {
        //"bar" is a movieclip inside the preloader object
        bar.scaleX = loaderInfo.bytesLoaded/loaderInfo.bytesTotal;
        //"percent" is a dynamic text inside the preloader object
        percent.text = Math.floor(loaderInfo.bytesLoaded/loaderInfo.bytesTotal*100)+"%";
        trace(loaderInfo.bytesLoaded+" / "+loaderInfo.bytesTotal);
        if (loaderInfo.bytesLoaded == loaderInfo.bytesTotal)
        {
            removeEventListener(Event.ENTER_FRAME,Load);
        }
    }
}

->Export on Frame 1除了Preloader

-> 第一帧上不存在任何对象;第一帧上的唯一代码是stop();

-> 我在第二帧中放置了每个 MovieClip 的副本,当单击 startButton 时,agotoAndStop(3);运行,因此没有人看到第 2 帧。

如果有人知道我可能忘记的任何简单事情,请告诉我!

谢谢!

4

3 回答 3

1

您要在预加载的文件中使用预加载器。在这种情况下,项目的其余代码和资产将会膨胀。您看到您的预加载器似乎延迟的原因是因为 swf 必须在任何代码执行之前完全加载。这包括舞台上的所有资产,无论它们在哪个帧上,即使您已经设置了导出到第 1 帧以外的其他内容的设置。相反,请尝试使用空白外壳作为您的预加载器。这个 shell 除了加载器代码和预加载器图形或动画外,什么都没有。加载完成后,隐藏您的预加载器并将您加载的内容添加到 shell 的阶段,或 shell 中的容器影片剪辑。

以下所有代码都在您的 shell 中,它只是另一个 FLA 文件,其中只有这段代码和一个预加载器栏。该文件的尺寸应该与您正在加载到其中的文件相同,即您尝试预加载的原始 swf 文件。

通过调用loadSwf("mySwfNameOrURLToSwf.swf");来使用它 变量_percent将填充当前负载百分比,您可以对应于您的加载条比例。假设预加载器栏名为“bar”,则行bar.visible = false; 在 onSwfLoaded 函数中将隐藏它。 addChild( _swf )将加载的 swf 添加到 shell 的阶段。_swf.init()行;在加载的 swf 中引用一个函数,您需要添加一个名为init()的函数,它会启动加载的 swf 执行它应该执行的任何操作。现在让加载的 swf 中的所有内容都从第一帧开始,包括init()函数。

import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Bitmap;

import flash.net.URLRequest;

import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.system.LoaderContext;
import flash.system.Security;

import flash.events.Event;
import flash.events.ProgressEvent;

var _swfLoader:Loader;
var _swf:DisplayObject;
var _percent:Number;

function loadSwf( swfURL:String ):void
{
    _swfLoader = new Loader();
    var req:URLRequest = new URLRequest( swfURL );

    var loaderContext:LoaderContext = new LoaderContext();
    loaderContext.applicationDomain = ApplicationDomain.currentDomain;
    loaderContext.checkPolicyFile = true;

    _swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onSwfProgress);
    _swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoaded);

    _swfLoader.load(req, loaderContext);
}

function onSwfProgress( evt:Event ):void
{
    _percent = Math.round( ( evt.target.bytesLoaded / evt.target.bytesTotal ) * 100 );
}

function onSwfLoaded( evt:Event ):void
{
    _swfLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onSwfProgress);
    _swfLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onSwfLoaded);

    _swf = _swfLoader.content;

    addChild( _swf );
    bar.visible = false;
    _swf.init();

    _swfLoader = null;
}
于 2013-04-02T23:01:33.480 回答
0

你的代码看起来不错。当您不在 http 服务器上时,会模拟加载过程。

编译完成后按crtl+B。在菜单中可以选择下载速度,再次按ctrl+enter可以模拟下载。

它可能会帮助您调试预加载器

于 2013-04-02T09:40:00.857 回答
0

@Lee Burrows你说的是对的,但如果你看看我在代码末尾提到的内容会更好(三个粗体点)

我使用的解决方案是: -> 我将所有内容都设置为Export on Frame 2我的 3 帧文档。-> 删除第 2 帧上的所有内容 -> 创建了一个TextFieldvia 构造函数并用于drawRectangle加载栏 -> 第 1 帧上没有动画剪辑,并使用

var menuScreen:Class = getDefinitionByName("MenuScreen") as Class;
addChild(new menuScreen() as DisplayObject);

而不是之前的代码。

我最初没有工作的原因是,Lee Burrows如前所述,无论是否检查过,都会Export for Actionscript挂起加载 if X = 1in 。将其更改为 2 或取消选中是两个解决方案(除非它不是为 actionscript 导出的,否则它的代码不能被引用)。Export on Frame XExport on Frame 1Export for Actionscript

预加载器现在的起步价约为 2%。

于 2013-04-07T00:27:35.773 回答