for example is it possible to do somehing like public var socket:flash.net.Socket = new flash.net.Socket();?
No you will still get a class undefined compiler error if you don't import flash.net.Socket
Related
I am trying to target a variable in the main .as file (The one that acts as the stage) from another .as file.
public var stageRef:MovieClip = root as MovieClip;
or
MovieClip(root)variable = 10;
don't seem to want to work for me. Neither of them produce any compile errors but when I try to use them they give me a 1009 error, cannot access a property or a null object reference. Any ideas of how i would go about doing this? Thanks in advance.
Im your Main.as class make the variable public. Here's an example:
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
public var YOUR_VAR_HERE:VARIABLE_TYPE = DEFAULT_VALUE;
public function Main()
{
}
}
}
DEFAULT_VALUE is optional. VARIABLE_TYPE is recommended, if not specified the type will be Object by default.
There are many ways to pass a variable to another class. If the class is created inside the Main class, just pass the variable to that class like this:
var myOtherClass:OtherClass = new OtherClass(YOUR_VAR_HERE);
or
var myOtherClass:OtherClass = new OtherClass();
myOtherClass.varReference = YOUR_VAR_HERE;
In first case make sure the constructor is expecting a variable. In the second, make sure the OtherClass has a public variable varReference that you can access and modify.
Another way loved by newbie programmers are static (singleton) variables: in the Main class specify your variable as such:
public static var YOUR_VAR_HERE:VARIABLE_TYPE = DEFAULT_VALUE;
Then you can access YOUR_VAR_HERE simply by referring to the class Main. Like this:
trace(Main.YOUR_VAR_HERE);
NOTE: it's considered to use all uppercase letters for constants, not variables, in this case I used all caps for readability.
Say I have a basic class "Code.as"
And I also have class "Code2.as" that extends "Code.as"
In "Code.as" I have the line "public static var code2:Code = new Code2();"
I keep getting this error:
"TypeError: Error #1009: Cannot access a property or method of a null object reference"
at "Code2.as"s class declaration.
Why? I can't figure it out.
If "Code2.as" extends "Code.as" I thought it would be alright to address it as a "Code" as well...
The correct line should be:
public static var code2:Code2 = new Code2();
Also make sure you import "Code2":
import Code2;
I´m trying to initialize a class, based on a concatenation of a string and a number.
All my classes are public.
This is my code:
public function setCurrentPath(pathNumber:String)
{
var pth_class:Class = getDefinitionByName('Pth'+pathNumber) as Class;
var pth:MovieClip = new pth_class();
addChild(pth)
pth.getXY();
}
So I´m getting Error #1065.
Any help?
Yes I have up on my class file import flash.utils.*
Is your pth_class variable null?
If so, there are a couple of reasons this might be the case:
1) You haven't input the correct fully qualified class name of your class. E.g com.myClasses.Pth1
or
2)
If you're instanciating classes dynamically like this and there is no other "regular" reference to the class (such as blah = new Pth1()) then the "Pth1" class won't be included in the compilation process.
To get around this I think you can supply arguments to the compiler to force it to compile those classes OR you can manually include references to them in your existing code:
p1:Pth1;
p2:Pth2;
I'm having a problem in flex 3 where if a static const I have defined is used as the parameter to a function call within binding tags I get a "1120: Access of undefined property NodePropertyMatrix". _propMtx is a ArrayCollection.
<mx:HBox visible="{_propMtx.getItemAt(NodePropertyMatrix.srcParent)}">
Above code throws the error, but the following code does not
<mx:HBox visible="{NodePropertyMatrix.srcParent}">
NodePropertyMatrix is an AS class as follows:
package model.constants
{
import mx.collections.ArrayCollection;
public class NodePropertyMatrix
{
public static const srcParent:Number = 0;
}
}
Anyone know what is wrong here?
Found the problem.
In the mxml file where I was importing the NodePropertyMatrix is was doing this:
import Constants.*;
Instead of this:
import Constants.NodePropertyMatrix;
For some reason it doesn't work in this instance without explicity importing that class. Wildcard didn't do the trick....not sure why, but ignorance is bliss.
I'm trying to place a class into a package where another public class is placed. The documentation says that only one external visible declaration can be put in a package.
So i declare the second class internal. But then it gives the following error:
5006: An ActionScript file can not have more than one externally visible definition: character.AnimatedCharacterClass, character.CharacterPositions
The code I use is:
internal class CharacterPositions
{
public static const BEGIN_WALK:String = 'begin_walk';
public static const END_WALK:String = 'end_walk';
public static const STAND:String = 'stand';
}
Does anyone have a clue what is happening here?
I found that i have to put the second class outside the package. It still confuses me though.