3

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.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Jonathan Bro
  • 77
  • 1
  • 7

1 Answers1

2

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!

Gama11
  • 31,714
  • 9
  • 78
  • 100
SeiferTim
  • 438
  • 4
  • 18
  • You are right on the FlxTypedGroup Resolution of members. But the OP tries to group his players into 'p;ayers'. Can you make a group of FlxSpriteGroups, and run an overlap on the parent group, like the OP tried to ? – yannicuLar May 18 '15 at 22:55
  • And still, a FlxSpriteGroup will only work for simple transitions. If you upscale or rotate the group, the members positions and hitboxes will mess up. There are know issues on that: https://github.com/HaxeFlixel/flixel/issues/975 – yannicuLar May 18 '15 at 23:09
  • If his `Player` Object extends `FlxTypedGroup` - like he says it does, you would NOT be able to use `overlap` or `collide` they way he has it now. There are a lot of ways to 'solve' the problem. Using `FlxSpriteGroup` might be the simplest solution for him, if he's not worried about complex transitions - it might not work, depending on the situation. – SeiferTim May 19 '15 at 16:03
  • Yes, you're right on that. FlxTypedGroup will not add itself into the quadtree. Only it's members. My question is : But even if he made Player extend FlxSpriteGroup, would he be able to add his Players into another group (players) and pass the `players` to `Flx.overlap` ? – yannicuLar May 19 '15 at 17:55
  • I'm asking because I think that his logic (making a group that contains groups), is wrong in the first place. My impression is that you can pass either a Group of Sprites to Flx.overlap, or a single Group. Not a group that contains Groups. I hope I've made myself more clear now. btw can you please edit your post so I can upvote it? – yannicuLar May 19 '15 at 18:00
  • You can **absolutely** pass groups of groups to `FlxG.overlap` - you just have to understand what you would be getting back in the overlap callback. – SeiferTim May 19 '15 at 18:45