I’m having a bit of trouble using Bitmap objects as I would in actionscript.
What I’m doing is, I’m having a dynamic object, one of which’s property I assigned a Bitmap object.
Then later, when I am trying to access said object’s transform property, like such:
matrix = thisEntity.bitmap.transform.matrix;
I get the error message, that thisEntity.bitmap.transform is undefined.
Investigating the issue I found that the reference thisEntity.bitmap does only have the properties specific to the Bitmap class, none of those inherited by DisplayObject.
Yeah, using Dynamic is discouraged. If at all possible, declare your variables with the correct type.
class Entity {
public var bitmap:Bitmap;
}
class Main {
public var thisEntity:Entity;
}
If thisEntity could one of any number of classes, I’d suggest making an interface:
interface IEntity {
public var bitmap:Bitmap;
}
class Entity implements IEntity {
public var bitmap:Bitmap;
}
class OtherEntity implements IEntity {
public var bitmap:Bitmap;
}
class Main {
public var thisEntity:IEntity;
}
But what if you couldn’t change the Entity and OtherEntity classes? Instead, you just want to tell the compiler "thisEntity is an object with a Bitmap." What you need in that case is a typedef:
typedef SomethingWithBitmap = {
var bitmap:Bitmap;
}
class Main {
public var thisEntity:SomethingWithBitmap;
}
In all three of these examples, you can access thisEntity.bitmap.transform.matrix, since the compiler will know what bitmap is. Note that the first option is the fastest, and the third option is the least safe.