ActionScript 3.0 :: What Is Static Variable
Apr 13, 2011What is static variable. what is difference between static variable and public variable?
View 2 RepliesWhat is static variable. what is difference between static variable and public variable?
View 2 RepliesThe Idea is i want to make avariable that stores his value after i close the swf and open ti again,
an example:
i make acounter
var i:int = 0;
var timer:Timer = new Timer(1000);
timer.start();
[Code]....
the value of i will increase but after i close the application and open it again the value will return to 0 , keep the variable stores his value after closing the application ?
I need to keep my "grid" + [o] value from 1-9 but add to the to the gridArray number (hence currentNo) within this double loop.
[Code].....
How can I replace
public function Mainclass()
{
Calculations.getDistance(5,9,4,2);
}
[Code]....
I'm not lazy (lying? :p), but I use the class more than 20 times, and it looks better if I replace al these long classnames.
I have a class named Locator that I use to GeoLocate a user. There's a static method in that class called "locate()". This method then calls another method which sets a static variable called "loc" to an instance of a LngLat class.The issue I'm having is, when I call the locate method at Frame 1 on the scene, and then try to access the loc variable it shows as null... but when I trace the loc variable after it's set in the Locator class it shows as an object.[code]and then trace the variable at the end of the static method in the Locator class it shows as a object and the city is traced as expected.
View 1 RepliesIt is possible to call variables using this[ "stringname" ] for instance, to refer to a variable called "stringname".
However, when referencing a static variable by the name "stringname", the "this" attribute no longer works if applied.
Is it possible to use this technique with statics?
I've been trying to create a script that pulls the current time and turns it into a variable, the problem is the variable keeps updating as the time changes, where as I need it to remain static as the time that is was when the variable was first created...it's just a simple:
Code:
var theTime = today.getTime();
In the following class
Code:
package
{
public class Factory
{
[Code]]....
A break point on the "Okay" line shows that x1 is null but x2 is equal to "<entity id="DRONE" points="100"/>".
If I take the whole block out of the switch statement, both x1 and x2 are equal to the expected xml.
I'm trying to change a static variable in the class's constructor. At the start I have:
public static var mainReference:Main;
public static var timerReference:Timer;
public var timer:Timer = new Timer(1000);
This is so my static functions can access main and timer. At Main's constructor I have:
mainReference = this;
timerReference = timer;
The problem is, the first gives no error when I compile it, but the second tells me Access of undefined property (timerReference).
Is there any way that you can have dynamic variables inside of a static function considering that you can't use "this" inside a dynamic function.
What I am trying to do:
public static function convertToDynamicString(pString:String):String
{
if(pString == "" || pString == null) return "";
[Code]....
I've rarely used static variables or methods when creating external classes, but I have on occasion, and it's very useful when I need global access to a value. I've seen negative criticism on the use of the "static" keyword, but never understood why. Is there a right and wrong way of using it, or is this not true? I'd like to use it in a context similar to classes such as MouseEvent or TextFieldAutoSize, where there is a list of values globally accessible for identification purposes.
It allows better code readability, and also allows for easy customization in the future. The value of TextFieldAutoSize.NONE is "none", but as long as a programmer used the reference TextFieldAutoSize.NONE and not the string literal "none", Adobe could change the value of TextFieldAutoSize.NONE to be "hahahahaha" and everything would still work just dandy because (TextFieldAutoSize.NONE == TextFieldAutoSize.NONE) is still true even though ("none" == "hahahahaha") is not.
Is there any kind of negative impact on using static variables this way? Or even functions for that matter? I'd just like to clarify this before I go and build a large project around the idea this is perfectly okay.
How can I access a timeline variable from a static class / singleton?
View 14 RepliesI have an actionscript class with a static member variable defined.
public class A
{
public static var x:int;
}
When I try to access it from different parts in my code I don't get the same value in each spot.
A.x
I am accessing the variable in different modules that are loaded, so they are all in their own separate .swf file.
I am attempting to create cash register simulation game and am running into a problem with a conditional statement that I am using to check whether the correct change is being provided to the customer and if a button is being pressed or not. In this game, the user can see the amount of the sale as well as the amount the customer pays. With this information they have to click on the cash drawer slots which will increase a variable called changeCount by the value of the monetary unit they have selected.
I am using an if statement to take them to the next frame if the changeCount = payment-sale and the confirm button has been pressed. To check for the button I am using a boolean to see if it is being pressed or not. For some reason, it is not registering the updated value of the changeCount for the if statement to work. If I trace the value with trace(changeCount); it will return the value I want.
The source code is pasted below:
package {
public class CashierMain extends MovieClip {
private var confirmBtn:ConfirmButton;
private var pennyBtn:PennyButton;
private var dimeBtn:DimeButton;
[Code] .....
Basically I have 2 classes, A and B. B derives from A. Inside A, I declare a static variable/constant to the class of B. When I run the program, I get a null object reference error, with the line pointing to the class declaration of B, i.e.
Code:
public class B extends A
For reference, these are the 2 classes:
Code:
package
{
public class A
{
[code]...
Also, this error only occurs when the variable is static. If it's just a normal variable it works fine, i.e.
Code:
private var OTHER:Class = B;
If I change the static variable to point to a different class, it's also fine, i.e.
Code:
private static var OTHER:Class = Main;
I can probably work around this, but it's a bit of a pain in the ass. Anybody know what's causing it, or is this a Flash bug?
In the following code (thoroughly debugged) the first case (1) in the switch is met and a new instance of BannerSingle is assigned to retVal (the return value). Unfortunately, while the second case (2) is never met, the assignment therein to retVal takes place and I can't compile. When I comment out the assignment to retVal for "case 2:" everything works fine.
I've tried using an if/else instead and I still get the same odd behavior. Also tried a number of solutions involving separate variables, etc.
package fl {
import fl.IBanner;
import fl.Banner;
[Code].....
I have 3 swfs, and let's label them MovieA, MovieB, and MovieC. MovieB is a child of MovieA, and MovieC is a child of MovieB. So the hierarchy goes like this: MovieA -> MovieB -> MovieC. Each of these movies have their own document class, so they are each essentially a class extending MovieClip.
Now I have this other external class which contains only static variables. Let's call this class "TestClass" and let's say one of its static variables is called "testVar". So in MovieB's document class, I imported TestClass and set testVar = "hello world". In MovieC's document class, I imported TestClass and traced TestClass.testVar. When I publish and run MovieB, testVar is successfully traced as "hello world". Now when I publish and run MovieA, testVar is traced as undefined.
I'm currently working on a particle system for a game that I recently started development on, but I've hit a snag: I have a class particlecontainer that tells each particle what to do on enterframe, and which contains each particle as a child. I also have a class (mischandler) which has a static array that contains all current particles, as well as another static array that is a buffer to remove particles from the particlecontainer. Whenever a particle "dies", it calls a function which adds it to the buffer array in mischandler. However, when I try to access this buffer array from the particlecontainer class it does not recognize it as having any elements. If this is unclear here's a simpler explanation:
particlecontainer tells a particle to do it's enterframe function particle calls die() and adds itself to the removebuffer inside mischandler particle container tries to access mischandler.removebuffer but removebuffer has no elements Strangely, the particle can access the removebuffer perfectly fine and sees all the elements in it.
how bad is this practice? I am having trouble getting to some stuff so i am taking the easy way out
Code:
private static var _interrupted:Boolean;
public static function setInterrupted(value:Boolean):void{
_interrupted = value;
}
I want to use the Event metadata tags to show what types of event my control will dispatch. The syntax looks like:
<fx:Metadata>
[Event(name="eventName", type="MyEvent")]
</fx:Metadata>
It seems like a best practice in Flex/Actionscript to define static variables that define event names like so:
public class MyEvent extends Event
{
public static const EVENT_NAME:String = "eventName";
// Other stuff..
}
It's a great practice since the event name can change easily and not have to be modified throughout the code. So my question is: Is there any way to use this static const in my metadata event tag? I can't seem to do something like this:
<fx:Metadata>
[Event(name="{MyEvent.EVENT_NAME}", type="MyEvent")]
</fx:Metadata>
Am I just ignorant of the proper syntax to do this, or is it impossible? Seems like it's just asking for hard to find bugs if someone decides to change the const since it is not strongly typed here.
I realize this is kind of an odd issue, but I am wondering if there's any way to get Flash to allow me to access a class's static functions using a class variable that points to the class. Example:I create a class called FooClass that has a static function named fooI then create a variable of type Class that points to it
Code:
var class:Class = Class(getDefinitionByName("FooClass"));
However, when I try to call foo() using the variable, it errors saying the function
[code].....
Let's say you have the following situation:
1. Static var(an Array) stored in a class.
2. You create a variable reference to the static class in a separate class.
3. Splice some items from the reference variable in the separate class.
4. Trace the static class and the items have been removed from that too.
5. Verify several times.
6. Get confused.
7. Post on Kirupa.
Ok, so I have a bit of a mess here (Which I'm probably not doing right anyway, which could be my problem...). I have a document class, I'll call it as Body.as, that creates an object from another class known as Headgear.as. There are also other objects created from other classes, or will be in the future. Now, amoung all this, Headgear.as and all of the other classes Body.as will use to make objects require the use of dragging functions (starting and stopping dragging). On stopping drags, position checks are then made to compare a particular hidden movieclip with the dragged MC.
To try and save myself some hassle (from making the same functions over and over to ensuring that when the objects are created I don't need to send a complete crapload of variables just to ensure the position checking function would work), I had the functions for the event listeners (Which are set-up on the draggable MCs in Headgear.as) just direct to functions in Body.as
[Code]...
Trying to replace the number on the following call with a variable... How could i do it? Have to remove 144 of them... rather do a loop for it...
Code:
_root.mc_movies_area.mc_area_54.mc_54.removeMovieClip();
I have 2 classes, Display holds the currently selected Component:
public class Display
{
public static var selectedComponent:Component;
}
Component has an ID string and the selectedComponent variable is set on click:
public class Component extends MovieClip
{
public var id:String;
addEventListener(MouseEvent.CLICK, function() {
[Code]...
Removing the selectedComponent variable type so it reads public static var selectedComponent; removes the conversion error and seems to change the ID variable but it appears to only be a copy of the object.
If I created a static method. say I decide to call on other methods within that static method. Do those methods I call on need to be static as well? what If I used some of the properties. Not to store data permanently, but just within that process. Do those properties need to be static ??
View 3 RepliesYesterday I found myself wondering, what is the difference between either having a class with methods that are all static, or simply giving your class the static attribute?
View 9 RepliesAlright, so I have a class that is linked (via the linkage panel) to a scrollbox class. I'll paste the class here:[code]Ignoring the formatting, the commented out functions are the ones causing the issue. Adobe says that it's a static class and I can't use non-static functions. The way I wanted to use it was:
1. Call the page button generating function above.
2. In the main code in my program is this line:scrollbox.setClickFunction(historyContent.generate Page);So when the buttons that are supposed to be generated are clicked, the scrollbox class can call the History pages generatePage function and pass it which page to show. (I did this because there was a lot of text and a limit on how much would display, so small chunks sounded logical).
3. In the onClick function (which I haven't finished yet because the rest wont work), when you click one of the buttons it calls the set function. Its that simple.
I'm kind off oblivious as to what I'm doing wrong here... I have two classes: - Alley - AlleyCat
[Code]...
Basically, what I mean is I i have few layers of classes, that calls and change something on the 1st(root) class. In between the process I have to use static var and static function.But when I want to run a function which does not change any variables, for example:otoAndStop(someFrames), I need to make the function a static function too or else there would be error
Code:
1061: Call to a possibly undefined method checkStatus through a reference with static type Class.
[code]....