2013-10-13 130 views
0

我正在嘗試構建一個簡單的Flash遊戲,如果用戶死亡,我需要重新啓動遊戲。但重置所有變量似乎並沒有完成任何事情。我所有的遊戲元素都存儲在數組中,我認爲可能將它們中的每一個都設置爲數組構造函數,而不是刪除它們指向的對象,並將它們留在屏幕上。有誰知道一種方法來刪除這些元素(因爲我不能遍歷列表刪除它們出於顯而易見的原因)還是有人知道更好的方式來重置閃存遊戲?作爲參考,這裏是我的程序頂部的init函數和變量聲明,它們應該啓動/重置遊戲。重置Flash遊戲時遇到問題

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 
     } 

回答

1

既然我無法看到所有的代碼,我會想,你是不是使用removeChild之()擺脫顯示的對象走下舞臺。

要創建一個對象:

var object:DisplayObject = new DisplayObject(); 

要使其顯示列表中可見:

parent.addChild(object); 

要從顯示列表中刪除:

parent.removeChild(object); 

要清除其內存:

object=null; 

(這四件事情必須按照這個順序完成,這樣才能正常工作。如果你在沒有將它從顯示列表中刪除的情況下創建一個null,那麼你將它留在那裏,仍然可見,無法引用它。它就像你的應用程序丟失一樣。 您必須確保在變量爲null或覆蓋該變量之前始終使用removeChild()。