【问题标题】:Flash actionscript 3, collision detection upon keypressFlash actionscript 3,按键碰撞检测
【发布时间】:2015-05-21 13:57:55
【问题描述】:

我正在尝试让我的角色与我创建的墙壁发生碰撞,但我在这样做时遇到了困难,我尝试在其他线程中使用解决方案,但没有一个奏效。请帮忙。本质上,我得到了碰撞检测工作,但是它只有在单击时才会激活,并带有鼠标事件。

square1_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);

function drag(e: MouseEvent): void {    
    e.target.startDrag();
}

function drop(): void {
    head_mc.x -= velocity;
    stopDrag();

    if (head_mc.hitTestObject(square2_mc)) {
        velocity = 0;    
        trace("Collision detected!");
    } else {
        trace("No collision.");
        velocity = 5;
    }
}

我要做的是,让我的玩家模型head_mc 在与我的墙square_mc 碰撞时停止向右移动。它仅在我单击对象时才有效,我需要它仅通过使用箭头键将 head_mc 移动到块中来工作。

【问题讨论】:

    标签: actionscript-3 flash collision detection


    【解决方案1】:

    您只能在单击时检测到碰撞,因为您已将 hitTestObject 放入一个作为 MouseEvent 函数的函数中。您可以做的是,不是在MOUSE_UPMOUSE_DOWN 上测试碰撞,而是通过使用TimerEvent 或直接通过Event.ENTER_FRAME 调用函数以频繁的时间间隔测试碰撞。这样,程序将在很短的时间间隔后继续检查碰撞,因此,当检测到碰撞时,该函数可以追踪trace("Collision detected!");,并且它没有碰撞的每一秒,它将继续追踪trace("No collision.");和你的速度将设置为 5。

    你可以这样添加一个函数:

    function checkCollision(e: Event): void {
      if (head_mc.hitTestObject(square2_mc)) {
        velocity = 0;
        trace("Collision detected!");
      } else {
        trace("No collision.");
        velocity = 5;
      }
    }
    
    stage.addEventListener(Event.ENTER_FRAME, checkCollision);
    

    【讨论】:

    • 如果可行,您可能希望选择此作为正确答案 :)
    猜你喜欢
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2013-05-04
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多