Reputation: 125
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.
Upvotes: 3
Views: 432
Reputation: 438
I know this is probably a little bit late, but there are a few things you can try:
You could try using FlxSpriteGroup
for Player
instead of FlxTypedGroup
. 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
and collide
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 your Player
object extends FlxTypedGroup<PlayerPart>
(if PlayerPart
extends FlxSprite
or something), when you do FlxG.overlap(weapons, players, onPickup)
, overlap is NOT going to pass the Player
object, it's going to pass the PlayerPart
object that overlapped - in fact, it's going to call onPickup
once for EVERY PlayerPart
object that overlaps a weapon - possibly the same one - this update
.
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!
Upvotes: 2