ActionScript 3.0 :: Accessing Static Variables Using The Class Name Itself
Jun 18, 2009
Say I have a class called Ball (movieclip) and I create a number of instances of it. There is a static variable, say, ballArray and need to access that from a different class, say, Bat. How do I go through with it? Can I access that using the class name itself (Ball.ballArray like in Java)?
I'm trying access a static variable located of a super class through an instance of one of it's subclasses and I'm receiving an access of undefined property error (example of which below). Creating another static variable in the subclass and assigning the value from the super class will allow me to dot operate to it.
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
I'm working on a small networked game (server is written in C# ready to go). I've completed all of the networking classes utilizing xmlsockets (socket handling, packets etc). My issue is once you get to the login screen and attempt to authorize a packet is sent depending if your login has been accepted so if it has you delete the login movieclip and show the game movieclip but I can't access it from my static class (it was created on the main stage).
I'm trying to make methods available like so: ActionScript Code: MyClass.myFunc(); Instead of ActionScript Code: var myVar:MyClass = new MyClass(); myVar.myFunc();
I know that to do so I need to declare the functions/variables/constants as static: ActionScript Code: package MyPackage { public class MyClass extends Object { public static function myFunc():void { } public function MyClass() { [Code] .....
When I try to access the static function through the full reference (i.e trace(MyPackage.MyClass.myFunc);), it works, while trace(MyClass.myFunc); returns undefined. This contradicts what I've read in tutorials and posts on the Internet. They all use the short syntax, not the full path.
First off I don't understand classes, how to "call" or "initiate" them. I'm class ignorant.
I have two .fla files. One of my .fla files consist of 15+ .as files; we'll call this one XML editor. The other .fla file consists of 10+ .as files; we'll call it the interface.
The xmleditor.swf loads the interface.swf. Within the xmleditor.swf, a login screen appears and the enduser logs in as either a "user" or an "admin". The "user" or "admin" is stored in a public variable called "userType". The userType variable is created in one of the many xmleditor.fla .as files called Login.as.
Once logged in, xmleditor loads the interface.swf. interface.fla uses 10+ .as files. one is called nodeNames.as I need an if statement in nodeNames.as that is something like this:
if (Login.userType == "user"){ trace("do something"); }
I have the following FlashVars.as file but I have no idea what the steps are to make it work.
I add the sprite to the stage and define it's x and y in the class file. How would I go about changing the x and y of the container within my fla file?
this works, but what If I want to change a variable declared in the button?
I would like to retrieve from an XML the movie I want to load pressing this particular button, but seems like the scope of the "designed" objects and the programmed object is a little different, since with
[Code]....
I've included a simplified version of the interface, the classes and the code is not 100% functional...
I am hoping someone can explain about subclasses accessing variables from the super class. I found that the subclass can only access variables which are set in the constructor of the super class. Is there any way around this?
Currently I have two classes which are "bullet" and "enemy".There are two instances of the enemy class.Code from an ENTER_FRAME event in the Bullet Class:
The Enemy Class has a private variable called "hits".What do I have to do in order to reduce the hits of the instance of the enemy that was hit by the bullet?
I was wondering if there is a simple way to access variables from a class object, where the variables are not actually a part of that class. So if you had an array defined on the first frame of the stage, and added some elements to it, you could use the methods of the custom class to manipulate the array's data.
Stage Frame 1 Code: var myArray:Array = new Array( "apple", "banana", "pear" ); var myClass:someFunctions = new someFunctions(); myClass.addValue( "orange" ); myClass.sortValues(); [Code] .....
It might not seem logical as to why you would ever want to do that, but it's a very simple example, and it's the means I'm after, not the result. I'm creating a list of functions available to execute by a user via an input field. To keep things simple I made them the methods of a class. The issue, however, is that these methods need to manipulate objects which are on the stage, added and removed by a separate controller class. Passing the objects by reference to the class as a parameter would not be viable with what I'm trying to do either.
Alright, 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 have two classes , and i need to call static function from one class to the other. also when i call this static function i need access the local variable of that class. Class Test2: Quote:
[Code]...
when i run these class i get the following error Test2.as(18): col: 10 Error: Access of undefined property testVar. this function works if i change [testVar] to a static variable , is there any other way to make this function work other than making the [testVar] variable to static?
I'm wondering if anyone has experience with if there is a big difference in performance in ActionScript 3 between keeping a class with only public static functions, and utilizing those functions often (as in a frame event at 30fps), and in turning the class into a "normal" class of which I instead make an instance and call the functions via the instance instead.
A reference to the prototype object of a class or function object. The prototype property is automatically created and attached to any class or function object that you create.This property is static in that it is specific to the class or function that you create. For example, if you create a class, the value of the prototype property is shared by all instances of the class and is accessible only as a class property.Instances of your class cannot directly access the prototype property. A class's prototype object is a special instance of that class that provides a mechanism for sharing state across all instances of a class. At run time, when a property is not found on a class instance, the delegate, which is the class prototype object, is checked for that property. If the prototype object does not contain the property, the process continues with the prototype object's delegate checking in consecutively higher levels in the hierarchy until Flash Player or the Adobe Integrated Runtime finds the property.
Note: In ActionScript 3.0, prototype inheritance is not the primary mechanism for inheritance. Class inheritance, which drives the inheritance of fixed properties in class definitions, is the primary inheritance mechanism in ActionScript 3.0.
from this I get the impression that prototypes are just static variables.. am I right?
I have created a class to define my main navigation buttons. I would like to handle which button is clicked/pressed as simply and elegantly as possible, and I figured the best way would be to add a static variable to the class that keeps track of the current button pressed. Here's some quick pseudo-code. Code: class MainButton ext MC static var currentButton:MainButton public var sectionToLoad:String public static function getCurrentButton() public function getSection() button.onPress = currentButton = this // other button actions. // then i can check for what the currently clicked button // is and now access its variables. MainButton.getCurrentButton().getSection() etc. etc. etc. I am curious if this is a "proper" way to go about tracking the buttons. I find it is pretty elegant, but i'd rather make sure that this is a good practice of using static class variables. Also, it looks to me that all custom class definitions become a _global object?
I'm a .net developer making a small website on Flash. I noticed that when I initialize a static variable during runtime in one place I cannot get its new value at another place, why?
I've always used this way to refer to variables on my document class Code: MovieClip(root).variablename But that complicates things because I would need to extend my class as a displayObject and then add it to the displaylist to be able to use "MovieClip(root)" to access the variables in my document class.. as long as variables in my document class are set out as public variables..
I didn't realise you could refer to variables on the document class by just using something like: Code: documentclassname.variablename With the the variable on the document class as "public static var". This solves a lot of problems.. as now I do not need to keep adding objects to the displaylist to get access to the document class variables..
Now.. My question is..Since I want to access most of my variables on my document class.. I would need to set them all to "public static var".. Would setting 100 or so variables to "public static var".. have any disadvantages? Like performance issues... Or anything else which may make it not be as efficent if I were to keep them simply as "public var" and use "MovieClip(root).variable" to access them?
Got some external classes, say MyClass.as MyClass.as has a static variable called foo
So, ordinarily in other AS files I can call this with MyClass.foo = bar;
However, this seems to be different in timeline scripts. Every time I try this I get the reference with a static type error 1119: Access of possibly undefined property foo through a reference with static type flash.display:DisplayObjectContainer.
I've tried doing an import MyClass, etc... nothing seems to be firing.
I'm literally new in AS3 and slowly trying to migrate in thats why im here seeking for help. Can someone help me how to access/control a certain public variable in a loaded swf from the main swf? this problem has been paralyzing me for days. (sigh...) [code]...
When I set a breakpoint and debug my application, Flash Builder 4 is not displaying static variables within the variables window. I'm using flash builder 4 to execute flex unit tests on one of my AS3 classes. I set a static variable within the [Before] function, which is accessed in each of the tests. I've set a breakpoint within one of the tests to see why it is failing, but I notice that static variables don't appear when I expand the 'this' object within the variables window. (In this case my static variable is the only variable associated with the class, so the only object in the variables window is the "this" object). How to make static variables appear in the variables window?
pros and cons of either using a Singleton that enforces only one instantiation of itself, versus creating one common class with static vars which can be accessed by all other classes that import it?
In my testing, importing but never instantiating works fine. I can even change the value of one of the variables and then have another class see that changed value later on in the program.
If I *know* I will never actually create an instance of my Global class using the New command (but will instead just reference the variables using the class's name), then why should I need to make it a strict Singleton?
I get this error 1120: Access of undefined property OtherClass.
Ths is the code: Code: public class SomeClass extends MovieClip { function SomeClass():void { } public function SomeFunction():void { var i:int = OtherClass.myVar; trace(i); }}
Code: public class OtherClass { public static var myVar:int = 2; public function OtherClass():void { }}