2 Replies Latest reply: Apr 20, 2012 11:50 AM by kkiilleerr1997 RSS

    How to import variables from timeline to a class?

    kkiilleerr1997 Community Member

      I'm pretty noobish to ActionScript 2.0, so I'm still reading an Ebook. But I wanna make a game where you can get skill points, and then use them on a buttons, and when you have pressed the button, your character gets 3+ speed. I can use the skill points and stuff, but I can't add the 3+ speed to my character, so I made a variable in frame 1 that's named "speedupgrade", and that variable gets 3+ if you press the button. And I made a variable in frame 3 called "speed1". And in my character, I made a variable "speed", and it was speed1+speedupgrade. I can show you guys the code.

       

      On my button

       

      on (release) {

       

       

                if (skill >= 1)

                {

                          skill -= 1;

                          speedupgrade += 10;

                }

                else

                {

                          gotoAndStop(22);

                }

      }

       

      frame 1

       

      var skill:Number = 1;

      var speedupgrade:Number = 1;

       

      frame 3


      var speed1:Number = 7;


       

      My character

       

      var speed:Number = speed1 + speedupgrade


       

      I can't see the problem, because I'm not very experienced with this...

        • 1. Re: How to import variables from timeline to a class?
          Ned Murphy CommunityMVP

          If you are expecting

           

          var speed:Number = speed1 + speedupgrade

           

          to automatically update every time the values of the variables change, that will not happen.  If the variables change you need to execute the code again so that the new values get used.

           

          If you are just learning AS2, you should get away from a bad practice right away.  Do not place code "on" any objects, such as what you are doing with that button.  Give your button an instance  name and use it to assign code to it from the timeline so that you have all your code in one easy to find location.  If you named your button "btn", then the timeline code for it would be...

           

          btn.onRelease = function(){

                 if (skill >= 1) {

                           skill -= 1;

                           speedupgrade += 10;

                  } else  {

                           gotoAndStop(22);

                  }

          }

           

          and if you have multiple button with the same code you could just create the function and assign it to the buttons ...

           

          btn1.onRelease = btnClick;

          btn2.onRelease = btnClick;

           

          function btnClick(){

                 if (skill >= 1) {

                           skill -= 1;

                           speedupgrade += 10;

                  } else  {

                           gotoAndStop(22);

                  }

          }

          • 2. Re: How to import variables from timeline to a class?
            kkiilleerr1997 Community Member

            Thanks for the answer, I've rewriten the code a little. Now I update the "speed" directly. So is there a way to make my character get the variables from the timeline? Like my code for moving is

             

            if (Key.isDown(Key.RIGHT))

                      {

                                _x += speed;

                      }

             

                      if (Key.isDown(Key.LEFT))

                      {

                                _x -= speed;

             

            ,and my speed variable is in frame 1 with 7 as number, and gets added 10 in frame 2, character is in frame 3. So is there a way for my character to get the speed variable?

             

            Thank you

             

            I figured out the root thing.. So the question is answered.