如何检测碰撞检测闪光灯AS3? [英] How do I detect collision detection in flash AS3?

查看:163
本文介绍了如何检测碰撞检测闪光灯AS3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Flash中创建AS3迷宫,与用户导向的特征。我尝试使用这个(如下图),但是这将需要我做所有的迷宫墙个人和碰撞检测设置为每一个。是否有完成同样的事情更简单的方法?

  monkey.addEventListener(Event.ENTER_FRAME,handleCollision)

功能handleCollision(五:事件):无效
{
    如果(monkey.hitTestObject(墙))
       {
           跟踪(打);
       }
       其他
       {
           跟踪(小姐);
       }
}
 

解决方案

您可以做到这一点的方法之一,就是使用 hitTestPoint()方法(如果有)的测试边角都打你的墙上。

hitTestPoint()只测试一个位置,看看是否能点与物体发生碰撞。这就是你可以测试你的猴子的左上角,看看它是否摸墙:

  //我假设X,Y是你的猴子的左上角

如果(wall.hitTestPoint(monkey.x,monkey.y,真))
{
   //左上撞上墙
{
 

所以,你可以为各个角落做同样的,如果你愿意,你可以决定你要检查猴子任何碰撞点。

根据您的precision水平,这种方法可能工作得很好满足您的需求。但是如果你想像素的完美碰撞,你可以检查出此链接:

http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/

I wanted to create a maze in flash AS3, with the user guiding the character. I tried using this (below) but this will require me to make all the maze walls individual and setting collision detection to each one. Is there an easier way of accomplishing the same thing?

monkey.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void
{
    if(monkey.hitTestObject(wall))
       {
           trace("HIT");
       }
       else
       {
           trace("MISS");
       }
}

解决方案

One way you could do this, is to use hitTestPoint() method to test if any of the corners have hit your wall.

hitTestPoint() tests only a single location to see if that point collides with an object. This is how you could test the top left corner of your monkey to see if it's touching the wall :

// I am assuming that x,y is the top left corner of your monkey

if (wall.hitTestPoint(monkey.x, monkey.y, true))
{
   // top left collided with wall
{

So you could do the same for all corners, or if you want, you can determine any collision points you want to check for the monkey.

Depending on your level of precision, this method might work just fine for your needs. But if you want pixel perfect collision, you can check out this link :

http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/

这篇关于如何检测碰撞检测闪光灯AS3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆