如何让Hit Detection在Flash中运行?

时间:2012-11-16 19:50:36

标签: actionscript-3 flash-cs6

艾夫斯尝试了许多命中检测,但似乎没有一个能为我效劳。我试过hittest hittestobject hitarea。当我的对象(这是一个或一个电影剪辑完全进入c影片剪辑时,我希望c移动300 x方向。不需要像它在c影片剪辑中那样长时间进行针点检测。< / p>
package  {

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.media.Sound;
import flash.ui.Mouse;

public class bakingCake extends MovieClip {


    public function bakingCake() {
        // constructor code

        var object:MovieClip = new MovieClip;

        a.addEventListener(MouseEvent.MOUSE_DOWN,objectA);
        b.addEventListener(MouseEvent.MOUSE_DOWN,objectB);

        if (object.hitArea(c) == true)
        {
                c.x = 300;
        }           

        function objectA():void
        {           
            object = a;             
            object.addEventListener(MouseEvent.MOUSE_OVER,objectFun);
        }

        function objectB():void
        {
            object = b;
            object.addEventListener(MouseEvent.MOUSE_OVER,objectFun);
        }       


        function objectFun(event:MouseEvent):void
        {

            object.addEventListener(MouseEvent.MOUSE_DOWN,drag);
            object.addEventListener(MouseEvent.MOUSE_UP,sDrag);
        }
        function drag(event:MouseEvent):void
        {
            object.startDrag();
        }
        function sDrag(event:MouseEvent):void
        {
            object.stopDrag();

        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

你试过getBounds()吗? 我建议条件:

if (c.getBounds(c.parent).containsRect(a.getBounds(c.parent))
 || c.getBounds(c.parent).containsRect(b.getBounds(c.parent))) {
    c.x = 300;
}

IMO最好的方法是检查它是否由ENTER_FRAME事件触发,为任何对象提供。