2010-10-24 23 views
1

我要製作一個計數器來計算考試題的正確答案。 計數器只會從每個幀中收集正確答案。創建一個MCQs考試並且在Actionscript 3中的得分計數器出現錯誤

這裏是第一幀:

var counter:Number = 0; 
var correctAns:Number; 

correctAns = ans1_mc.alpha; 

function clicked1(event:MouseEvent):void 
{ 
ans1_mc.alpha = 1; 
ans2_mc.alpha = 0; 
ans3_mc.alpha = 0; 
ans4_mc.alpha = 0; 
} 
function clicked2(event:MouseEvent):void 
{ 
ans1_mc.alpha = 0; 
ans2_mc.alpha = 1; 
ans3_mc.alpha = 0; 
ans4_mc.alpha = 0; 
} 
function clicked3(event:MouseEvent):void 
{ 
ans1_mc.alpha = 0; 
ans2_mc.alpha = 0; 
ans3_mc.alpha = 1; 
ans4_mc.alpha = 0; 
} 
function clicked4(event:MouseEvent):void 
{ 
ans1_mc.alpha = 0; 
ans2_mc.alpha = 0; 
ans3_mc.alpha = 0; 
ans4_mc.alpha = 1; 
} 
function submit(event:MouseEvent):void 
{ 
if (correctAns == 1) 
{ 
    counter++; 
} 
else 
{ 
    counter = counter; 
} 
trace (counter); 
gotoAndStop(currentFrame + 1); 
} 


ans1_btn.addEventListener(MouseEvent.CLICK, clicked1); 
ans2_btn.addEventListener(MouseEvent.CLICK, clicked2); 
ans3_btn.addEventListener(MouseEvent.CLICK, clicked3); 
ans4_btn.addEventListener(MouseEvent.CLICK, clicked4); 

submit_btn.addEventListener(MouseEvent.CLICK, submit); 


ans1_mc.alpha = 0; 
ans2_mc.alpha = 0; 
ans3_mc.alpha = 0; 
ans4_mc.alpha = 0; 

text1_txt.text = "A"; 
text2_txt.text = "B"; 
text3_txt.text = "C"; 
text4_txt.text = "D"; 

stop(); 

第二幀是:

ans1_mc.alpha = 0; ans2_mc.alpha = 0; ans3_mc.alpha = 0; ans4_mc.alpha = 0;

correctAns = ans1_mc.alpha;

text1_txt.text = "E"; 
text2_txt.text = "F"; 
text3_txt.text = "G"; 
text4_txt.text = "H"; 

stop(); 

所有其他幀將作爲第二幀。

我想知道錯在哪裏。

回答

0

您的一般做法在這裏是錯誤的&您的遊戲的可擴展性不是很好。您需要對遊戲進行概述並嘗試提出更好的邏輯。

您可以在偵聽器中標識一個按鈕,因此不需要添加太多的事件偵聽器,就可以。識別被點擊的按鈕並相應地設置答案。

function clickHandler(event:MouseEvent):void 
{ 
     trace(event.currentTarget); 
    } 

如果你把所有的按鈕都放在一個數組中,它將很容易設置它們的alpha屬性。

var buttons:Array = [ans1_mc, ans2_mc, ans3_mc , ans4_mc]; 

function clickHandler(event:MouseEvent):void 
{ 
    //set all buttons alpha to 0 
    for each(var button:MovieClip in buttons) 
     button.alpha = 0; 

    //set the alpha of the clicked button to 1 
    event.currentTarget.alpha = 1; 

    //set the correct answer 
    if(event.currentTarget == ans1_mc) 
     correctAns = 1; 
    } 

我沒有測試你的代碼,所以我不知道如何錯誤有,在任何情況下,你應該設置correctAns變量的函數,像我上面那樣。

+0

非常感謝。我會嘗試你的代碼,並會回來給你一個反饋。 – 2010-10-25 03:44:19

+0

總的來說,這並不是我需要的東西,但這並不意味着你沒有幫助我,你做了一項很好的工作,幫助我用一種很好的方法實現了我的目標。非常感謝 – 2010-10-25 14:50:36

相關問題