hitTest or hitTestObject to detect a collision with multiple objects in AS3?

跟風遠走 提交于 2019-12-11 20:53:10

问题


I'm working on a small game and I would like to detect if the player collided with one of the boxes that are placed on the stage. What is the best method to do this, hitTest or hitTestObject? My code:

var hitTestClips:Array = [smallbox, mediumbox, bigbox] //more to come

    var fps = 60;
    var moveTimer:Timer = new Timer(1000/fps);

    moveTimer.addEventListener(TimerEvent.TIMER, onMoveTimer);
    moveTimer.start();

    function onMoveTimer(e:TimerEvent){
    player.x += Math.round(1)

    for(var player:MovieClip in hitTestClips)
    {
      if(player.hitTest(this.x, this.y, true))
      {
        trace("HIT");
      }
    }

    }

回答1:


You can loop through the enemy objects and use hitTestObject to see if a collision occurred.

var hasCollision:Boolean = player.hitTestObject( enemy );

This page describes this and various other methods: AS3 Collision Detection



来源:https://stackoverflow.com/questions/28000351/hittest-or-hittestobject-to-detect-a-collision-with-multiple-objects-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!