-
1. Re: Global Variables in Flash CS4 (AS3) ?
kglad Mar 15, 2010 2:57 PM (in response to Me2LoveIt2)you can create a class that allows you define pseudo-global variables:
package{
public class glo{
public static var bal:Object = {};
}
}
then import the glo class wherever needed and assign:
glo.bal.whatever=whatevervalue;
and retrieve
glo.bal.whatever
wherever else it's needed
-
2. Re: Global Variables in Flash CS4 (AS3) ?
Me2LoveIt2 Mar 16, 2010 10:11 AM (in response to kglad)Thank you for the answer!
I am however still having problems.
It's giving me this error: 1037:Packages cannot be nested
I tried to google it couldnt make out the problem though. Any Ideas?
Here is my code:
//This is on frame 1, scene 1. package{ public class glo{ public static var widthOfSlider:Number = 5; } } //This part is in Frame 1 of scene 1 > my_mc. var numOfCovers = 6; var temp = glo.widthOfSlider.Number; this.addEventListener(Event.ENTER_FRAME, centerB); function centerB(event:Event):void{ for(var i = 0; i < numOfCovers ; i++ ){ //This loop lines up the buttons inside my_mc this["cover_btn_"+String(i)].y = 0; this["cover_btn_"+String(i)].x = temp; temp += this["cover_btn_"+String(i)].width + 5; } glo.widthOfSlider.Number = temp; this.removeEventListener(Event.ENTER_FRAME, centerB); } //I am trying to get the new width of my_mc . (so i can make my_mc.width = glo.widthOfSlider.Number [because it does not dynamically adjust])Thank you!
-
3. Re: Global Variables in Flash CS4 (AS3) ?
kglad Mar 16, 2010 10:16 AM (in response to Me2LoveIt2)your code should be removed. the code i suggested should be in a file named glo.as
-
4. Re: Global Variables in Flash CS4 (AS3) ?
Me2LoveIt2 Mar 16, 2010 10:25 AM (in response to kglad)Sorry for the inconvenience, but could you elaborate a bit?
Thanks.
-
5. Re: Global Variables in Flash CS4 (AS3) ?
kglad Mar 16, 2010 10:32 AM (in response to Me2LoveIt2)package{
public class glo{
public static var bal:Object = {};
}
}
should be in a class file named glo.as
click file/new/actionscript file. paste the above code and save that file as glo.as
then in any class where you want to assign a global variable use:
import glo;
glo.bal.widthOfSlider = 5;
and in any class where you want to retrieve that variable, use:
import glo;
trace(glo.bal.widthOfSlider);
-
6. Re: Global Variables in Flash CS4 (AS3) ?
Me2LoveIt2 Mar 16, 2010 12:39 PM (in response to kglad)Thank you this is what i needed I got it to work now!
But I do still have one last question on this topic.
This is the Code:
//this is in a file Actionscript File called glo.as package{ public class glo{ public static var widthOfSlider:Number = 5; } } // This code is in scene 1 > my_mc import glo; trace(glo.widthOfSlider); //This code is in scene 1 > my_mc > myNext_mc import glo; var numOfCovers = 6; var temp = glo.widthOfSlider; trace(glo.widthOfSlider); this.addEventListener(Event.ENTER_FRAME, centerB); function centerB(event:Event):void{ for(var i = 0; i < numOfCovers ; i++ ){ this["cover_btn_"+String(i)].y = 0; this["cover_btn_"+String(i)].x = temp; temp += this["cover_btn_"+String(i)].width + 5; } glo.widthOfSlider = temp; trace(glo.widthOfSlider); this.removeEventListener(Event.ENTER_FRAME, centerB); }When I run this I get the following output:
5
5
5
725.45
725.45
The first "5" is from the trace(glo.widthOfSlider); in scene 1 > my_mc
The next two "5"'s are from the trace(glo.widthOfSlider); in scene 1 > my_mc > myNext_mc
Is there a way so that I can first execute the code in scene 1 > my_mc > myNext_mc before the code in scene 1 > my_mc ?
I am trying to get 725.45 through the trace(glo.widthOfSlider); in scene 1 > my_mc
-
7. Re: Global Variables in Flash CS4 (AS3) ?
kglad Mar 16, 2010 12:56 PM (in response to Me2LoveIt2)you can't change the order in which code is executed in flash. code in nested movieclips always executes after code in parent movieclips.
but you can always put code in a function body and call that function whenever you want. so, while the code that defines a frame 1 parent function will always execute before code in frame 1 of a child, you can call that function much later, if you want.
p.s. you're still missing an important part of the glo class: using that one class (the way i defined it) you can define an unlimited number of pseudo-global variables without touching the glo class itself.
-
8. Re: Global Variables in Flash CS4 (AS3) ?
Me2LoveIt2 Mar 16, 2010 1:29 PM (in response to kglad)Thank you again this it rally great help and I really do appreciate it!
This is the working code:
//this is in a file Actionscript File called glo.as package{ public class glo{ public static var bal:Object = {}; } } // This code is in scene 1 > my_mc FRAME 2 import glo; trace(glo.bal.widthOfSlider); // This code is in scene 1 > my_mc FRAME 3 stop(); //This code is in scene 1 > my_mc > myNext_mc FRAME 1 import glo; var numOfCovers = 6; var temp = 5; this.addEventListener(Event.ENTER_FRAME, centerB); function centerB(event:Event):void{ for(var i = 0; i < numOfCovers ; i++ ){ this["cover_btn_"+String(i)].y = 0; this["cover_btn_"+String(i)].x = temp; temp += this["cover_btn_"+String(i)].width + 5; } glo.bal.widthOfSlider = temp; this.removeEventListener(Event.ENTER_FRAME, centerB); }Thanks!
-
9. Re: Global Variables in Flash CS4 (AS3) ?
kglad Mar 16, 2010 1:54 PM (in response to Me2LoveIt2)you're welcome.


