2012-03-08 39 views
0

我只是得到了编码的窍门,这在很大程度上归功于这个网站......但我还是noob,并有另一个noob问题。Flash Builder组件和循环

我创建了一个简单的计算器,我的孩子可以用它来处理他们的数学技能,但是我不知道如何循环它以便它会去10次。我理解for-else和until循环的语法,但不知道要在循环中放置什么。

做到这一点(我认为)最简单的方法可能是使用组件或类(不知道使用哪一个)然后调用它。

这就是说,这里是我的基本代码:

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> 

<fx:Script> 
    <![CDATA[ 
     import spark.components.FormItem; 
     import spark.components.TextInput; 

     public var n_1:Number = Math.round(Math.random() * 100) 
     public var n_2:Number = Math.round(Math.random() * 100) 
     public var ttl:Number 
     public var answer:Number 
     public var n_right:int 
     public var n_total:int = 1 


     protected function check_bn_clickHandler(event:MouseEvent):void 
     { 
      n1.text = n_1.toString(); 
      n2.text = n_2.toString(); 
      ttl = Number(total.text); 
      answer = n_1 + n_2 
     if(ttl == answer) { 
      yes_no_lbl.text = "YES, click NEXT!"; 
      n_right = n_right + 1 


     } else { 
      yes_no_lbl.text = "NO, try again!"; 

     } 

     } 
     protected function next_bn_clickHandler(event:MouseEvent):void 
     { 
      // reset variables 
      n_1 = 0 
      n_2 = 0 
      ttl = 0 
    // use component here???? 

     } 

    ]]> 
</fx:Script> 



<fx:Declarations> 

    <!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 


<s:Form id="Calc" x="28" y="27"> 
    <s:FormItem label="1st #"> 
     <s:Label id="n1" text = "{n_1.toString()}"/> 
    </s:FormItem> 
    <s:FormItem label="2nd#"> 
     <s:Label id="n2" text = "{n_2.toString()}"/> 
    </s:FormItem> 
    <s:FormItem label="Total="> 
     <s:TextInput id="total" prompt="Total"/> 
    </s:FormItem> 
</s:Form> 
<s:Button id="check_bn" x="273" y="124" label="Check" click="check_bn_clickHandler(event)"/> 
<s:Button id="next_bn" x="377" y="123" label="Next" click="next_bn_clickHandler(event)"/> 
<s:Form id="display_correct" x="649" y="27"> 
    <s:FormItem label="# Correct"> 
     <s:Label id="lbl_ratio" text="{n_right}/{n_total} "/> 
    </s:FormItem> 
</s:Form> 
<s:Label id="yes_no_lbl" x="462" y="83" width="184" height="61"/> 
</s:Application> 

那么其中有多少是我放的组成部分,多少钱,我留在应用程序?或者是否有像“重绘”这样的功能,可以再次运行该应用程序,但只保留长期变量(n_right等)?再次

谢谢!

回答

1

你需要(至少:)两两件事:

1)当你,你需要生成两个新的随机数(萨姆刚才指出的那样)生成你的下一个问题

2)让变量包含你的随机数[Bindable]。否则,您的文本框将不会刷新其新值。 (只写[Bindable]每个var在其声明之前)

[Bindable] public var n_1:Number = Math.round(Math.random() * 100) 
[Bindable] public var n_2:Number = Math.round(Math.random() * 100) 
+0

嗯...好吧,你是对的我需要这样做,但无法弄清楚语法。在声明中,例如:'public var ttl:Number'在哪里添加'[Bindable]'?我尝试了很多地方,它总是让我犯错。谢谢! – nebmuzik 2012-03-08 15:17:18

+0

[Bindable] public var n_1:Number ..... – Eduardo 2012-03-08 15:31:30

+0

编辑答案的文本以显示 – Eduardo 2012-03-08 15:32:18

1

似乎所有你想要做的就是产生一个新的随机问题?

protected function next_bn_clickHandler(event:MouseEvent):void 
{ 
    if (yes_no_lbl.text == "YES, click NEXT!") 
    { 
     // reset variables 
     n_1 = Math.round(Math.random() * 100); 
     n_2 = Math.round(Math.random() * 100); 

     total.text = ""; 
     yes_no_lbl.text = ""; 
     n_total++;  
    } 
} 

这产生了两个新的随机数,清除yes_no_lbl,清除total和增量n_total。我添加了一个逻辑检查,确保问题在提出新问题之前得到了正确答案。

你也应该让你n_1/n_2变量[Bindable],因为这将确保标签更新,以显示新的值时,它的变化。

+0

我喜欢你做了什么存在,但它仍然给我相同的数字。看下面的答案来帮助解决这个问题。谢谢......因为这是一个非常简单的解决方案! (一个我可以包裹我的noob头。) – nebmuzik 2012-03-08 15:16:48