2014-03-31 27 views
0

基本上,我需要做一個平臺遊戲,側滾動擊敗他們。使用一個主遊戲循環來運行遊戲中的所有功能(As3)

這是一些僞代碼。

主時間軸,添加輸入框listenr,gameLoop

function gameLoop 

//start screen 
If(current frame is 2(start screen) 
{then run this code}  

If(current frame is 3(options) 
{run this code] 

if(current frame is 4(level1) 
{add key listeners, gravity();, movement();, collision();, score(); etc}  

if(current frame is 5, (death screen) 
{do this code} 

這將高效編碼或有更好的辦法?

Vesper,你的意思是這樣嗎?

public function Main() 
    { 
     //iniation players 
     character = new player; 
     timmy = new SirTimmy; 
     caroline = new princess; 
     //stage.addEventListener(Event.ENTER_FRAME, mainGameLoop) 

     //check EnterFrames 
     preLoader(); 
     titleScreen(); 
    } 

    private function titleScreen():void 
    { 
     if (currentFrame == 2) 
     { 
      stage.addEventListener(Event.ENTER_FRAME, titleScreenLoop) 
     } 
    } 

    private function titleScreenLoop(e:Event):void 
    { 
     titlePlay.addEventListener(MouseEvent.CLICK, goToStartScreen) 
    } 

    private function goToStartScreen(e:MouseEvent):void 
    { 
     if (e.target == titlePlay) 
     { 
      trace("go to Start Screen"); 
      this.gotoAndStop("Start Screen"); 
      stage.removeEventListener(Event.ENTER_FRAME, titleScreenLoop) 
     } 
    } 

回答

1

我想你最好在每次遊戲轉換狀態時重新添加輸入幀偵聽器和合適的代碼。這樣你就可以擁有四個事件監聽器,一個用於死亡屏幕,一個用於級別(主戰)屏幕等,當你要改變幀時,你會刪除舊的事件監聽器並添加一個新的監聽器。這將縮短事件處理程序,並讓您不用擔心主戰中發生的情況,同時屏幕上有選項菜單。

+0

這很有道理,謝謝。 所以每個幀都有它自己的輸入幀。我現在知道了。 當我在主屏幕中時,我將運行運行代碼的輸入框。 當我在選項屏幕上時,我將運行該框架並刪除主要輸入框架偵聽器。 非常感謝。 – Moynul

0

從您的描述來看,聽起來像'gameLoop'會觸發每一幀。在遊戲循環中,你不會想要檢查遊戲中的菜單階段。只有在玩家準備開始遊戲時才添加輸入框事件偵聽器(並且記住在遊戲停止時將其移除)。結構化菜單,以便在遊戲循環之前進行。

+0

忍者8秒。 Niiice :) – Vesper