Search code examples
actionscript-3collision-detection

AS3 collision detection is not recognized


I'm quite new to AS3, and I need some help. I'm trying to make a game like Mario. I've made a character which can jump right now, but I've got some problems with collision detection.

I would like my character to jump on a bar, which is placed higher. My collision detection doesn't work at all I gues..

I've made a cirle which has a instance name mcMain and I've made a MovieClip of it. T also made a rectangle which has a instance name balkje, I also made aMovieClip of it.

I hope you can tell me what is wrong about my code and what I've to change to make the collision detection work! Thanks a lot!

balkje.addEventListener(KeyboardEvent.KEY_DOWN, drag);
stage.addEventListener(KeyboardEvent.KEY_UP, drop);

function drag(e:KeyboardEvent):void
{
    e.target.startDrag();
}

function drop(e:KeyboardEvent):void
{
    stopDrag();
    if (balkje.hitTestObject(mcMain))
    {
        trace("Collision detected!");
    }
    else
    {
        trace("No collision.");
    }
}

Solution

  • I think you should be using mouseEvent, not keyboard event. How can you drag with the keyboard?

    balkje.addEventListener(MouseEvent.MOUSE_DOWN, drag);
    balkje.addEventListener(MouseEvent.MOUSE_UP, drop);
    
    function drag(e:MouseEvent):void
    {
        e.target.startDrag();
    }
    
    function drop(e:MouseEvent):void
    {
        e.target.stopDrag();
    
        if (balkje.hitTestObject(mcMain))
        {
            trace("Collision detected!");
        }
        else
        {
            trace("No collision.");
        }
    }