开发者

collision detection in actionscript using bitmap.hittest

开发者 https://www.devze.com 2023-01-13 13:51 出处:网络
Okay so I have two png files, a circle and a maze. Basically the maze is a square with some paths carved through it. I want to draw these images, move the circle to the mouse coordinates, and have a t

Okay so I have two png files, a circle and a maze. Basically the maze is a square with some paths carved through it. I want to draw these images, move the circle to the mouse coordinates, and have a text say 'hit' when the circle intersects a wall of the maze and 'miss' when it doesn't. Now I want to do these using the bitmapdata.hittest method. The circle is a 32x32 image and the maze is a 256*256 image. I've gotten everything set up and everything draws on the screen correctly but I can't get the actual collision detection part of it to work, that is it always displays 'miss' rather than 'hit' even when the circle clearly intersects the maze. Here's what I've done:

package 
{
    import开发者_JAVA百科 flash.display.*;     
    import flash.events.*;
    import flash.geom.*;
    import flash.text.TextField;
    import flash.utils.getTimer;
    import flash.net.URLRequest;
    import flash.ui.Mouse;
    import flash.system.*;
    import Math;
    public class MAIN extends Sprite
    {
        private var TEXT:TextField = new TextField();
        public var LOADER_1:Loader = new Loader();
        public var LOADER_2:Loader = new Loader();
        public var DATA_1:BitmapData;
        public var DATA_2:BitmapData;

        public function MAIN()
        {   
            LOADER_2.load(new URLRequest('TEST.png'));
            LOADER_2.x = 125;       LOADER_2.y = 125;
            DATA_2 = new BitmapData(256,256,true,0);
            DATA_2.draw(LOADER_2);
            addChild(LOADER_2);

            LOADER_1.load(new URLRequest('BALL.png'));
            LOADER_1.x = mouseX;        LOADER_1.y = mouseY;
            DATA_1 = new BitmapData(32,32,true,0);
            DATA_1.draw(LOADER_1);
            addChild(LOADER_1);

            Mouse.hide();
            stage.frameRate = 60;
            addChild(TEXT);
            stage.addEventListener(Event.ENTER_FRAME,STEP);
        }
        public function STEP(event:Event):void
        {
            LOADER_1.x = mouseX;
            LOADER_1.y = mouseY;

            if (DATA_1.hitTest(new Point(LOADER_1.x,LOADER_1.y),255,DATA_2,new Point(LOADER_2.x,LOADER_2.y)))
            {
                TEXT.text = 'hit';
            }
            else
            {
                TEXT.text = 'miss';
            }
        }
    }
}

So can someone tell me what I'm doing wrong here?


You have to wait until your images load before drawing them to a BitmapData.

        LOADER_2.load(new URLRequest('TEST.png'));
        LOADER_2.x = 125;       LOADER_2.y = 125;
        DATA_2 = new BitmapData(256,256,true,0);
        DATA_2.draw(LOADER_2);

At this point you are taking a "snapshot" of your loader, but the loader has no content. So, you should ait for the Event.COMPLETE event to fire on each loader, and the draw to the BitmapData object.

0

精彩评论

暂无评论...
验证码 换一张
取 消