Flash Builder变量

时间:2012-03-07 19:24:28

标签: flash flash-builder

好的,我的智慧结束了。我是一个菜鸟,我明白了,自从我以任何诚意完成编程以来,已经有一段时间了。也就是说,我正试图重新回到它并且左右碰到问题。

我最大的问题与分配变量有关。作为一个侧面项目,我正在尝试使用FlashBuilder创建一个简单的计算器。我知道它没有完成,它可能有多个问题。所以任何帮助都非常感谢!

到目前为止,这是我的代码。什么。上午。 I.失踪???

<?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 mx.events.FlexEvent;
        import spark.components.FormItem
        import spark.components.TextArea;
        import spark.components.TextInput;

        public var n_1:Number
        public var n_2:Number
        public var ttl:Number = 0


    protected function added():void
    {
    ttl = n1 + n2;
    total.text = String(ttl);

    }
    ]]>
</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 id="n1" label="1st #">
        <s:TextInput prompt="Number"/>
    </s:FormItem>
    <s:FormItem id="n2" label="2nd#">
        <s:TextInput prompt="Number"/>
    </s:FormItem>
    <s:FormItem label="Total=">
        <s:Label id="total"/>
    </s:FormItem>
</s:Form>
<s:Button id="add_bn" x="377" y="83" label="+" click="added()"/>
<s:Button id="minus_bn" x="377" y="123" label="-"/>
</s:Application>

1 个答案:

答案 0 :(得分:0)

您正在使用其他表单项添加表单项,因此不会收到数字(NaN)。

您需要的是TextInput文本值Number

您也可以使用数据绑定,但要使用您的示例:

<?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;
            public var n_2:Number;
            public var ttl:Number = 0;

            protected function add_bn_clickHandler(event:MouseEvent):void
            {
                // get numbers from text field
                n_1 = Number(n1.text);
                n_2 = Number(n2.text);

                // total:
                ttl = n_1 + n_2;

                total.text = ttl.toString();
            }

            protected function minus_bn_clickHandler(event:MouseEvent):void
            {
                // get numbers from text field
                n_1 = Number(n1.text);
                n_2 = Number(n2.text);

                // total:
                ttl = n_1 - n_2;

                total.text = ttl.toString();
            }
        ]]>
    </fx:Script>


    <s:Form id="Calc"
            x="28"
            y="27">
        <s:FormItem label="1st #">
            <s:TextInput id="n1"
                         prompt="Number" />
        </s:FormItem>
        <s:FormItem label="2nd#">
            <s:TextInput id="n2"
                         prompt="Number" />
        </s:FormItem>
        <s:FormItem label="Total=">
            <s:Label id="total" />
        </s:FormItem>
    </s:Form>
    <s:Button id="add_bn"
              x="377"
              y="83"
              label="+"
              click="add_bn_clickHandler(event)" />
    <s:Button id="minus_bn"
              x="377"
              y="123"
              label="-"
              click="minus_bn_clickHandler(event)" />
</s:Application>