如何将文本框值推送到Array并调用它

时间:2015-11-03 11:16:06

标签: actionscript-3

这是我第一次在本网站上询问,如果有任何错误和不恰当的事情提前道歉

我试图使用动作脚本3制作我自己的篮球比分板,但是我仍然在调用球员犯规得分并单独展示。

在图片中,第二个方框是输入犯规的球员号码的位置,第三个方框是数字显示该球员被犯规的次数。 The second box is where to type a player number who made foul and the third box is where the number shows how many times this player fouls.

我需要知道如何编写一个数组存储,它接收来自“玩家”文本框的值作为玩家编号,并将犯规计数与特定玩家的编号一起存储(如果我输入另一个玩家编号则会计算犯规数分开,下次我输入存在的数字,它会调出他犯规的次数)

1 个答案:

答案 0 :(得分:0)

您可以使用数组,字典甚至动态属性。

我们假设您的文字字段名为txtTeam1foulstxtPlayertxtFoulstxtTeam2fouls。我们还假设您有一个名为curTeam的var,它存储了您输入其玩家编号的团队的整数标识符(例如,12)。

以下是在Array中存储基本对象的示例:

var fouls:Array = []; //create a new empty array

//add a listener for when you type something into the player text input
txtPlayer.addEventListener(KeyboardEvent.KEY_UP, updatePlayer);

//this function retries a foul record from the array for a specific player
function getFouls(player:int, teamId:int):Object {
    //loop through the array until you find a match
    for(var i:int=0;i<fouls.length;i++){
        if(fouls[i].player === player && fouls[i].team === teamId){
            return fouls[i];
        }
    }
    //if no record in the array, return 0
    return null;
}

//this function updates the foul text field when you change the what's in the player text field
function updatePlayer(e:Event):void {
    var foulRecord = getFouls(int(txtPlayer.text), curTeam); 
    //if a foul record exists, use it's foul count, if not use 0
    txtFouls.text = foulRecord ? foulRecord.fouls.toString() : 0;
}

//call this function whenever you add a new foul record. 
function addFoul(player:int, teamId:int):void {

    //first, see if there is an existing foul record in the array
    var foulObj:Object = getFouls(player, teamId);

    if(!foulObj){
        //if there was no record, create one, then push (add) it to the array
        foulObj = {team: teamId, player: player, fouls: 1};
        fouls.push(foulObj);
    }else{
        //if there is an existing record, increment it.
        foulObj.fouls++;
    }

    //now update the totals for each team
    var team1Ctr:int = 0;
    var team2Ctr:int = 0;

    for(var i:int=0;i<fouls.length;i++){
        switch(fouls[i].team){
            case 1:
                team1Ctr++;
                break;

            case 2:
                team2Ctr++;
                break;
        }
    }

    txtTeam1Fouls.text = team1Ctr.toString();
    txtTeam2Fouls.text = team2Ctr.toString();
}