1

我一直在做这一切。我希望每个人都可以提供帮助。所以...这是在做 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;
    

  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.

work_txt.maxChars = 3;
rnr_txt.maxChars = 3;
exerciseB_txt.maxChars = 3;
exerciseM_txt.maxChars = 3;
chores_txt.maxChars = 3;
social_txt.maxChars = 3;
food_txt.maxChars = 3;
twt_txt.maxChars = 3;
partying_txt.maxChars = 3;
other_txt.maxChars = 3;

work_txt.restrict = "0-9"
rnr_txt.restrict = "0-9"
exerciseB_txt.restrict = "0-9"
exerciseM_txt.restrict = "0-9"
chores_txt.restrict = "0-9"
social_txt.restrict = "0-9"
food_txt.restrict = "0-9"
twt_txt.restrict = "0-9"
partying_txt.restrict = "0-9"
other_txt.restrict = "0-9";

/*work_txt.text = "0";
rnr_txt.text = "0";
exerciseB_txt.text = "0";
exerciseM_txt.text = "0";
chores_txt.text = "0";
social_txt.text = "0";
food_txt.text = "0";
twt_txt.text = "0";
partying_txt.text = "0";
other_txt.text = "0";*/

var survival:Number = 0;

nextQuestion_btn.visible=false;

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;

trace(work);
trace(rnr);
trace(exerciseB);
trace(exerciseM);
trace(chores);
trace(social);
trace(food);
trace(twt);
trace(partying);
trace(other);
trace(total400);

total400_txt.text = String(total400);   

    if(total400 >= 400){
        nextQuestion_btn.visible=true;
    }else{
        nextQuestion_btn.visible=false;
    }
}

4

1 回答 1

1

第一季度

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

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

第二季度

如果要将文本恢复为 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;
   }
}
于 2013-10-14T09:29:04.390 回答