I want the player to pickup objects in my game using the code below, but I am getting:
Error #1034: Type Coercion failed: cannot convert flixel::FlxSprite@51e1b69 to Player.
...
FlxG.overlap(weapons, players, onPickup)
}
private function onPickup(wep:Weapon, player:Player):Void
{
//access player function
}
I've initialized the players and weapons already as below, and added to the group
players= new FlxTypedGroup<Player>();
weapons= new FlxTypedGroup<Weapon>();
Weapon
extends FlxSprite
and Player
extends FlxTypedGroup<FlxSprite>
.
I'm using FlxTypedGroup
because I want the player to have multiple sprites associated with it.
Please help so I can access the player class variables!
If I replace player:Player
with player:FlxSprite
there is no error, but then I can no longer access Player
class functions.
I know this is probably a little bit late, but there are a few things you can try:
You could try using
FlxSpriteGroup
forPlayer
instead ofFlxTypedGroup
. It may take some work to get it working the way you want.Also, the reason why it's giving you an error, is because
overlap
andcollide
will (by default) drill down through your Groups until it comes to an actual object...How to explain... If you have a
FlxTypedGroup<Player>
and yourPlayer
object extendsFlxTypedGroup<PlayerPart>
(ifPlayerPart
extendsFlxSprite
or something), when you doFlxG.overlap(weapons, players, onPickup)
, overlap is NOT going to pass thePlayer
object, it's going to pass thePlayerPart
object that overlapped - in fact, it's going to callonPickup
once for EVERYPlayerPart
object that overlaps a weapon - possibly the same one - thisupdate
.You can use this behavior to your advantage, if you can figure it out - make your Player group contain several PlayerParts but set all of them to
allowCollisions = NONE
except for one which will be your hitbox, etc.There's lots of things you can do, it's just figuring out the specifics. Good Luck!