使用AS3添加文本框号

时间:2013-10-14 08:58:05

标签: actionscript-3

我一直都在做这一切。我希望每个人都可以提供帮助。所以...这样做是将9个文本框号码添加到动态文本框中。所以这是我的问题。

  1. 如何用0替换空文本框,如果用户摆脱已经存在的0,则会出现NaN。下面的if语句应该修复它,也许有人可以改进它。

    stage.addEventListener(Event.CHANGE, checkTotal); nextQuestion_btn.addEventListener(MouseEvent.MOUSE_DOWN, nextQuestion);

    function checkTotal(e:Event){
    
    var work:Number = parseInt(work_txt.text);
    var rnr:Number = parseInt(rnr_txt.text);
    var exerciseB:Number = parseInt(exerciseB_txt.text);
    var exerciseM:Number = parseInt(exerciseM_txt.text);
    var chores:Number = parseInt(chores_txt.text);
    var social:Number = parseInt(social_txt.text);
    var food:Number = parseInt(food_txt.text);
    var twt:Number = parseInt(twt_txt.text);
    var partying:Number = parseInt(partying_txt.text);
    var other:Number = parseInt(other_txt.text);    
    
    if(work_txt.text==""){
    work=0;
    }
    if(rnr_txt.text==""){
    rnr=0;
    }
    if(exerciseB_txt.text==""){
    exerciseB=0;
    }
    if(exerciseM_txt.text==""){
    exerciseM=0;
    }
    if(chores_txt.text==""){
    chores=0;
    }
    if(social_txt.text==""){
    social=0;
    }
    if(food_txt.text==""){
    food=0;
    }
    if(twt_txt.text==""){
    twt=0;
    }
    if(partying_txt.text==""){
    partying=0;
    }
    if(other_txt.text==""){
    other=0;
    }
    
    var total400:Number = work + rnr + exerciseB + exerciseM + 
    chores + social + food + twt + partying + other;
    
  2. function checkTotal(e:Event){ var work:Number = parseInt(work_txt.text); var rnr:Number = parseInt(rnr_txt.text); var exerciseB:Number = parseInt(exerciseB_txt.text); var exerciseM:Number = parseInt(exerciseM_txt.text); var chores:Number = parseInt(chores_txt.text); var social:Number = parseInt(social_txt.text); var food:Number = parseInt(food_txt.text); var twt:Number = parseInt(twt_txt.text); var partying:Number = parseInt(partying_txt.text); var other:Number = parseInt(other_txt.text); if(work_txt.text==""){ work=0; } if(rnr_txt.text==""){ rnr=0; } if(exerciseB_txt.text==""){ exerciseB=0; } if(exerciseM_txt.text==""){ exerciseM=0; } if(chores_txt.text==""){ chores=0; } if(social_txt.text==""){ social=0; } if(food_txt.text==""){ food=0; } if(twt_txt.text==""){ twt=0; } if(partying_txt.text==""){ partying=0; } if(other_txt.text==""){ other=0; } var total400:Number = work + rnr + exerciseB + exerciseM + chores + social + food + twt + partying + other;
    
    
    

    1. I can't let my text boxes add up over 400 so as the user types in 399 into one box, if the user types 2 into the next that current text box will revert to 0 because it would be over 400.

    I was told using e.currentTarget could solve that problem but I'm not sure how to use it.

    All my code...This is my first time on this site so please forgive me for my noobness.

1 个答案:

答案 0 :(得分:1)

<强> Q1

如果值只能是int类型,请尝试使用int而不是Number

var work:int = parseInt(work_txt.text);//work will be 0 if the text is empty

<强> Q2

如果要将文本还原为0(输入2的文本框)

function checkTotal(e:Event){ 

   var target:TextField = e.target as TextField;

   if(total400 >= 400){

       if (target) {//you may check if target is one of the text box you have listed.

          target.text = "0";

       }

       nextQuestion_btn.visible=true;
   }
}