2012-03-16 96 views
0

所以我正在通過動作完全創建一個計算器的過程中,暫存區域中沒有任何東西。到目前爲止,我正在執行計算。但是,我似乎有問題。我的代碼示例如下圖所示:AS3計算器

import flash.events.MouseEvent; 

var btn:Array = new Array(); 

for(var i = 0; i < 10; i++) { 
var myBtn:Btn = new Btn(); 
myBtn.y = 15; 
myBtn.x = i * 100 + 15; 
myBtn.width = 48; 
myBtn.height = 48; 
myBtn.buttonMode = true; 
myBtn.mouseChildren = false; 
myBtn.num = Number(i); 
myBtn.caption.text = String (i); 
addChild(myBtn); 
btn.push(myBtn); 
btn[i].addEventListener(MouseEvent.CLICK,pressNumber);//EVENT TO ADD NUMBERS TO  DISPLAY 

} 
btn[0].y += 370; 
btn[0].x += 10; 

btn[1].y += 310; 
btn[1].x += -90; 

btn[2].y += 310; 
btn[2].x += -130; 

btn[3].y += 310; 
btn[3].x += -170; 

btn[4].y += 250; 
btn[4].x += -390; 

btn[5].y += 250; 
btn[5].x += -430; 

btn[6].y += 250; 
btn[6].x += -470; 

btn[7].y += 190; 
btn[7].x += -690; 

btn[8].y += 190; 
btn[8].x += -730; 

btn[9].y += 190; 
btn[9].x += -770; 

//OPERATORS// 
var operators:Array = new Array(); 
for(var io:int = 0; io < 6; io++) { 
var opBtn:Btn_operator = new Btn_operator(); 
opBtn.width = 48; 
opBtn.height = 48; 
opBtn.buttonMode = true; 
opBtn.mouseChildren = false; 
opBtn.caption_op.text = String ("."); 
addChild(opBtn); 
operators.push(opBtn); 
operators[io].addEventListener(MouseEvent.CLICK, pressOperator);//EVENT TO ADD OPERATORS TO DISPLAY 
} 

//ADD DOT 
var dot:Btn_dot = new Btn_dot(); 
dot.width = 48; 
dot.height = 48; 
dot.buttonMode = true; 
dot.mouseChildren = false; 
dot.caption_op.text = String ("."); 
addChild(dot); 

dot.y += 383; 
dot.x += 78; 
dot.addEventListener(MouseEvent.CLICK, addDot);//EVENT TO ADD DOT TO DISPLAY 

//BACKSPACE 
var goBack:Btn_backspace = new Btn_backspace(); 
goBack.width = 48; 
goBack.height = 48; 
goBack.buttonMode = true; 
goBack.mouseChildren = false; 
goBack.caption_op.text = String ("<--"); 
addChild(goBack); 

goBack.y += 203; 
goBack.x += 256; 
goBack.addEventListener(MouseEvent.CLICK, backSpace);//EVENT TO GO BACK SPACE IN DISPLAY 

//BACKSPACE 
var clearAll:Btn_clear = new Btn_clear(); 
clearAll.width = 48; 
clearAll.height = 48; 
clearAll.buttonMode = true; 
clearAll.mouseChildren = false; 
clearAll.caption_op.text = String ("C"); 
addChild(clearAll); 


clearAll.y += 323; 
clearAll.x += 256; 
clearAll.addEventListener(MouseEvent.CLICK, clearFields);//EVENT TO GO BACK SPACE IN DISPLAY 

operators[0].y += 383; 
operators[0].x += 138; 
operators[0].caption_op.text = String("="); 

operators[1].y += 383; 
operators[1].x += 198; 
operators[1].caption_op.text = String("/"); 

operators[2].y += 323; 
operators[2].x += 198; 
operators[2].caption_op.text = String("*"); 

operators[3].y += 263; 
operators[3].x += 198; 
operators[3].caption_op.text = String("-"); 

operators[4].y += 203; 
operators[4].x += 198; 
operators[4].caption_op.text = String("+"); 

operators[5].y += 263; 
operators[5].x += 256; 
operators[5].caption_op.text = String("-/+"); 


//VARIABLE HANDLE OPERATION NOT BUTTON 
var operate:String; 

//HANDLE FIRST AND SECOND VALUE 
var num1:Number; 
var num2:Number; 

//grouping all buttons in function 


//display_txt.text = "0"; 
//DISPLAYING NUMBERS IN DISPLAY 
//var numberEntered:String =""; 
function pressNumber(e:MouseEvent):void{ 
     display_txt.appendText(e.target.num); 
} 

//DISPLAY OPERATORS 
function pressOperator(event:MouseEvent):void{ 
var operatorEntered:String; 
trace("a",display_txt.text,display_txt.text.length,event.currentTarget.caption_op.t  ext,event.currentTarget.caption_op.text.length); 
display_txt.appendText(event.currentTarget.caption_op.text); 
//CHECKING FOR VALUES AND STORING NUMBERS 
trace("b",display_txt.text);   
if(isNaN(num1)){ 
    //CONVERT STRING TO NUMBER 
    num1=Number(display_txt.text); 
    operate = operatorEntered; 
    display_txt.text = ""; 
    //trace(num1,isNaN(num1)); // if this outputs some number and true, display_txt an   html enabled textfield, has kerning enabled or is a multiline textfield. fix that. 
} 
else if(isNaN(num2)){ 
    num2 = Number(display_txt.text); 
    //trace(num1,isNaN(num2)); // if this outputs some number and true,  display_txt an html enabled textfield, has kerning enabled or is a multiline textfield. fix that. 
    performCalculation(); 
    operate = operatorEntered; 
} 
} 


//CLEARS DISPLAY AREA 
function clearFields(event:MouseEvent):void{ 
display_txt.text = ""; 
num1=NaN; 
num2=NaN; 
} 

//ADDS DECIMAL PLACE 
function addDot(event:MouseEvent):void{ 
if(event.target.num == Number(display_txt.text)) 
{ 
display_txt.text = "0"; 
} 
if (display_txt.text.indexOf(".")==-1){ 
    display_txt.appendText("."); 
} 

} 
//BACKSPACE DISPLAY AREA 
function backSpace(e:MouseEvent):void{ 
var temp_str:String = display_txt.text; 
display_txt.text = temp_str.substr(0, (temp_str.length-1)); // Get rid of last  character in the string, which in this case is the phantom \r or \n character 
} 

function performCalculation():void{ 

switch (operate){ 
case "multiply": 
    num1*=num2; 
break; 
case "divide": 
    num1/=num2; 
break; 
case "subtract": 
    num1-=num2; 
break; 
case "add": 
    num1+=num2; 
break; 
    default: 
break; 
} 
//now that we found out the result 
//let's display on the window 
display_txt.text=String(num1); 
num2=NaN; 

} 

它似乎並沒有被執行計算並在下面所示的代碼,它的目的是要調用函數來執行計算,但它不會似乎正在這樣做,當我試圖跟蹤num1它輸出爲NaN當它是爲了顯示最後按下按鈕,當我點擊一個操作符。

function pressOperator(event:MouseEvent):void{ 
var operatorEntered:String; 
trace("a",display_txt.text,display_txt.text.length,event.currentTarget.caption_op.t ext,event.currentTarget.caption_op.text.length); 
display_txt.appendText(event.currentTarget.caption_op.text); 
//CHECKING FOR VALUES AND STORING NUMBERS 
trace("b",display_txt.text);   
if(isNaN(num1)){ 
    //CONVERT STRING TO NUMBER 
    num1=Number(display_txt.text); 
    operate = operatorEntered; 
    display_txt.text = ""; 
    //trace(num1,isNaN(num1)); // if this outputs some number and true, display_txt an  html enabled textfield, has kerning enabled or is a multiline textfield. fix that. 
} 
else if(isNaN(num2)){ 
    num2 = Number(display_txt.text); 
    //trace(num1,isNaN(num2)); // if this outputs some number and true,  display_txt an html enabled textfield, has kerning enabled or is a multiline textfield. fix that. 
    performCalculation(); 
    operate = operatorEntered; 
} 
} 

我認爲這是導致NaN的,因爲沒有任何事情到瓦爾的操作,但是operatorEntered它們都進入對方。我將如何解決?

+0

對於'String-> Number'的轉換,你需要'parseInt(str_num)'或'parseFloat(str_num)'方法,而不是'Number(str_num)'。 – Engineer 2012-03-16 20:25:02

回答

1

這可能不會直接回答你的問題,但我真的建議你看看如何在「純粹的」ActionScript 3項目中編寫文檔類。它不僅會迫使你「收緊」你的代碼,如果只是略微。但你也可以使用一個適當的IDE(即免費的FlashDevelop)和更好的調試功能,這將幫助你更容易地找到你的問題。

此外,如果您發佈了您希望其他人查看的代碼,請確保其格式良好且易於閱讀(至少縮進並最好帶有註釋)。在這種情況下,它會利用FLA文件中的內容,因此我們不能只複製您的代碼並將其粘貼到我們的Flash中。 所有這些因素讓你真的很難幫助你。