如何在bitmapData和对象之间正确使用hitTest?
我已将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
hitTest 有 3 个强制参数:
检查 文档
hitTest has 3 mandatory arguments:
Check the doc
最后,我将播放器影片剪辑转换为位图数据,将 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
您想要的方法是 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()