2 Replies Latest reply on Oct 19, 2010 10:02 AM by ignacio_nieto

    calculator donth work

    ignacio_nieto Level 1

      hi im triyng to do a calculator with flex. the calculator rest a number from "puntajeInicial" and put it text field called "marcador"

       

      marcador.text= (puntajeInicial-elResultado).toString();

       

      i use a evento code to capture the keybiard press and asigne a value to it

       

      every thing goes fine until I try to work with more than two numbers onley one input keyboard event  is recognize by the as3 code

       

      could any be so kind to help me

       

      thanks in advance

       

      ------------------------------------------------------------------------------>

      here is the code

       

       

      <?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.controls.Alert;
                  import mx.events.FlexEvent;
                  import mx.core.FlexGlobals;
                  import flash.events.KeyboardEvent;
                 
                  private var puntajeInicial:Number =0;
                  private var paga:Number=0;
                 
                  private var numeroEntrado1:String;
                  private var numeroEntrado2:String;// --> agruega un numero
                  private var elResultado:Number;// --> rusultado final
                 
                 
                 
                 
                  public function puntaje(event:MouseEvent):void
                  {
                      this.addEventListener(KeyboardEvent.KEY_DOWN, sienteTecla);
                     
                      // suma total
                      if (grupo1.selectedValue=="Arroz")    { puntajeInicial=800; }
                      if (grupo1.selectedValue=="Pure")     { puntajeInicial=400; }
                      if (grupo2.selectedValue=="Ensalada") { puntajeInicial= puntajeInicial+500; }
                      if (grupo2.selectedValue=="PapasFritas") { puntajeInicial= puntajeInicial+600; }
                      if (grupo3.selectedValue=="Pollo") { puntajeInicial= puntajeInicial+550; }
                      if (grupo3.selectedValue=="Pescado") { puntajeInicial= puntajeInicial+450; }
                     
                      // falta seleccionar opciones
                      if (grupo1.selection==null || grupo2.selection==null || grupo3.selection==null )
                      {     Alert.show("Falta Seleccionar opciones"); }
                     
                      //agruega al marcador caja de texto el total y lo convierte en texto
                      marcador.text= "Tu Plato es: " + numeroFormato.format(puntajeInicial).toString()+ "pesos";
                     
                  }
                  
         
                 
             
                 
              private function sienteTecla(event:KeyboardEvent):void {
                  if (marcador.text!="") {
                  switch(event.charCode){
                      case "1".charCodeAt() : {
                 
                          numeroEntrado1 = "1";
                          //Alert.show("uno");
                         
                          break;
                      }
                      case "2".charCodeAt() : {
                         
                          numeroEntrado2 = "2";
                         
                          //Alert.show("dos");
                          break;
                      }

       

                  }
                  /*
                  if (event.keyCode == 48 ) {
                              numeroEntrado = 0 ;
                   //Alert.show("hay");   
                          }
                          if (event.keyCode == 49 ) { numeroEntrado = 1;  }
                          if (event.keyCode == 50) {     numeroEntrado = 2; }
                          if (event.keyCode == 51) { numeroEntrado = 3; }       
                  */   
             
              }
              }
                 
                  public function resta(event:MouseEvent):void
                  {
                      elResultado = Number(numeroEntrado1.toString()) +  Number(numeroEntrado2.toString());
                      
                      //agrga al marcador caja de texto el total y lo convierte en texto
                      marcador.text= (puntajeInicial-elResultado).toString();
                      //marcador.text= (numeroEntrado-puntajeInicial).toString();
                  }
                 
                 
              ]]>
          </fx:Script>
          <fx:Declarations>
             
              <s:RadioButtonGroup id="grupo1"/>
              <s:RadioButtonGroup id="grupo2"/>
              <s:RadioButtonGroup id="grupo3"/>
              <mx:NumberFormatter id="numeroFormato"
                                  precision="0"
                                  rounding="nearest"/>
              <fx:XMLList id="platos">
                  <plato>
                      <platos>Arroz</platos>
                      <precio>800 pesos</precio>
                      <cant>50 porciones</cant>
                      <active>true</active>
                      <image>images/arrow_icon_sm.png</image>
                  </plato>
                  <plato>
                      <platos>Pure</platos>
                      <precio>400 pesos</precio>
                      <cant>55 porciones</cant>
                      <active>true</active>
                  </plato>
                  <plato>
                      <platos>Ensalada</platos>
                      <precio>500 pesos</precio>
                      <cant>47 porciones</cant>
                      <active>false</active>
                  </plato>
                  <plato>
                      <platos>Papas fritas</platos>
                      <precio>600 pesos</precio>
                      <cant>50 porciones</cant>
                      <active>true</active>
                  </plato>
                  <plato>
                      <platos>Pollo</platos>
                      <precio>550 pesos</precio>
                      <cant>35 porciones</cant>
                      <active>true</active>
                  </plato>
                  <plato>
                      <platos>Pescado</platos>
                      <precio>450 pesos</precio>
                      <cant>35 porciones</cant>
                      <active>true</active>
                  </plato>
              </fx:XMLList>
          </fx:Declarations>
          <s:Panel x="11" y="-4" width="803" height="401" title="COMIDA RAPIDA" fontWeight="normal" textDecoration="none">
              <!---
              A rectangle with a solid color fill that forms the background of the application.
              The color of the fill is set to the Application's backgroundColor property.
              -->
              <s:Rect id="backgroundRect" left="0" right="0" top="0" bottom="-35"  >
                  <s:fill>
                      <!--- @private -->
                      <s:SolidColor id="bgRectFill" color="#1E5C75"/>
                  </s:fill>
              </s:Rect>
             
              <s:Rect top="10" bottom="10" right="36" left="10">
                  <s:fill>
                      <s:LinearGradient>
                          <s:entries>
                              <s:GradientEntry color="#1E5C75"/>
                              <s:GradientEntry color="#6DB6C6"/>
                          </s:entries>
                      </s:LinearGradient>
                  </s:fill>
                  <s:filters>
                      <s:BlurFilter/>
                      <s:BevelFilter/>
                  </s:filters>
              </s:Rect>
             
              <s:HGroup x="36" y="223" width="699" height="126">
                  <s:VGroup width="158" height="126">
                      <s:Label text=" Entrada:" width="78" height="22" verticalAlign="middle" fontWeight="bold"  textAlign="center"/>
                      <s:RadioButton id="ArrozRadioBtn" label="Arroz" groupName="grupo1" textAlign="right" chromeColor="#9FF5F8"/>
                      <s:RadioButton id="PureRadioBtn" label="Pure" groupName="grupo1" chromeColor="#9FF5F8"/>
                  </s:VGroup>
                  <s:VGroup width="174" height="126">
                      <s:Label text="Acompañamiento:" fontWeight="bold"  height="22" verticalAlign="middle" width="129" textAlign="center" chromeColor="#0E0D0D"/>
                      <s:RadioButton id="multiRadioBtnA" label="Ensalada" groupName="grupo2" chromeColor="#9FF5F8"/>
                      <s:RadioButton id="multiRadioBtnB" label="PapasFritas" groupName="grupo2" chromeColor="#9FF5F8"/>
                  </s:VGroup>
                  <s:VGroup width="162" height="126">
                      <s:Label text="Carne:" fontWeight="bold"  height="21" verticalAlign="middle" textAlign="center" width="78"/>
                      <s:RadioButton id="trueRadioBtn3" label="Pollo" groupName="grupo3" chromeColor="#9FF5F8"/>
                      <s:RadioButton id="falseRadioBtn3" label="Pescado" groupName="grupo3" chromeColor="#9FF5F8"/>
                  </s:VGroup>
                  <s:VGroup width="255" height="135">
                      <s:Button id="marcadorBtn" label="Total" click="puntaje(event)" width="166" height="26" fontWeight="bold" fontSize="16" chromeColor="#F7C25B"/>
                      <s:Label  id="marcador" width="185" height="26" chromeColor="#F5F5FA" fontWeight="bold"/>
                      <s:Button click ="resta(event)" id="marcadorBtn0" label="Paga con:" width="166" height="26" fontWeight="bold" fontSize="16"  chromeColor="#F7C25B"/>
                      <s:TextInput width="187"  id="cacho"/>
                  </s:VGroup>
              </s:HGroup>
              <mx:DataGrid id="dg" x="36" y="25" width="505" height="190" rowCount="3" dataProvider="{platos}" variableRowHeight="true" chromeColor="#ECE242">
                  <mx:columns>
                      <mx:DataGridColumn headerText="Platos" dataField="platos" fontSize="15" fontWeight="bold" wordWrap="false"/>
                      <mx:DataGridColumn headerText="Precio" dataField="precio" fontSize="15" fontWeight="bold"/>
                      <mx:DataGridColumn headerText="Cant.Porciones" dataField="cant" fontSize="15" fontWeight="bold"/>
                  </mx:columns>
              </mx:DataGrid>
              <mx:Image x="582" y="60" width="101" height="112" source="file:/E:/4° Semestre DGM/Curso prfNieto/logo.jpg"/>
          </s:Panel>
      </s:Application>