Bitmap object lacking inherited properties?

Hello there,

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.

Why is this? What might I be doing wrong here?

Using html5 target btw.

did you use current version of openfl (via “haxelib git”?)

As explained here, using Dynamic makes getters and setters fail. Problem is, DisplayObject just loves its getters and setters:

https://github.com/openfl/openfl/blob/develop/openfl/display/DisplayObject.hx#L50-L69

Don’t worry, there’s an easy solution. Once you tell the compiler that you’re dealing with a Bitmap, it will know how to access transform:

var bitmap:Bitmap = thisEntity.bitmap;
matrix = bitmap.transform.matrix;

Wow.

Thank you, your suggestion of using a temporary typed variable solved my problem!

But… this behavior of Haxe is just insanely counterintuitive, and hance error prone, especially when migrating from as3…

Anyway, thank you for the hint!

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.

Yes, if I’d do a project from scratch in Haxe, that’d probably be the best approach. Thank you for your help!