views:

37

answers:

1

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 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?

A: 

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.

Juan Pablo Califano
How exactly do I do that then? I've tried this:LOADER_1.contentLoader.addEventListener(Event.COMPLETE,LOADED_1);but there's a compiler error.
Okay I got it to compile (it was contentLoaderInfo) but nothing is showing up now. This is what I did (sorry if it's not showing up right): LOADER_2.contentLoaderInfo.addEventListener(Event.COMPLETE,LOADED_2); LOADER_2.load(new URLRequest('TEST.png')); function LOADED_2(event:Event):void { LOADER_2.x = 125; LOADER_2.y = 125; DATA_2 = new BitmapData(256,256,true,0); DATA_2.draw(LOADER_2); addChild(LOADER_2); }
I ended up using embed instead...I don't like it but it's so much easier to work with.