MKII
MKII

Reputation: 911

BitmapData Collision

I have been trying to use BitmapData for collision detection, but have failed so far, and I don't know why. The code compiles, but doesn't do anything (when it should print "hit" ). Can anyone help me?

package 
{
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.display.BitmapData;
import flash.geom.Point;

public class Main extends MovieClip
{
    private var spd:int = 5;
    private var pressedKeys:Object = {};
    private var bgMask:BitmapData;
    private var plMask:BitmapData;
    private var wall:Wall = new Wall();

    public function Main()
    {
        var bgBitMapData:BitmapData = new BitmapData(bg.width,bg.height,true,0);
        bgMask = bgBitMapData.clone();
        wall.x = bg.x;
        wall.y = bg.y;
        bgMask.draw( wall );

        var plBitMapData:BitmapData = new BitmapData(pl.width,bg.height,true,0);
        plMask = bgBitMapData.clone();
        bgMask.draw( pl );

        stage.addEventListener(KeyboardEvent.KEY_DOWN,this.keyPressHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP,this.keyPressHandler);
        stage.addEventListener(Event.ENTER_FRAME,this.gameLoop);
    }

    public function keyPressHandler( evt:KeyboardEvent ):void
    {
        if (evt.type == KeyboardEvent.KEY_DOWN)
        {
            pressedKeys[evt.keyCode] = 1;
        }
        else
        {
            delete pressedKeys[ evt.keyCode ];
        }
    }

    public function inputHandler():void
    {
        var moveXBy:int;
        var moveYBy:int;

        if (pressedKeys[Keyboard.J])
        {
            moveXBy -=  spd;
        }

        if (pressedKeys[Keyboard.K])
        {
            moveYBy +=  spd;
        }

        if (pressedKeys[Keyboard.L])
        {
            moveXBy +=  spd;
        }

        if (pressedKeys[Keyboard.I])
        {
            moveYBy -=  spd;
        }

        pl.x +=  moveXBy;
        pl.y +=  moveYBy;

    }

    public function playerWallCollision():void
    {
        if ( plMask.hitTest( new Point( wall.x, wall.y ), 1, bgMask, new Point( pl.x, pl.y ), 1))
        {
            trace(  "hit" );
        }
    }

    public function gameLoop( evt:Event ):void
    {
        wallUpdate();
        inputHandler();
        playerWallCollision();
    }

    private function wallUpdate()
    {
        wall.x = bg.x;
        wall.y = bg.y;
    }
}
}

Upvotes: 0

Views: 264

Answers (1)

Sidrich2009
Sidrich2009

Reputation: 560

as far as this script tells me, after the lines:

var bgBitMapData:BitmapData = new BitmapData(bg.width,bg.height,true,0);
    // bgBitMapData is an empty BMP at this point cause its just freshly instanciated
    bgMask = bgBitMapData.clone();
    wall.x = bg.x;
    wall.y = bg.y;
    bgMask.draw( wall );

    var plBitMapData:BitmapData = new BitmapData(pl.width,bg.height,true,0);
    // no idea y you make this plBitMapData anyway
    plMask = bgBitMapData.clone();
    // now you clone the empty bgBitMapData to be plMask
    bgMask.draw( pl );

plMask is never filled with anything! So compared with an empty BitmapData should allways return false.

So the answer to when should it output hit - never ;)

And as a sidenote try to keep it more readable by 1. write a comment every here and there what your intention is and 2. go for direct ways :

    var bgBitMapData:BitmapData = new BitmapData(bg.width,bg.height,true,0);
    bgMask = bgBitMapData.clone();
    // this position seems just wrong to me wall.x or wall.y do not effect the draw so keep it somewhere else... since you will copy the inside of wall
    wall.x = bg.x;
    wall.y = bg.y;
    bgMask.draw( wall );

For me it should look like:

    // placing a wall to new / init coordinates 
    wall.x = bg.x;
    wall.y = bg.y;

    // drawing wall on bgMask for hittesting against player or what ever you intende here
    bgMask = new BitmapData(bg.width,bg.height,true,0);
    bgMask.draw( wall );

Upvotes: 1

Related Questions