Problem with create object when know object's class name - actionscript-3

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...

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;
}
}
}

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();
}
}
}

Pass class as constructor's argument

I'm trying to pass my object class as constructor argument. I have something like this:
package myclass {
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
public class Example extends MovieClip {
public var _stageImg:Bitmap;
public var _stageData:BitmapData;
public var _stageClass:Class;
private var _stage:Object;
public function Example(stageClass:Class) {
_stageClass = stageClass;
}
public function createStage():Object {
_stageData = new _stageClass(0,0);
_stageImg = new Bitmap(_stageData);
_stage = addChild(_stageImg);
return _stage;
}
}
}
Now, when I try to create my Example:
import myclass.Example;
var example:Example = new Example(myObjClass);
I get message, that I'm not passing any variable (Error #1063). Why is that? What is wrong with it?
You are passing an instance of your class instead of the definition of the class itself.
In ActionScript it's a bit clunky, but you can get the class definition of an instance like this:
//first get the fully qualified classname, i.e. including the package
var className:String = getQualifiedClassName(myObjInstance);
//then get the class definition for that name
var Klass:Class = getDefinitionByName(className) as Class;
//and finally pass it to your constructor
var example:Example = new Example(Klass);
(note: I named the variable 'Klass' because 'Class' is a reserved keyword)

I have a var in as3 and I want to get its name in a string

Example:
public var myVar:Object;
// now I want to get the myVar string
// myVar is not instanced*
public var whatINeedIsThisVar:String = 'myVar';
thanks
There is no way to do that, and when you are working in release mode (i.e. not debuuging a swf) local variable name didn't exist anymore.
But if it's a public field name of a class you can use describeType to found the name.
package {
import flash.utils.describeType;
class Foo extends Sprite {
public var bar:Object;
function Foo(){
super();
// trace all public field name in this class
for each (variable:XML in describeType(this).variable) {
trace("field name : ", variable.#name);
}
}
}
}

References classes?

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";