如何在bitmapData和对象之间正确使用hitTest?

发布于 2024-11-30 21:40:40 字数 776 浏览 1 评论 0原文

我已将 PNG 转换为位图,然后将其转换为 bitmapData。

我有一个名为 _player 的对象,我希望添加碰撞检测,但我似乎可以让它工作。

我的代码是:

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

bmd1 是我的 bitmapData_player 是希望测试的对象。

我收到以下错误:

1136: Incorrect number of arguments, Expected 3

我环顾四周,但找不到我缺少的参数 有什么

想法吗?

更新

尝试

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

我没有高兴地

更新2

抱歉,我应该提到,我采取这种方法的原因是我有一个带有透明区域的PNG,我需要测试非透明区域中的碰撞,这就是我使用这种方法的原因

我有一个PNG,我导入它并转换为位图,然后转换为bitmapData

我也许这样做完全错误。你能告诉我问题出在哪里吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最美不过初阳 2024-12-07 21:40:40

hitTest 有 3 个强制参数:

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

检查 文档

hitTest has 3 mandatory arguments:

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

Check the doc

明天过后 2024-12-07 21:40:40

最后,我将播放器影片剪辑转换为位图数据,将 png 贴图转换为位图数据,然后使用 hitTest 来检查每个位图的 x 和 y

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

○闲身 2024-12-07 21:40:40

您想要的方法是 hitTestPoint() 而不是 hitTest()

编辑:哎哟我错过了您正在针对 BitmapData 而不是 DisplayObject 进行命中测试。 BitmapData.hitTest() 执行像素级检测,在许多情况下速度相当慢。您最好将 BitmapData 放入 Sprite 中,然后使用 hitTestPoint()

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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文