Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:39:45+00:00 2026-06-02T19:39:45+00:00

I have been trying to use BitmapData for collision detection, but have failed so

  • 0

I have been trying to use BitmapData for collision detection, but have failed so far, and I don’t know why. The code compiles, but doesn’t do anything (when it should print “hit” ). Can anyone help me?

package 
{
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.display.BitmapData;
import flash.geom.Point;

public class Main extends MovieClip
{
    private var spd:int = 5;
    private var pressedKeys:Object = {};
    private var bgMask:BitmapData;
    private var plMask:BitmapData;
    private var wall:Wall = new Wall();

    public function Main()
    {
        var bgBitMapData:BitmapData = new BitmapData(bg.width,bg.height,true,0);
        bgMask = bgBitMapData.clone();
        wall.x = bg.x;
        wall.y = bg.y;
        bgMask.draw( wall );

        var plBitMapData:BitmapData = new BitmapData(pl.width,bg.height,true,0);
        plMask = bgBitMapData.clone();
        bgMask.draw( pl );

        stage.addEventListener(KeyboardEvent.KEY_DOWN,this.keyPressHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP,this.keyPressHandler);
        stage.addEventListener(Event.ENTER_FRAME,this.gameLoop);
    }

    public function keyPressHandler( evt:KeyboardEvent ):void
    {
        if (evt.type == KeyboardEvent.KEY_DOWN)
        {
            pressedKeys[evt.keyCode] = 1;
        }
        else
        {
            delete pressedKeys[ evt.keyCode ];
        }
    }

    public function inputHandler():void
    {
        var moveXBy:int;
        var moveYBy:int;

        if (pressedKeys[Keyboard.J])
        {
            moveXBy -=  spd;
        }

        if (pressedKeys[Keyboard.K])
        {
            moveYBy +=  spd;
        }

        if (pressedKeys[Keyboard.L])
        {
            moveXBy +=  spd;
        }

        if (pressedKeys[Keyboard.I])
        {
            moveYBy -=  spd;
        }

        pl.x +=  moveXBy;
        pl.y +=  moveYBy;

    }

    public function playerWallCollision():void
    {
        if ( plMask.hitTest( new Point( wall.x, wall.y ), 1, bgMask, new Point( pl.x, pl.y ), 1))
        {
            trace(  "hit" );
        }
    }

    public function gameLoop( evt:Event ):void
    {
        wallUpdate();
        inputHandler();
        playerWallCollision();
    }

    private function wallUpdate()
    {
        wall.x = bg.x;
        wall.y = bg.y;
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:39:47+00:00Added an answer on June 2, 2026 at 7:39 pm

    as far as this script tells me, after the lines:

    var bgBitMapData:BitmapData = new BitmapData(bg.width,bg.height,true,0);
        // bgBitMapData is an empty BMP at this point cause its just freshly instanciated
        bgMask = bgBitMapData.clone();
        wall.x = bg.x;
        wall.y = bg.y;
        bgMask.draw( wall );
    
        var plBitMapData:BitmapData = new BitmapData(pl.width,bg.height,true,0);
        // no idea y you make this plBitMapData anyway
        plMask = bgBitMapData.clone();
        // now you clone the empty bgBitMapData to be plMask
        bgMask.draw( pl );
    

    plMask is never filled with anything! So compared with an empty BitmapData should allways return false.

    So the answer to when should it output hit – never 😉

    And as a sidenote try to keep it more readable by 1. write a comment every here and there what your intention is and 2. go for direct ways :

        var bgBitMapData:BitmapData = new BitmapData(bg.width,bg.height,true,0);
        bgMask = bgBitMapData.clone();
        // this position seems just wrong to me wall.x or wall.y do not effect the draw so keep it somewhere else... since you will copy the inside of wall
        wall.x = bg.x;
        wall.y = bg.y;
        bgMask.draw( wall );
    

    For me it should look like:

        // placing a wall to new / init coordinates 
        wall.x = bg.x;
        wall.y = bg.y;
    
        // drawing wall on bgMask for hittesting against player or what ever you intende here
        bgMask = new BitmapData(bg.width,bg.height,true,0);
        bgMask.draw( wall );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to use JScrollPane with my applet, but it doesn't work.
I have been trying to use the hibernate dialect for SQLite from http://code.google.com/p/hibernate-sqlite/ in
I have been trying to use Prototype to do some DOM manipulation, but I
I have been trying to use the gem 'character-encodings' which doesn't build in 1.9.2
I have been trying to use localstorage to store Jquery-min but I am unable
I have been trying to use OleDbDataAdapter to update a DataTable but got confused
I have been trying to use JQuery UI but cant seem to get any
I have been trying to use some objective C, i have got as far
I have been trying to use nextAll() and siblings() but those functions never return
I have been trying to use regex but I cant seem to get it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.