HaxeFlixel Logo HaxeFlixel Snippets
  • About
  • Tutorials
  • Back to HaxeFlixel.com
  • Basics
    • Screen
    • Adding
  • Objects
    • Alive
    • Health
  • Sprites
    • Making Sprites
    • Loading Sprites
    • Animation
    • Alpha
    • Color
    • Facing
    • Scale
    • Baked Rotations
  • Text
    • FlxText
    • FlxBitmapText
  • Movement
    • Velocity
    • Acceleration
    • MaxVelocity
    • Gravity
    • Jumping
    • Angle
    • Angular Velocity
    • Angular Acceleration
  • Groups
    • Basic Group
    • Typed Group
    • Recycling
  • Tiling
    • Tileblock
    • Tilemap
  • Overlap
    • Simple Overlap
    • Overlap Callbacks
  • Collision
    • 1-to-1 Collision
    • Immovable
    • Tilemap Collision
    • Moving Platforms
  • Camera
    • Flash
    • Fade
    • Shake
    • Follow
  • Input
    • Basics
    • Keyboard
    • Mouse
    • Button
  • Sound
    • FlxSound

Simple Overlap

Use FlxG.overlap() to check if the bounding box of a FlxObject (or any objects in a group) overlaps the bounding box of another FlxObject (or any objects in the same or another group).

FlxG.overlap() will return true if it detects an overlap, and false. if it does not.

// overlaps will be true if the two objects are overlapping each other
overlaps = FlxG.overlap(objectA, objectB);

Demonstration

Source

source/PlayState.hx

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;

class PlayState extends FlxState
{
    var sprite:FlxSprite;
    var box:FlxSprite;

    override public function create()
    {
        super.create();

        box = new FlxSprite("assets/bigbox.png");
        box.screenCenter();
        add(box);

        sprite = new FlxSprite("assets/sprite.png");
        sprite.y = FlxG.height / 2 - sprite.height / 2;
        sprite.velocity.x = 100;
        add(sprite);
    }

    override public function update(elapsed:Float)
    {
        if ((sprite.velocity.x > 0 && sprite.x >= FlxG.width - sprite.width)
            || (sprite.velocity.x < 0 && sprite.x <= 0))
        {
            sprite.velocity.x *= -1;
        }

        box.alpha = FlxG.overlap(sprite, box) ? 0.5 : 1;

        super.update(elapsed);
    }
}
View Source on GitHub

See Also

  • Collision/1-to-1-collision

Tags

overlap
  1. Home
  2. Overlap
  3. Simple Overlap
Powered By HaxeFlixel Logo HaxeFlixel