Author Topic: FlxU.overlap calls kill method, but where and how?  (Read 836 times)

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Hi everyone,

This is my first post. I just have got Flixel up and running and it's fantastic (obviously!).
I just have a question about FlixelInvaders.

Here's a piece of code from it:

Code: [Select]
//This is how we do basic sprite collisions in flixel!
//We compare one array of objects against another, and then if any of them overlap
// flixel calls their 'kill' method, which by default sets the object to not exist (!exists)
FlxU.overlap(_shields,_vsShields,overlapped);
FlxU.overlap(_playerBullets, _aliens);
FlxU.overlap(_alienBullets, _player);

The comments say that FlxU.overlap calls the kill method. But I don't understand where or how it does that.
I understand that overlap returns a boolean, which I don't see would help calling kill. And the bottom two lines do not use the callback function.


Can anyone shed some light on this?

Thanks!
Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart

cai

  • Contributor
  • ****
  • Posts: 465
  • Karma: +0/-0
  • the illest of villains
    • View Profile
    • Brandon Cash
Re: FlxU.overlap calls kill method, but where and how?
« Reply #1 on: Sat, Sep 4, 2010 »
It actually does it in FlxQuadTree.as:
Code: [Select]
if(_oc == null)
{
_o.kill();
co.kill();
c = true;
}
_oc is the callback function which you pass to FlxU.overlap (or in this case, don't), which is then passed to FlxQuadTree.overlap(), which is then checked by FlxQuadTree.overlapNode(); if it's null, kill() is called on both.

Why does it do this?  Well, it's a good default for a lot of games... say you shoot a bullet at an enemy: both the bullet and the enemy should then go away.  Obviously, it doesn't work in other situations very well: Mario goes to jump for a coin, but both he and the coin are killed.  Whoops!

Anyways, it's obviously a good idea to provide a callback, as chances are good you don't always want them destroyed.
Follow me on Twitter | Come join us at #flixel on irc.freenode.net!

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Re: FlxU.overlap calls kill method, but where and how?
« Reply #2 on: Sun, Sep 5, 2010 »
Thanks!
This greatly improves my understanding of what Flixel is doing.

Mods, consider this thread solved!
Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart