简体   繁体   English

如何卸载外部“ .swf”文件来加载另一个文件?

[英]How do I unload an external “.swf” file to load another?

I am a student who's working for months on a game and now I've got stuck on a problem. 我是一个在游戏上工作了几个月的学生,现在我陷入了一个问题。 I am new to actionscript 3 but i learn fast. 我是ActionScript 3的新手,但我学得很快。

I can load my menu screen ("startScreen.swf") into my game (firstgame) automatically and everything works fine when I click play or storyScreen or instructionsScreen. 我可以将菜单屏幕(“ startScreen.swf”)自动加载到我的游戏(firstgame)中,然后单击“ play”或“ storyScreen”或“ instructionsScreen”,一切正常。 But the problem is that I want to return after pressing ESC button. 但是问题是我想按ESC按钮后返回。 I have tried many code but nothing works exactly how I want. 我已经尝试了很多代码,但是没有任何功能可以满足我的要求。

Example: I click on story and the swf (storyScreen.swf) loads and I can read the story and when I am finished, I want to press ESC to reload my swf (startScreen.swf) and all the functions in it. 示例:单击故事,然后加载swf(storyScreen.swf),可以阅读该故事,完成后,我想按ESC重新加载swf(startScreen.swf)及其中的所有功能。 Like play and instructions. 喜欢玩耍和指示。

You can find my code and empty space in the function ( esc ). 您可以在函数( esc )中找到我的代码和空白区域。

I know it is maybe easy to solve but I really don't know how. 我知道这可能很容易解决,但我真的不知道如何解决。 :( :(

public class FirstGame extends MovieClip
{

    public var Player:MovieClip

    private var leftKeyIsDown:Boolean;
    private var RightKeyIsDown:Boolean;

    private var aMissileArray:Array;
    private var aEnemyArray:Array;

    public var scoreTxt:TextField;
    public var ammoTxt:TextField;
    public var MenuEnd:EndGameScreen;
    public var menuAgain:EndGameScreen;

    private var MenuStart:mcStartGameScreen;
    private var MenuStory:mcStartGameScreen;
    private var MenuInstructions:mcStartGameScreen;

    private var nScore:Number;
    private var nAmmo:Number;
    private var tEnemyTimer:Timer;


    public function FirstGame() 
    {
        //Create a loader object
        var startLoader:Loader = new Loader();
        //add event listener to listen for the complete event
        startLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startLoaded);
        //load our loader object
        startLoader.load(new URLRequest("startScreen.swf"));
    }

    public function startLoaded(e:Event):void 
    {
        MenuEnd.hideScreen();
        Player.visible = false;
        scoreTxt.visible = false;
        ammoTxt.visible = false;

        //get a reference to the loaded movieclip
        MenuStart = e.target.content as mcStartGameScreen;
        //listen for start game event
        MenuStart.addEventListener("START_GAME", playGameAgain);
        //add it to the stage
        addChild(MenuStart);

        //get a reference to the loaded movieclip
        MenuStory = e.target.content as mcStartGameScreen;
        //listen for start game event
        MenuStory.addEventListener("SHOW_STORY", storyGameScreen);
        //add it to the stage
        addChild(MenuStory);

        //get a reference to the loaded movieclip
        MenuInstructions = e.target.content as mcStartGameScreen;
        //listen for start game event
        MenuInstructions.addEventListener("SHOW_INSTRUCTIONS", instructionsGameScreen);
        //add it to the stage
        addChild(MenuInstructions);
    }

    private function instructionsGameScreen(e:Event):void 
    {
        var instructionsLoader:Loader = new Loader();
        var url:URLRequest = new URLRequest("instructionsScreen.swf");
        instructionsLoader.load(url);
        addChild(instructionsLoader);

        stage.addEventListener(KeyboardEvent.KEY_UP, esc);
    }

    private function storyGameScreen(e:Event):void 
    {
        var storyLoader:Loader = new Loader();
        var url:URLRequest = new URLRequest("storyScreen.swf");
        storyLoader.load(url);
        addChild(storyLoader);

        stage.addEventListener(KeyboardEvent.KEY_UP, esc);
    }

    private function esc(e:KeyboardEvent):void 
    {
        if (e.keyCode == 27) 
        {
            //fscommand("quit");

            /////////test//////////////////(new URLRequest(stage.loaderInfo.url), "FirstGame.swf");

        }
    }


    private function playGameAgain(e:Event):void 
    {

        //initialize variables
        aMissileArray = new Array();
        aEnemyArray = new Array();
        nScore = 0;
        nAmmo = 20;
        Player.x = 262,95
        Player.y = 323,30

        Player.visible = true;
        scoreTxt.visible = true;
        ammoTxt.visible = true;

        MenuStart.hideScreen();


        MenuEnd.addEventListener("PLAY_AGAIN", playGameAgain);
        MenuEnd.hideScreen();

        updateScoreText();
        updateAmmoText();

        //trace("First Game Loaded");
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);

        stage.addEventListener(Event.ENTER_FRAME, gameLoop)

        //creat an timer object
        tEnemyTimer = new Timer(1000)
        //listen for the timer ticks/intervals
        tEnemyTimer.addEventListener(TimerEvent.TIMER, addEnemy)
        //start our timer
        tEnemyTimer.start();

    }

I believe you should be able to start a new request on the same loader object. 我相信您应该能够在相同的加载器对象上启动新请求。 Either way, you're dealing with object creation + cleanup. 无论哪种方式,您都在处理对象创建和清理。 The crux of the solution I'm offering is that you reuse your loaders. 我提供的解决方案的关键是您可以重复使用装载机。

Your current code is somewhat repetitious, so I've modified it slightly to demonstrate how you could simplify the code. 您当前的代码有些重复,因此我对其进行了一些修改,以演示如何简化代码。 Some thoughts while reviewing your code: 查看代码时的一些想法:

  • You're adding the same object to the stage multiple times (ie., MenuStart, MenuStory, MenuInstructions); 您要多次向舞台添加同一对象(即,MenuStart,MenuStory,MenuInstructions); these are all pointers to the same root swf you loaded (aka, startLoader). 这些都是指向您加载的同一根swf的指针(也称为startLoader)。
  • You've registered events the stage at multiple locations. 您已经在多个位置注册了舞台的事件。 Best practice is to place these in your constructor as they are persistent. 最佳做法是将它们放置在您的构造函数中,因为它们是持久性的。
  • Because you want to reuse your loaders at a later point, keeping a table with them makes it easier to reference. 因为您想在以后重用装入程序,所以与它们保持一张表可以更轻松地进行引用。
  • Any time you find yourself programming the same code in similar ways, it's a good indication that you can simplify with a single function (simply change the arguments). 每当您发现自己以类似的方式对相同的代码进行编程时,就很好地表明您可以使用单个函数进行简化(只需更改参数)。

Give this a try: 试试看:

public var Player:MovieClip

private var leftKeyIsDown:Boolean;
private var RightKeyIsDown:Boolean;

private var aMissileArray:Array;
private var aEnemyArray:Array;

public var scoreTxt:TextField;
public var ammoTxt:TextField;
public var MenuEnd:EndGameScreen;
public var menuAgain:EndGameScreen;

private var MenuStart:mcStartGameScreen;
private var MenuStory:mcStartGameScreen;
private var MenuInstructions:mcStartGameScreen;

private var nScore:Number;
private var nAmmo:Number;
private var tEnemyTimer:Timer;

private var screens:Object = {
    "SHOW_INSTRUCTIONS":{
        "loader":new Loader(),
        "url":new URLRequest("instructionsScreen.swf")
    },
    "SHOW_STORY":{
        "loader":new Loader(),
        "url":new URLRequest("storyScreen.swf")
    },
    "START_GAME":{
        "loader":new Loader(),
        "url":new URLRequest("startScreen.swf")
    }
}

public function FirstGame() {
    var startLoader:Loader = loadScreen({"type":"START_GAME"});
    //Register our event listeners
    startLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startLoaded);
    stage.addEventListener(KeyboardEvent.KEY_UP, esc);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
    stage.addEventListener(Event.ENTER_FRAME, gameLoop)
}

private function loadScreen(e:Object):Loader {
    screens[e.type].loader.load(screens[e.type].url);
    addChild(screens[e.type].loader);
    return screens[e.type].loader;
}

public function startLoaded(e:Event):void {
    //initialize variables
    aMissileArray = new Array();
    aEnemyArray = new Array();
    nScore = 0;
    nAmmo = 20;
    Player.x = 262, 95
    Player.y = 323, 30
    Player.visible = true;
    scoreTxt.visible = true;
    ammoTxt.visible = true;
    MenuEnd.addEventListener("PLAY_AGAIN", playGameAgain);
    MenuEnd.hideScreen();
    updateScoreText();
    updateAmmoText();

    //creat an timer object
    tEnemyTimer = new Timer(1000)
    tEnemyTimer.addEventListener(TimerEvent.TIMER, addEnemy)
    tEnemyTimer.start();

    var swfRoot = screens.START_GAME.loader["content"];
    swfRoot.addEventListener("START_GAME", loadScreen);
    swfRoot.addEventListener("SHOW_STORY", loadScreen);
    swfRoot.addEventListener("SHOW_INSTRUCTIONS", loadScreen);
}

private function esc(e:KeyboardEvent):void {
    if (e.keyCode == 27) {
        //fscommand("quit");

        // Loop through our loaders, and reset them all.
        for each (var entry:Object in screens) {
            entry.loader.unloadAndStop();
            removeChild(entry.loader);
        }

        // Reload the start screen
        var startLoader:Loader = loadScreen({"type":"START_GAME"});
        startLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startLoaded);
    }
}

The last thing I want to be guilty of is telling you how to run your project, however, I think you may be taking the wrong approach, here. 我要犯的最后一件事是告诉您如何运行您的项目,但是,我认为您在这里可能采用了错误的方法。

Instead of setting up the story and the game as separate swf files, what you would probably need to do is set up the story as a MovieClip, and the game as a MovieClip. 与其将故事和游戏设置为单独的swf文件,您可能需要做的是将故事设置为MovieClip,将游戏设置为MovieClip。 (Yes, MovieClips can contain other symbols, including other MovieClips.) Then, you'll want to hide/show or add/remove these MovieClips from your stage using code. (是的,MovieClips可以包含其他符号,包括其他MovieClips。)然后,您将需要使用代码从舞台中隐藏/显示或添加/删除这些MovieClips。

If you need to know how to do this, let me give you the age-old admonishment: RTD (Read the Documentation). 如果您需要知道如何执行此操作,请让我给您一个古老的建议:RTD(请阅读文档)。 These are basic tasks that are covered both in the online Adobe Flash documentation, and across numerous tutorials online. 这些是在线Adobe Flash文档以及众多在线教程中介绍的基本任务。

I hope that helps! 希望对您有所帮助!

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

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