Implicit coercion error with world.classFirst()


(Jacob) #1
trace(world.classCount(BlueBlock)); // output: 2
var block:BlueBlock = world.classFirst(BlueBlock); // Gives implicit coercion error

Error: Implicit coercion of a value with static type net.flashpunk:Entity to a possibly unrelated type blocks:BlueBlock.

BlueBlock is a subclass of Entity so I don’t understand why I’m getting the error.

Could someone please help?


(Jacob) #2

I believe I’ve figured it out.

I changed: var block:BlueBlock = world.classFirst(BlueBlock);

to: var block:BlueBlock = world.classFirst(BlueBlock) as BlueBlock;

Seems to work fine that way.


(TaylorAnderson) #3

It works because world.classFirst seems to return type Entity, which you then have to cast to BlueBlock before you assign it to an instance of class BlueBlock.