Author Topic: Noob Question - Overlap() and FlxGroups  (Read 1693 times)

Kewp

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Noob Question - Overlap() and FlxGroups
« on: Wed, Feb 18, 2015 »
I've been having some trouble getting my head around Flixel, in particular when trying to overlap my player with a FlxGroup. I'm trying to get checkInteraction() to trace "It works!" whenever my player is overlapping anything inside the interactableGroup flxgroup when I press the spacebar.

Quote
public var interactableGroup:FlxGroup;
private var player:Player;
private var char2:Interactable;

override public function create():void
      {
        interactableGroup = new FlxGroup();
        interactableGroup.add(player);
        interactableGroup.add(char2);

        add(player);
        add(char2);
      }


private function checkInteraction(player:FlxObject, interactableGroup:FlxObject):void
      {
         if (FlxG.keys.justPressed("SPACE"))
         {
            Trace("It works!");
         }
      }

This code works, but I would expect interactableGroup:FlxObject):void to be interactableGroup:FlxGroup):void. The later throws an error. Googling yielded results I could not understand. Could somebody explain why interactableGroup needs to be a FlxObject and not a FlxGroup?

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Noob Question - Overlap() and FlxGroups
« Reply #1 on: Wed, Feb 18, 2015 »
When you overlap with a group, it doesn't actually overlap with the group object itself, it recursively goes through and overlaps with everything inside the group. So your player is doing an overlap check against the player and char2, since those are in the group (the group doesn't have any sense of a collision rectangle, only the objects inside it do). If you put a group inside a group, it'll recursively keep going into the members of each and overlap all the objects. The parameters to that callback are the actual objects it overlapped with, and not the group (so you can tell whether it was player or char2 that was overlapped).

dca

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Noob Question - Overlap() and FlxGroups
« Reply #2 on: Mon, Feb 23, 2015 »
On a side note, you don't need to add each object once it's in a group.  You can simply add the group.
Code: [Select]
override public function create():void
      {
        interactableGroup = new FlxGroup();
        interactableGroup.add(player);
        interactableGroup.add(char2);

        add(interactableGroup);
      }

Kewp

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Noob Question - Overlap() and FlxGroups
« Reply #3 on: Thu, Mar 12, 2015 »
Gotcha, thanks guys!