A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: pixel perfect collisions with bitmap.hittest

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    3

    pixel perfect collisions with bitmap.hittest

    Basically I'm trying to get something like this to work:

    http://www.freeactionscript.com/2009...ion-detection/

    I have two png files. A ball and a maze. The ball is transparent at its corners (otherwise it would be a square) and the maze has various transparent tunnels in it. I want it so that when the ball is in the transparent part of the maze that no collision is detected and when it touches a side of the maze a collision is detected.

    It seems so simple but I've honestly been trying to get this to work for 3 days now. I've looked at tons of examples on google so I doubt any links to those (yes I've looked at mike chambers thing, and eight bit rocket, and all the other ones) are going to help me.

    Basically I have three problems, either no collision is ever detected, a collision is detected even when the ball is touching transparent pixels, or nothing is drawn on the screen.

    My latest failed attempt (one of many) never registers a collision:

    Code:
    package 
    {
        import flash.display.*;     import flash.events.*;
    	import flash.geom.*;		import flash.text.TextField;
    	import flash.utils.*;		import flash.net.*;
    	import flash.ui.Mouse;		import flash.system.*;
    	import Math;
        public class MAIN extends Sprite
        {
    		public var TEXT:TextField = new TextField();
    		public var LOADER_2:Loader = new Loader();
    		public var DATA_2:BitmapData;
    		public var LOADER_1:Loader = new Loader();
    		public var DATA_1:BitmapData;
            public function MAIN()
            {	
    			addChild(LOADER_2);
    			LOADER_2.contentLoaderInfo.addEventListener(Event.COMPLETE,LOADED_2);
    			LOADER_2.load(new URLRequest('TEST.png'));
    			
    			addChild(LOADER_1);
    			LOADER_1.contentLoaderInfo.addEventListener(Event.COMPLETE,LOADED_1);
    			LOADER_1.load(new URLRequest('BALL.png'));
    			
    			Mouse.hide();			addChild(TEXT);
    			stage.frameRate = 60;	stage.addEventListener(Event.ENTER_FRAME,STEP);
            }
    		
    		public function LOADED_2(event:Event):void
    		{
    			DATA_2 = new BitmapData(LOADER_2.width,LOADER_2.height,true,0);
    			DATA_2.draw(DisplayObject(event.target.content));
    		}
    		
    		public function LOADED_1(event:Event):void
    		{
    			DATA_1 = new BitmapData(LOADER_1.width,LOADER_1.height,true,0);
    			DATA_1.draw(DisplayObject(event.target.content));
    		}
    		
    		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),255))
    			{TEXT.text = 'hit';} else {TEXT.text = 'miss';}
    		}
        }
    }
    Honestly could someone just please post a very very very very simple working example of this using loaders to load the png files? I've tried so many things and just cannot get this to work for the life of me.
    Last edited by 1101; 08-27-2010 at 09:09 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center