1

I have converted a PNG into a bitmap, then converted that into bitmapData.

I have a object called _player, and I wish to add collision detection, however I can seem to get it to work.

my code is:

if(bmd1.hitTest(new Point(_player.x, _player.y))){
trace("hit");
}

bmd1 is my bitmapData,_player is the object is wish to test against.

I am getting the following error:

1136: Incorrect number of arguments, Expected 3

I have looked around but cannott find what argument I am missing

Any ideas?

Update

I have tried

if(bmd1.hitTest(new Point(_player.x, _player.y), 50, _player)){
trace("hit");
}

with no joy

Update 2

Sorry, I should mention that the reason for me taking this approach is that I have a PNG, with transparent areas, I need to test for collisions in the non-transparent areas, which is why I was using this approach

I have a PNG, i import that and convert to bitmap, then convert to bitmapData

I maybe doing this completely wrong. Could you show me where the problem lies?

phwd
  • 19,975
  • 5
  • 50
  • 78
atmd
  • 7,430
  • 2
  • 33
  • 64
  • 1
    hitTest on a BitmapData object is a bit different to the methods found in normal display objects. Are you sure you didn't want to run the hitTest against your Bitmap instead? – shanethehat Aug 23 '11 at 15:48

3 Answers3

6

hitTest has 3 mandatory arguments:

public function hitTest(firstPoint:Point, firstAlphaThreshold:uint, secondObject:Object, secondBitmapDataPoint:Point = null, secondAlphaThreshold:uint = 1)

Check the doc

Kodiak
  • 5,978
  • 17
  • 35
1

In the end I converted my player movieclip to bitmapdata, converted my png map to bitmap data, then used hitTest to check the x and y of each bitmap against each other

atmd
  • 7,430
  • 2
  • 33
  • 64
0

The method you want is hitTestPoint() not hitTest()

EDIT: whoops I missed that you were doing the hit test against BitmapData instead of a DisplayObject. BitmapData.hitTest() performs pixel-level detection which is pretty slow in many situations. You're probably better off putting the BitmapData into a Sprite and then using hitTestPoint()

jhocking
  • 5,527
  • 1
  • 24
  • 38
  • 1
    BitmapData does not have a hitTestPoint method, it's a bit different to other DisplayObjects. – shanethehat Aug 23 '11 at 15:47
  • Place the BitmapData inside a Sprite and it's problem solved. – Alex Aug 23 '11 at 15:53
  • oh, he's running the hit test directly on the bitmap data? That's weird; I would run the hit test on a Sprite which has that BitmapData. I generally treat BitmapData as just the static image information and Sprite as an interactive object. – jhocking Aug 23 '11 at 15:53