Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 8 of 8

Thread: Collision detection BitmapData.hitTest

  1. #1

    Afrostyle Collision detection BitmapData.hitTest

    I'm curious as to what I'm doing wrong.

    Here's a demo: http://theosoft.deviantart.com/art/F...lebee-91450740
    Code:
    package Bumblebee
    {
        import flash.display.Bitmap;
        import flash.display.BitmapData;
        import flash.display.Loader;
        import flash.display.Sprite;
        import flash.events.*;
        import flash.geom.Point;
        import flash.geom.Rectangle;
        import flash.net.URLRequest;
        import flash.ui.Mouse;
        import flash.text.TextField;
    
        
        public class Main extends Sprite
        {
            
            [Embed(source = 'media/bee.png')]        
            private var crosshairClass:Class;
            private var crosshair:Bitmap = new crosshairClass ();    
            
            [Embed(source = 'media/yellow_flower.png')]
            private var yflowerClass:Class;
            private var yflower:Bitmap = new yflowerClass ();
            
            private var yflowers:int = 5;
            
            private var score_disp:TextField = new TextField();
            private var score_txt:TextField = new TextField();
            private var score:int;
            
            public function Main():void
            {
                Mouse.hide();
                
                score_disp.x = 5;
                score_disp.y = 5;
                score_disp.text = "Score: ";
                addChild(score_disp);
                
                score_txt.x = 40;
                score_txt.y = 5;
                score = 0;
                score_txt.text = score.toString();
                addChild(score_txt);
                
                crosshair.height = 50;
                crosshair.width = 50;
                addChild(crosshair);
                addFlower(yflower);
                stage.addEventListener(Event.ENTER_FRAME, mouseMove);
                stage.addEventListener(Event.ENTER_FRAME, flowerMove);
    
            }
            private function addFlower(flowername:Bitmap):void
            {
                
                flowername.height = 30;
                flowername.width = 30;
                flowername.x = 800;
                flowername.y = Math.round(Math.random()*500);
                addChild(flowername);
                
            }
            
            private function flowerMove(evt:Event):void
            {
                if (yflower.x > 0)
                {
                    yflower.x--;
                }
                else
                {
                    removeChild(yflower);
                    addFlower(yflower);
                }
            }
            
            private function mouseMove(evt:Event):void 
            {
                crosshair.x = mouseX - crosshair.width/2;
                crosshair.y = mouseY - crosshair.height/2;
                
                if (crosshair.bitmapData.hitTest(new Point(crosshair.x-25,crosshair.y-25),255,yflower.bitmapData, new Point(yflower.x, yflower.y),255))
                {
                    
                    /*yflower.x = Math.round(Math.random()*400);
                    yflower.y = Math.round(Math.random() * 400);*/
                    removeChild(yflower);
                    score+=100;
                    score_txt.text = score.toString();
                    addFlower(yflower);
                }
                
                
            }
    
        }
    }

  2. Quote Originally Posted by theosoft View Post
    I'm curious as to what I'm doing wrong.

    Here's a demo: http://theosoft.deviantart.com/art/F...lebee-91450740
    Code:
    package Bumblebee
    {
        import flash.display.Bitmap;
        import flash.display.BitmapData;
        import flash.display.Loader;
        import flash.display.Sprite;
        import flash.events.*;
        import flash.geom.Point;
        import flash.geom.Rectangle;
        import flash.net.URLRequest;
        import flash.ui.Mouse;
        import flash.text.TextField;
    
        
        public class Main extends Sprite
        {
            
            [Embed(source = 'media/bee.png')]        
            private var crosshairClass:Class;
            private var crosshair:Bitmap = new crosshairClass ();    
            
            [Embed(source = 'media/yellow_flower.png')]
            private var yflowerClass:Class;
            private var yflower:Bitmap = new yflowerClass ();
            
            private var yflowers:int = 5;
            
            private var score_disp:TextField = new TextField();
            private var score_txt:TextField = new TextField();
            private var score:int;
            
            public function Main():void
            {
                Mouse.hide();
                
                score_disp.x = 5;
                score_disp.y = 5;
                score_disp.text = "Score: ";
                addChild(score_disp);
                
                score_txt.x = 40;
                score_txt.y = 5;
                score = 0;
                score_txt.text = score.toString();
                addChild(score_txt);
                
                crosshair.height = 50;
                crosshair.width = 50;
                addChild(crosshair);
                addFlower(yflower);
                stage.addEventListener(Event.ENTER_FRAME, mouseMove);
                stage.addEventListener(Event.ENTER_FRAME, flowerMove);
    
            }
            private function addFlower(flowername:Bitmap):void
            {
                
                flowername.height = 30;
                flowername.width = 30;
                flowername.x = 800;
                flowername.y = Math.round(Math.random()*500);
                addChild(flowername);
                
            }
            
            private function flowerMove(evt:Event):void
            {
                if (yflower.x > 0)
                {
                    yflower.x--;
                }
                else
                {
                    removeChild(yflower);
                    addFlower(yflower);
                }
            }
            
            private function mouseMove(evt:Event):void 
            {
                crosshair.x = mouseX - crosshair.width/2;
                crosshair.y = mouseY - crosshair.height/2;
                
                if (crosshair.bitmapData.hitTest(new Point(crosshair.x-25,crosshair.y-25),255,yflower.bitmapData, new Point(yflower.x, yflower.y),255))
                {
                    
                    /*yflower.x = Math.round(Math.random()*400);
                    yflower.y = Math.round(Math.random() * 400);*/
                    removeChild(yflower);
                    score+=100;
                    score_txt.text = score.toString();
                    addFlower(yflower);
                }
                
                
            }
    
        }
    }
    For on thing, you're using hittest. I would use a simple distance calculation method or something.

  3. #3
    What's wrong with hitTest? Everything I read, says to use it.

  4. #4
    213
    posts
    Registered User
    Quote Originally Posted by theosoft View Post
    What's wrong with hitTest? Everything I read, says to use it.
    Nothing is wrong with it, ArmoredSandwich just doesn't see that you want to perform pixel level detection between two bitmaps.
    Based on the code you provided, there is nothing that shows what the problem is, your usage of the bitmapdata hiTest is correct at the very least.
    Those the problem has to be in how that bitmapdata is being created, most likely even though your testing to ignore all transparency pixels your bitmap doesn't have any transparency.
    Of course I would make sure your bitmaps have transparency before jumping to any conclusions.
    The only thing that jumps out at me is new Point(crosshair.x-25,crosshair.y-25), why are you wanting to do the test 25 pixels to the top and left away from where the bitmap really is on screen?

  5. #5
    I dunno if i'm being noob but
    isn't _x and _y instead of x and y?
    or it has changed from AS 1 to 3?

  6. Quote Originally Posted by Gathan View Post
    Nothing is wrong with it, ArmoredSandwich just doesn't see that you want to perform pixel level detection between two bitmaps.
    Based on the code you provided, there is nothing that shows what the problem is, your usage of the bitmapdata hiTest is correct at the very least.
    Those the problem has to be in how that bitmapdata is being created, most likely even though your testing to ignore all transparency pixels your bitmap doesn't have any transparency.
    Of course I would make sure your bitmaps have transparency before jumping to any conclusions.
    The only thing that jumps out at me is new Point(crosshair.x-25,crosshair.y-25), why are you wanting to do the test 25 pixels to the top and left away from where the bitmap really is on screen?
    Nope, didn't see that I just think hittest is extremely slow and should be avoided at all times.

  7. #7
    TheCanadian's Avatar
    10,305
    posts
    Noo doot aboot it, eh?
    Your bitmaps probably have a white background which you should make transparent and set the alpha threshold parameter of BitmapData.hitTest to 1.
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  8. #8
    I'll check the transparency of my bitmaps and then get back to you. Thanks for the help.

    That might be the problem.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012