2016-10-10 41 views
1

我一直在使用我製作了一年的初級引擎創建文本冒險。存儲文本冒險故事數據AS3

對於這個故事,我有一個對象數組(是,它叫什麼?)用各種故事數據的東西,我通過

解析有人告訴我,使用它我的方式是愚蠢的,因爲它是應該用於其他的東西,但我只使用它,因爲它很容易學會如何解析數據,因爲我是一個初學者

它是越來越乏味的寫故事和做每個部分的東西(創造的背景和這樣的)因爲它太長了。

有沒有什麼方法可以讓我更容易寫故事?

這裏的對象陣列設置(與選擇)單個零件

public static var parts:Object = 
{ 

    "0": 
     { 
      "text":"Text here", 
      "choices": 
       { 
        "response1": 
         { 
          "text":"Response1", 
          "nextPart":"1" 
         }, 
        "response2": 
         { 
          "text":"Response2", 
          "nextPart":"2" 
         } 

       }, 
       "background": Assets.AssetClass.Background1, 
       "BGM":"bg1" 
     }, 
} 

這裏是改變它們與部分如何我的引擎處理的例子,:

我有一個輸入檢查器檢查當輸入被按下,然後做什麼取決於屏幕上的東西

public function onEnter(button:KeyboardEvent):void 
    { 
     if (button.keyCode == 32 && !Won) 
     { 
      if (Dead && textFinished && !choosing) // pressing enter while dead and the text is still writing 
      { 
       curPart = parts[curPart]["lastPart"] // lastPart will only be stored in parts that have the player die 
       textFinished = false 
       Dead = false; 
       myTextField.text = "" 
       counter = 0; 
       sInt = setInterval(addCharackter, textSpeed) 
       if (stage.getChildByName("cText")) 
       { 
        stage.removeChild(continueText) 
       } 
       if (parts[curPart].hasOwnProperty("background")) //check if the background needs to change. 
       { 
        if (stage.getChildByName("img")) 
        { 
         stage.removeChild(background) 
        } 
        background = new Background(parts[curPart], 800, 600) 
        stage.addChildAt(background, 0) 
       } 
      } 
      if (!textFinished && !choosing)// pressing enter when there's no choices on the screen and the text isn't finished and the text is still writing 
      { 
       this.myTextField.text = this.parts[this.curPart]["text"]; 
       clearInterval(this.sInt); 
       this.textFinished = true; 

       if (parts[curPart].hasOwnProperty("choices")) 
       { 

        choosing = true 
        createOptions(); // function for parsing through the responses bit of that part and displaying them 
       } 
       else 
       { 
        stage.addChildAt(continueText, 2) 
       } 
       if (parts[curPart].hasOwnProperty("lastPart")) 
       { 
        Dead = true; 
        dead() 
       } 
      } 
      else if (textFinished && !choosing && !Dead) // pressing enter if there's no choices on the screen and there's no choices (it'll take you to the next part) 
      { 
       trace("Text finished!!") 
       curPart = parts[curPart]["nextPart"] 
       myTextField.text = "" 
       counter = 0; 
       sInt = setInterval(addCharackter, textSpeed) 
       textFinished = false; 
       if (parts[curPart].hasOwnProperty("background")) 
       { 
        if (stage.getChildByName("img")) 
        { 
         trace("Removed!") 
         stage.removeChild(background) 
        } 
        background = new Background(parts[curPart], 800, 600) 
        stage.addChildAt(background, 0) 
       } 
       if (parts[curPart].hasOwnProperty("BGM")) // does the current part have a new background music? 
       { 
        trace("Music!!!!") 
        sndBGMusic = musicArray[parts[curPart]["BGM"]] 
        sndBGMusicChannel.stop() 
        sndBGMusicChannel = sndBGMusic.play(0, 9999999999) 
        stage.addChildAt(background, 0) 
       } 
       stage.removeChild(continueText) 
      } 
     } 
    } 
+0

你可以提供一些關於你的代碼如何工作的上下文嗎?具體來說,我想知道如何在腳本的其他部分使用這個零件對象。 'parts'在您的引擎中實際上做了什麼?它看起來像使用玩家輸入來返回一些結果。我們能否看到一個如何使用這個「零件」對象的例子? –

+0

零件包含遊戲的所有場景。我通過解析每個部分並獲取相關數據(例如:文本,檢查是否有選擇)並根據該數據在屏幕上顯示內容。 – Ducktor

+0

你能通過編輯你的帖子在你的問題中展示一個例子嗎? –

回答

1

這裏有一些想法。這些只是我會做的不同於你所做的事情。我不保證他們以任何方式更好,但看看你的想法。

我將有一個全局變量爲_curPart。我會有一個名爲Part的自定義類。該課程將擁有_BGM,_bgImage等屬性。它也可以有_choicesArray作爲屬性。我會有其他全局變量,如_hasCandle。或者,您可以將項目存儲在數組中,如果您需要蠟燭,則只需檢查candle是否在陣列中。這些全局變量將從一個部分持續到下一個部分。

然後,您可以通過執行_curPart._bgImage訪問您所在部分的屬性。對我來說,這看起來更清潔。

並創建一個新的部分它可能看起來像(不完全):

var p15:Part = new Part(); 
p15._bgImage = image15; 
p15._BGM = song10; 
//... 

我推薦的最後一件事就是試圖重構在那裏你可以。例如,如果您有//pressing enter if there's no choic...,用一個或幾個函數調用(無論是最有意義的並允許您重用代碼)替換該括號中的所有代碼。這只是讓我們更容易看到發生了什麼,我想。因此,而不是所有這些if塊,只是像nextPart();這樣的函數,然後該函數將包含所有if塊。合理?個人喜好,但是當事情變得複雜時,重構幫助我清除蜘蛛網。就像您使用dead()createOptions()所做的一樣,但我只是更進一步。這不會讓你的代碼更有效率,但它可能會使的代碼更有效率,這在我的書中是最重要的(直到它不是)。