References classes? - actionscript-3

what's the correct way to reference a class, in this case from the same package. the code below shows class "Two" attempting to trace the value of a variable in class "One". no success. trace returns undefined.
//Class One
package
{
import flash.display.Sprite;
public class One extends Sprite
{
public var myString:String = "My String";
public function One()
{
new Two();
}
}
}
//Class TWO
package
{
public class Two
{
private var oneClass:Class = One; // <- is that right?
public function Two()
{
trace(oneClass.myString);
}
}
}

There is an error in trace(oneClass.myString); because myString isn't static. But besides that, you can assign One to oneClass
A reference of type Class doesn't have any property of the class or the object, is not an instance is just a reference to the constructor of that class. i.e:
public class One {
static public var classValue = "Hello";
public var instanceValue = "World!";
}
public class Two {
public function Two() {
trace( One.classValue ); // this traces: Hello
trace( One.instanceValue ); // this throws an Error
var classReference:Class = One;
trace( classReference.classValue ); // this throws an Error
trace( classReference.instanceValue ); // this throws an Error
var objectReference:One = new classReference();
trace( objectReference.classValue ); // this throws an Error
trace( objectReference.instanceValue); // this traces: World!
}
}
The class properties (static ones) only can be acceded from the Class directly (like One.classValue) not from a Class reference and instance properties (NOT static ones) can only be acceded from objects of that class (like new One().instanceValue)

myString in class One is not static. So it doesn't belong to the class, but to the instances. That's why your code doesn't work. Try with :
static public var myString:String = "My String";

Related

AS3 - Error #1009: Cannot access a property or method of a null object reference

I have no idea what is causing the error. I'm sure its very obvious.
(See comments below).
package src.main{
import src.tilespack.Tile;
public class Main{
public var tile:Tile;
public function Main{
var t:Tile = Tile.tiles[0]; //Error here
trace(Tile); //This will also cause an error
}
Tile:
package src.tilespack{
public static var tiles:Array = [];
public static var floorTile:Tile = new FloorTile(0); //Causes an error in FloorTile Class
public var bitmapData:BitmapData;
public function Tile(bitmapData:BitmapData, ID:int)
{
this.ID = ID;
tiles[ID] = this;
this.bitmapData = bitmapData;
}
}
FloorTile:
package src.tilespack{
import src.gfx.Assets;
import src.tilespack.Tile;
public class FloorTile extends Tile{ //Error here
public function FloorTile(ID:int){
super(Assets.floorTileData, ID);
}
}
}
Error #1009: Cannot access a property or method of a null object
reference.
I've noticed few problems in your code:
1) The file Tile doesn't have a definition of class. Probably, this is just a typo, but anyway, this file should look something like:
package src.tilespack
{
public class Tile
{
public static var tiles:Array = [];
public static var floorTile:Tile = new FloorTile(0); //Causes an error in FloorTile Class
public var ID:int;
public var bitmapData:BitmapData;
public function Tile(bitmapData:BitmapData, ID:int)
{
this.ID = ID;
tiles[ID] = this;
this.bitmapData = bitmapData;
}
}
}
2) Your code has something like "recursive linkage" (sorry, I don't know the official term). Your class Tile has the static variable floorTile, and it tries to create an instance of the FloorTile class, which itself extends the class Tile.
So we have a situation, when the class Tile tries to use the class FloorTile (because static variables should be instantiated during the first class use), and the class FloorTile tries to use the class Tile. And it's a problem.
You may either remove static variable of the type FloorTile or change code of the class Tile to prevent usage of the class FloorTile before the class Tile is prepared to work. Here an example of the second approach:
package src.tilespack
{
import flash.display.BitmapData;
public class Tile
{
public static var tiles:Array = [];
private static var _floorTile:Tile;
public static function get floorTile():Tile
{
if (!Tile._floorTile)
{
Tile._floorTile = new FloorTile(0);
}
return Tile._floorTile;
}
public var ID:int;
public var bitmapData:BitmapData;
public function Tile(bitmapData:BitmapData, ID:int)
{
this.ID = ID;
tiles[ID] = this;
this.bitmapData = bitmapData;
}
}
}

access public var of an instance from a different object in actionscript3

how do you access property from one class instance to another, assuming both MCButtonA and MCButtonB instances are defined in the main class. This is without using static var.
I keep on getting:
1120: Access of undefined property varA
package {
import flash.display.MovieClip;
public class MCButtonA extends MovieClip {
public var varA:String = "abc";
public function MCButtonA() {
// constructor code
}
}
}
package {
import flash.display.MovieClip;
public class MCButtonB extends MovieClip {
public var varB:String = "abc";
public function MCButtonB() {
// constructor code
trace( ?????varA)
}
}
}
Main class:
var aButton:MCButtonA = new MCButtonA();
var bButton:MCButtonB = new MCButtonB();
trace(this.parent.aButton.varA);
upd
will work only after both buttons are added to stage

AS3 undefined function #1006

I have a parent class called 'main.as'. I am trying to get the child class to call main's function. They both reside in the same folder.
// main.as //
package {
public class main extends MovieClip {
public function main() {
var child:child_mc = new child_mc(this);
}
public function callFunction():void {
trace("Done it");
}
}
}
.
// child.as //
package {
import main;
public class child extends MovieClip {
private var main:MovieClip = new MovieClip();
public function child(main:MovieClip):void {
this.main = main;
main.callFunction();
}
}
}
This is the error I've been getting:
TypeError: Error #1006: callFunction is not a function.
so I tried doing a trace like this
trace(main.callFunction);
and it says undefined. Can someone tell me what I am missing. I get this feeling its a very basic thing that I have overlooked!
Your "child" package is defined as "main". I'm not even sure how it complied, let alone run to the point of showing the error message you got.
I believe the code below should do what you expected.
(I also took the liberty to rename the classes to use CamelCase (with initial caps) to adhere to best practices and to be easier to distinguish from variable names.)
Main.as
package {
public class Main extends MovieClip {
public function Main() {
var child:ChildMC = new ChildMC();
child.main = this;
}
public function callFunction():void {
trace("Done it");
}
}
}
EDIT: I just saw your comment that points out that child_mc is a MovieClip in the Library. I guess then that the child class is set as the Base Class of the child_mc?
If so, you cannot pass properties through the instantiator, you need to find another way to pass along the instance of the Main class to the Child class.
One way would be to add a setter, like the following:
Child.as (Base Class for ChildMC)
package {
public class Child extends MovieClip {
private var _main:Main;
public function Child() {
}
public function set main(main:Main):void {
this._main = main;
this._main.callFunction();
}
}
}

Problem with create object when know object's class name

I'm tring to create instance of class, when I got name of this class.
I think better to explain my problem will be this code:
package
{
import flash.utils.getDefinitionByName;
public class SomeClass extends ParentClass
{
[Embed(source='../assets/gfx/levelImg/level0.png')]
public static const Level0Img:Class;
public function someFunction():void
{
var imgString:String = "Level0Img";
var imgClass:Class = getDefinitionByName(imgString) as Class;
}
}
I invoke someFunction, and I get error: [Fault] exception, information=ReferenceError: Error #1065: Variable Level0Img was not defined.
What can be wrong with this ?
}
You are declaring a nested Class. The definition can't be found by the name you provided.
Try this:
(...)
public class SomeClass extends ParentClass
{
[Embed(source='../assets/gfx/levelImg/level0.png')]
public static const Level0Img:Class;
public function someFunction():void
{
var imgString:String = "SomeClass_Level0Img";
var imgClass:Class = getDefinitionByName(imgString) as Class;
}
(...)
why don't you just write var imgClass:Class = Level0Img;?That's better than classname guessing...

when does the stage become initialised?

I have a singleton class that inherits from sprite so that it can access the stage, like this..
package
{
import flash.display.Sprite;
public class C extends Sprite
{
private var _grid:Array = new Array();
public function get Grid():Array
{
return _grid;
}
private static var _instance:C;
public static function get Instance():C
{
if (_instance == null)
{
_instance = new C();
}
return _instance;
}
function C()
{
this.InitGrid();
}
private function InitGrid():void
{
var gridWidth:Number = stage.width / 10;
}
}
}
This throws the error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at C/InitGrid()
at C()
at C$/get Instance()
at C()
at Main()
If I replace stage.width with an int the code executes OK.
is this because the object has not been added to the displayList of any children of the stage?
Thanks
Yes. The Sprite will only have a stage property once it's a part of the Display list.
To get the stage you will need to either give your singleton a reference to the stage or add it to the Display list. If you choose the latter you can add a listener Event.ADDED_TO_STAGE, and handle that accordingly inside your singleton.