Skip navigation
kkiilleerr1997
Currently Being Moderated

How to import variables from timeline to a class?

Apr 18, 2012 10:31 PM

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...

 
Replies
  • Currently Being Moderated
    Apr 19, 2012 4:32 AM   in reply to kkiilleerr1997

    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);

            }

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points