3 Replies Latest reply on Jan 26, 2007 7:46 AM by Peter Lorent

    Passing value from one frame to another?

    aspidistra
      HI
      I am trying to use actionscript to calculate an amount and pass it from keyframe (1) to another keyframe (15) where the total is supposed to be carried over and displayed. Any ideas why this doesn't work?

      Also should I be using screens or scenes to make this any easier? - I am not sure.

      thanks
      Emma


      ON FRAME ONE:

      //Calculates total price.
      two_btn.onPress = function() {
      priceTotal_txt.text = Number (price1_txt.text) + Number (price2_txt.text) + Number (price3_txt.text) + Number (price4_txt.text);
      var MyTotal:Number = priceTotal_txt.text;
      _root.gotoAndStop (15);
      }

      ON FRAME 15:

      this.onEnterFrame = function (){
      var MyTotal:Number = priceTotal_txt.text;}
        • 1. Re: Passing value from one frame to another?
          Peter Lorent Level 2
          Number of things wrong here.

          1. if you declare var MyTotal within the body of a function it is considered a local variable and automatically gets deleted after the function has executed. Meaning, you loose it.
          The var needs to be declared on the timeline (and is called a timeline variable).

          var MyTotal:Number=0;

          //Calculates total price.
          two_btn.onPress = function() {
          priceTotal_txt.text = Number (price1_txt.text) + Number (price2_txt.text) + Number (price3_txt.text) + Number (price4_txt.text);
          MyTotal = priceTotal_txt.text;
          _root.gotoAndStop (15);
          }

          2. onEnterFrame is a misleading name for the event. You will probably include a stop() action on frame 15 and then the function you assign to the event handler gets executed n number of times per second where n is the framerate of the movie. You don't want that.

          on frame 15:
          trace(MyTotal); // I'm not sure what you want to do here...

          3. Convert a string to a number by using parseInt()

          var MyTotal:Number=0;

          //Calculates total price.
          two_btn.onPress = function() {
          priceTotal_txt.text = Number (price1_txt.text) + Number (price2_txt.text) + Number (price3_txt.text) + Number (price4_txt.text);
          MyTotal = parseInt(priceTotal_txt.text);
          _root.gotoAndStop (15);
          }
          • 2. Re: Passing value from one frame to another?
            aspidistra Level 1
            LuigiL
            1. thanks for clearing that up about the where the variable should be declared...i had tried it outwith the function but of course without the rest it still wouldn't work!

            2. all i wanted to happen here on frame 15 was the totals from the previous screen (frame1) to appear - they now work!

            priceTotal_txt.text = Number(MyTotal)
            mobno_txt.text = Number(MobTot)

            3. A question!
            Can you explain why this had to be parsed then the three totals are data typed as 'Number'? ....don't understand why.

            thanks
            Emma
            • 3. Re: Passing value from one frame to another?
              Peter Lorent Level 2
              If you don't use parseInt() MyTotal will hold a string value. Use this simple script on your frame 15:
              if(typeof(MyTotal)=="number"){
              trace("True");
              }
              Try without using parseInt() and you won't get a trace. Change the condition to test to:
              if(typeof(MyTotal)=="string"){
              trace("True");
              }
              and you will get a trace when you don't use parseInt()