2012-11-23 54 views
2

我試圖做一個閃存,每次啓動.swf文件時文本動態更新文本文件。使用動作文本從文本文件更改動態文本3

我不是最聰明的,但我會嘗試解釋我想要做什麼。

我想有一個.txt文件在一定的格式。與此相似

例如:

Team1: Time 
    Player1: Dusk 
    Player2: Dawn 
    Player3: Noon 
    Team2: Food 
    Player1: Pizza 
    Player2: Cheese 
    Player3: Bread 

然後輸出的每個元素,並將它們輸出到具有相同名稱的動態文本對象之後的文本。

我想要一個名爲Team1的空文本對象:在這個腳本運行後,它會說「時間」而不是空白。

我已經嘗試了幾種不同的閱讀文件的方式,但是它涉及到拆分並將其發送給我遇到問題的動態文本對象。

與閃光燈適當的調整最終的結果會是這個樣子

Time  vs  Food 
    Dusk     Pizza 
    Dawn     Cheese 
    Noon     Bread 

這是我有什麼截至目前

var TextLoader:URLLoader = new URLLoader(); 
    TextLoader.addEventListener(Event.COMPLETE, onLoaded); 
    function onLoaded(e:Event):void { 
     var PlayerArray:Array = e.target.data.split(/\n/); 
    } 
    TextLoader.load(new URLRequest("roster1.txt")); 

當前的代碼,所以真正的問題是,我如何正確分割這與我使用的格式,然後將動態文本設置爲文本後跟標籤(team1 :, player1:等)

任何幫助將不勝感激

回答

0

這裏是分裂的數據的快速和骯髒的嘗試:

它假定前綴和值將被分開「:」和「團隊」是用來確定一個團隊的開始。

它循環遍歷字符串數組,並沿着「:」拆分每個字符串,然後檢查前綴是否包含字符串「Team」以確定它是否是新團隊的開始,還是當前是目前的球隊。

//assumes this is the starting state of the data 
var playerArray:Array = new Array(); 
playerArray.push("Team1: Time", 
"Player1: Dusk", 
"Player2: Dawn", 
"Player3: Noon", 
"Team2: Food", 
"Player1: Pizza", 
"Player2: Cheese", 
"Player3: Bread"); 

var teams:Array = new Array(); 
var currentTeam:Array = new Array();; 
var prefix:String; 
var value:String; 
for(var counter:int = 0; counter < playerArray.length; counter++){ 
    prefix = playerArray[counter].substring(0, playerArray[counter].indexOf(": ")); 
    value = playerArray[counter].substring(playerArray[counter].indexOf(": ") + ": ".length); 

    // found a team prefix, this is the start of a new team 
    if(prefix.indexOf("Team") != -1){ 
     teams.push(currentTeam); 
     currentTeam = new Array(); 
     currentTeam.push(value); // add the name of the currentTeam to the array 
    } else { 
     // else this should be a player, add it to the currentTeam array 
     currentTeam.push(value); 
    } 
} 
// add the last team 
teams.push(currentTeam); 
// remove the first empty team array just due to the way the loop works 
teams.shift(); 

trace(teams.length); // traces 2 
trace(teams[0]); // traces the team members of first team 
trace(teams[1]); // traces the team members of next team 

結果是一個團隊數組數組,其中每個團隊數組的索引0是團隊名稱,其後是玩家。

從這裏你應該能夠創建textfields(或使用現有的)並設置數組中的文本。

也許別人可以想出一個更有效的方法?我還嘗試通過將它合併爲一個長字符串並沿着「團隊」,然後是「播放器」,然後是「:」來嘗試將其分離出來,但是它會變得更加混亂,並且可能出現錯誤玩家的名字中包含「團隊」或「玩家」。