Skip to main content
GogetaX
Inspiring
April 8, 2019
Answered

Using vars from different frames?

  • April 8, 2019
  • 1 reply
  • 1052 views

Hey everyone, im still trying to understand how exactly i supposed to use flash vars.

so bear with me, i will try my best to explain the situation.

i have a custom class i made, where it stores vars inside:

package {

public class PHPClass extends EventDispatcher {

var stats = {HP:Number, MP: Number, Nickname:String};

public function PHPClass() {

//constructor code

}

}

}

now, to use this class, for some reason, i need to create it,

so here i am at frame number 1 at my code, i do

var PHP:PHPClass = new PHPClass;

now what this allows me to do, when i write "PHP" then adding a dot (.) after that, i see the hint box what vars should i be using.

so far so good?

now, when i switch to frame 2 at my flash game, i no longer can use the hint box anymore but the command is still usable,

so like PHP.stats.HP = 20.0; still working, but i have no hint box to use anymore.

is there any way i can declare a var inside my 2nd frame so it will be using the same var i created at 1st frame?

because something like:

(on 2nd frame)

var NewPHP:PHP; does not work..

i hope i explained the situation clear enough, if no please tell me ill provide an example code.

This topic has been closed for replies.
Correct answer ASWC

You can put code in frames but you really shouldn't. There are tons of reasons why you shouldn't and what you are experiencing is just one of them. The correct way: Write your classes inside .as files (class files) and link your project to them so you can use them anywhere in your project.

Anyway .... Create a layer without keyframes that spans the entire timeline, all code (and classes) you put there will be accessible in the other layer's keyframes.

1 reply

ASWCCorrect answer
Inspiring
April 8, 2019

You can put code in frames but you really shouldn't. There are tons of reasons why you shouldn't and what you are experiencing is just one of them. The correct way: Write your classes inside .as files (class files) and link your project to them so you can use them anywhere in your project.

Anyway .... Create a layer without keyframes that spans the entire timeline, all code (and classes) you put there will be accessible in the other layer's keyframes.

GogetaX
GogetaXAuthor
Inspiring
April 8, 2019

yeah i know, but here is another problem with this that i need an answer also.

  1. package { 
  2. public class PHPClass extends EventDispatcher { 
  3.  
  4. var stats = {HP:Number, MP: Number, Nickname:String}; 
  5. public function PHPClass() { 
  6. //constructor code 

how can i access this class from any frame of my code?

each frame, i have to "Create" the class once again using

var PHP:PHPClass = new PHPClass;

but that messes up my saved variables i made inside that class.

any other way to access the class from multiply frames?

Inspiring
April 8, 2019

I told you, create a layer WITHOUT keyframes (must be a layer as long as the entire timeline) and put your code in there, now you can access that code from anywhere in THAT timeline.