2013-10-14 36 views
1

我已經有一段時間都在做這一切。我希望每個人都可以提供幫助。所以......這樣做的是拿出9個文本框號碼並將它們添加到動態文本框中。所以這是我的問題。使用AS3添加文本框編號

  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. 我不能讓我的文本框400加起來,從而將用戶在399到一個盒子,如果用戶鍵入2到下一個,目前的文本框會回到0,因爲它會超過400.

我被告知使用e.currentTarget可以解決這個問題,但我不知道如何使用它。

我所有的代碼...這是我第一次在這個網站上,所以請原諒我的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; 
    } 
} 

回答

1

Q1

如果值只能是int類型,嘗試使用int,而不是數

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; 
    } 
}