lundi 13 février 2017

(Actionscript 3) Pixel-perfect collision detection between the walls and player?

I am trying to make a flash game in which there is collision detection between the player and the walls. However, when I try using Wall11.hitTestPoint(), I cannot get the collision detection to be perfect. Then, I decided to use bitmap but it is hard to code this because the wall is irregularly shaped (it is not a square, rectangle, circle or any regular shape). Is there anyway to improve the collision detection with walls?

function checkCollision(_debug:Boolean = false):Boolean {           
        var bmd1:BitmapData = new BitmapData(Wall11.width, Wall11.height, true, 0);
        var bmd2:BitmapData = new BitmapData(LevelOnePlayer.width, LevelOnePlayer.height, true, 0);

        bmd1.draw(Wall11);
        bmd2.draw(LevelOnePlayer);

        if (_debug) {
            var bmp:Bitmap = new Bitmap(bmd1);
            bmp.x = Wall11.x;
            bmp.y = Wall11.y;
            addChild(bmp);

            var bmp2:Bitmap = new Bitmap(bmd2);
            bmp2.x = LevelOnePlayer.x;
            bmp2.y = LevelOnePlayer.y;
            addChild(bmp2);
        }
        if(bmd1.hitTest(new Point(Wall11.x, Wall11.y), 255, bmd2, new Point(LevelOnePlayer.x, LevelOnePlayer.y), 255))
        return true;
        if (!_debug) {
            bmd1.dispose();
            bmd2.dispose();
        }
        return false;
    }    

Aucun commentaire:

Enregistrer un commentaire