Mister PHP
Mister PHP

Reputation: 327

How to use hitTestObject on many objects

I can check if one object is hiting another, but what if I have 10 MovieClip objects, and i want to check if any object is hiting ANY object:

package  {
    import flash.display.MovieClip;
    import flashx.textLayout.events.DamageEvent;
    import fl.motion.Animator;
    import flash.geom.Point;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.Event;
    import flash.geom.ColorTransform;

    public class Test extends MovieClip {


    private var arrows:Array;
    private var timer:Timer;
        public function Test() {
            init();
        }

        private function init():void {

            timer = new Timer(1000, 6);
            timer.addEventListener(TimerEvent.TIMER, timerEvent);
            arrows = new Array();
            timer.start();

        }

        private function timerEvent(e:TimerEvent):void{

            var arrow:Arrow = new Arrow();
            arrow.x = 5;
            arrow.y = Math.random() * 200 + 10;
            addChild(arrow);
            arrow.addEventListener(Event.ENTER_FRAME, onEnterFrame);
            arrows.push(arrow);

            //trace(555);
        }

        private function onEnterFrame(e:Event):void{

            e.target.x += 4;

            if(e.target.x > 400)
            {
                e.target.transform.colorTransform = new ColorTransform(0, 0, 1, 1, 0, 0, 1, 1);
                e.target.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                e.target.addEventListener(Event.ENTER_FRAME, goBack);

            }

        }

        private function goBack(e:Event):void {

            e.target.x -= 4;

            if(e.target.x < 50)
            {
                e.target.transform.colorTransform = new ColorTransform(1, 1, 1, 1, 0, 0, 1, 1);
                e.target.removeEventListener(Event.ENTER_FRAME, goBack);
                e.target.addEventListener(Event.ENTER_FRAME, onEnterFrame);

            }

        }

    }

}

how can i check if any arrow is touching other arrow object?, doesn't matter what object,I need something like hitTestGlobal

Upvotes: 0

Views: 921

Answers (2)

Rytis Alekna
Rytis Alekna

Reputation: 1377

At least you can all objects hitting one point by using method DisplayObjectContainer.getObjectsUnderPoint(point:Point). If boundaries of your main object is not changing you can predefine edge points that you will hit testing every EnterFrame event.

Upvotes: 2

Gio
Gio

Reputation: 1954

Yes. You will have to check hit test on every object you need. And yes, it's a costy operation, but when writing games there's no other workaround. Try using Vector instead of an Array for a little performance boost, as Vector is type dependant array and it uses less memory. You can check the syntax HERE.

You'd instantiate it like this:

private var arrows:Vector.<Arrow> = new Vector.<Arrow>();

Upvotes: 1

Related Questions