Actionscript 3:类内的hitTestObject

时间:2012-06-02 15:59:59

标签: actionscript-3 flash class hittest

当我点击汽车(MouseEvent.MOUSE_DOWN)时,汽车消失,并执行verlaagLevens()函数。这有效(但我在其中加入了评论)

现在,我想要检测我的Ship和同一辆Car之间的hitTestObject,而不是clickevent,但它似乎没有工作。 这是我的汽车级:

package {
import flash.display.*;
import flash.events.*;

public class Car extends MovieClip
{
    //private var hit:uint = 0;
    private var mySpeed:uint = 0;
    private var target:Ship;
    private var stageRef:Stage;
    public static var instance2:Car;

    public function Car(stageRef:Stage, target:Ship) {
        instance2 = this;
        this.x = x;
        this.stageRef = stageRef;
        this.target = target;
        mySpeed = 10;
        addEventListener(Event.ENTER_FRAME, beweeg);
        reset();

    }
    public function reset() {
        if(Main.end_def == 0){
        this.y =0-100/Math.random();
        this.x = Math.random()*(1+300-60)+60;
        }
        //hit = 0;

    }
    public function getroffen() {

        Main.instance.verlaagLevens();
        this.parent.removeChild(this);

    }


    public function beweeg(evt:Event):void {
        if (hitTestObject(target.hit))
        {
            trace("hitTest");
            getroffen();
            reset();
        }
        this.y += mySpeed;
        if (this.y>480) {
            reset();
        }

    }


}

}

这是我的主要班级:

package {

    import flash.display.*;
    import flash.display.Stage;
    import flash.events.*;
    import flash.utils.*;

    public class Main extends MovieClip
    {

        private var aantaltegenliggers:uint = 5;
        private var _levens:uint = 3;
        var ourShip:Ship = new Ship(stage);
        public static var instance:Main;
        var car:MovieClip;
        var end = 0;
        public static var end_def:uint;


        public function Main()
        {
            stop();
            instance = this;
        }

        public function initGame():void
        {
            //add it to the display list
            stage.addChild(ourShip);
            ourShip.x = stage.stageWidth / 2;
            ourShip.y = stage.stageHeight / 1.3;

            _levens=3;
            end_def=0;
            teken_levens();


            for (var i:uint=0; i<aantaltegenliggers; i++) 
                {
                    var myTimer:Timer = new Timer(5000, 1);
                    myTimer.start();
                    myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent){


                    if (end==0){
                    car = new Car(stage, ourShip);
                    car.name = "car"+i;
                    //car.addEventListener(Event.ENTER_FRAME,getroffen);
                    car.addEventListener(Event.ENTER_FRAME,gameover);
                    stage.addChild(car);

                    }
                    });


            }

        }
            public function verlaagLevens():void 
            {
                verwijder_een_leven(_levens);
                _levens--;
            }
            // één leven tekenen
            public function teken_een_leven(levenIndex:uint):void 
            {
                var leven:MovieClip = new Leven(); // linkage - export voor Actionscript
                leven.x = levenIndex*35; // positie leven in de x as, 35 pixels opschuiven
                leven.y = 10; // positie leven in de y as
                leven.name = "leven"+levenIndex;
                addChild(leven);    // één leven tonen op het scherm
            }
            // alle levens tekenen
            public function teken_levens():void 
            {
                verwijder_levens(); // verwijder één leven als je de missile niet hebt kunnen raken
                for (var i:uint=1; i<=_levens; i++) 
                {
                    teken_een_leven(i);
                }
            }
            // een leven van het scherm verwijderen
            public function verwijder_een_leven(levenIndex:uint):void 
            {
                if (!(getChildByName("leven"+levenIndex)==null) ) 
                {  // ! is negatie = logische operator
                    removeChild(getChildByName("leven"+levenIndex));
                }
            }
            // alle levens verwijderen
            public function verwijder_levens():void 
            {
                for (var i:uint=1; i<=_levens; i++) 
                {
                    verwijder_een_leven(i);
                }
            }

            public function gameover(evt:Event):void 
            {
                if(_levens==0)
                {   
                    if(end_def == 0){
                    end = 1;
                    stage.removeChild(ourShip);
                    gotoAndStop("gameover");
                    }
                    end_def=1;
                }
            }



    }

}

1 个答案:

答案 0 :(得分:1)

您正在Car的构造函数中执行命中测试,该构造函数仅被调用一次。 您可能想要的(因为汽车移动)是在您的两个功能中检查每一帧的碰撞。 (这也是假设target.hit是正确的,我无法分辨,因为我没有访问你的船但我会给你怀疑的好处)。