0

我正在尝试构建一个简单的 Flash 游戏,如果用户死亡,我需要能够在其中重新启动游戏。然而,重置所有变量似乎并没有完成任何事情。我所有的游戏元素都存储在数组中,我认为可能将它们中的每一个设置为数组构造函数并不会删除它们指向的对象,因此它们会留在屏幕上。有谁知道删除这些元素的方法(因为出于明显的原因我无法遍历列表以删除它们),或者有谁知道在 Flash 中重置游戏的更好方法?作为参考,这是我的程序顶部应该启动/重置游戏的 init 函数和变量声明。

public class Main extends MovieClip {

        //put field variables here
        static var hudLayer:Sprite; //layer used to represent HUD elements
        static var gameLayer:Sprite; //layer used to represent objects in the game space
        static var uiTextLayer:Sprite; //layer used to represent text that should appear ON TOP of the HUD
        static var backgroundLayer:Sprite; //layer used to display the background image
        static var players: Array; //array of all the player objects for reference
        static var backgrounds:Array; //array of all the background objects
        static var lines: Array; //array of all the lines
        static var powerUps:Array; //array of all the powerups
        static var enemies:Array; //array of all the enemies
        static var miscellaneousObjects:Array; //array of miscellaneous objects that I'd like to be able to keep track of
        var xCoords:Array, yCoords:Array; //Used to temporarily hold x and y coordinates when making new drawings
        static var grav:Number; //coefficient representing the force of gravity
        static var isPaused:Boolean; //manages pausing mechanic
        static var timer:Timer; //used for time delayed updating of game elements
        var pt:Point; //variable used by collision detection
        var asteroidTiming:Number; //used to properly delay creating of asteroids on the screen
        static var asteroidDelay:Number; //current delay between when asteroids are deployed, changes over course of execution
        static var score:Number; //this should be self-explanatory
        var scoreField:TextField; //used to display the score
        static var myTextFormat:TextFormat; //used to format the text in the scoreField
        static var inGameOver:Boolean; //used to determine if we're at the game over screen

        [Frame(factoryClass="Preloader")]
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        public function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            //set up the Sprite Layers
            backgroundLayer = new Sprite();
            gameLayer = new Sprite();
            hudLayer = new Sprite();
            uiTextLayer = new Sprite();
            addChild(backgroundLayer);
            addChild(gameLayer);
            addChild(hudLayer);
            addChild(uiTextLayer);

            //instantiate important variables
            xCoords = new Array();
            yCoords = new Array();
            players = new Array();
            backgrounds = new Array();
            enemies = new Array();
            powerUps = new Array();
            miscellaneousObjects = new Array();
            grav = .04;
            addBackGround();
            addPlayer(400, 50);
            isPaused = false;
            lines = new Array();
            score = 0;
            inGameOver = false;

            //instantiate text fields
            scoreField = new TextField();
            scoreField.text = "Score: " + score;
            hudLayer.addChild(scoreField);
            scoreField.x = 20;
            scoreField.y = 20;
            scoreField.textColor = 0xFFFFFF;
            myTextFormat = new TextFormat();
            myTextFormat.size = 15;
            scoreField.setTextFormat(myTextFormat);

            //set up timer
            timer = new Timer(5);
            timer.addEventListener(TimerEvent.TIMER, function():void { update()});
            timer.start()
            asteroidTiming = 0;
            asteroidDelay = 150;

            //set up mouse listener
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEvent);

            //set up key listener
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyEvent);

            //tests
        }
4

1 回答 1

1

由于我看不到您的所有代码,我猜您没有使用 removechild() 来摆脱舞台上的显示对象。

创建对象:

var object:DisplayObject = new DisplayObject();

要使其在显示列表中可见:

parent.addChild(object);

要将其从显示列表中删除:

parent.removeChild(object);

要清除其记忆:

object=null;

(这四件事必须按此顺序完成,才能正常工作。如果您将某些内容设为 null 而不将其从显示列表中删除,则将其留在那里,仍然可见,无法引用它。它就像迷失在你的应用程序。您必须确保在使变量为空或覆盖变量之前始终使用 removeChild()。

于 2013-10-13T19:43:34.093 回答