testHitObject在前几帧

时间:2015-05-18 19:36:45

标签: actionscript-3 flash-cs6 hittest

if I add the else statement I do receive the "not hit" in my output window, so it looks like the statement only returns true on the first few frames, when it is supposed to return false. Now my problem is that the code wants to remove child objects before they've even been created. I just want to achieve basic collision detection between the enemy and the bullet so that i can remove both children when an enemy is shot. Please keep in mind that there will be multiple enemies and bullets on the stage at a given time.

我似乎缩小了我的问题范围。在我使用removeChild()函数之后,发生的事情是敌人的实例仍然存在。直到现在我才出于某种原因看不到它。并且它的x和y属性等于0.因此,如果我将船移动到x = 0,它将有一个“看不见的”敌人与它相撞。所以我认为我的问题是我没有正确删除实例?如果没有,请你帮帮我吗?

    `    <pre>


 var BulletsArr:Array = new Array();
    var EnemysArray:Array = new Array();

    for(i = 0; i < 5; i++)
    {
        BulletsArr[i] = new Bullet();
    }

    for(i = 0; i < 5; i++)
    {
        EnemysArray[i] = new Enemy();
        //EnemysArray[i].x = 2000; //Fix
        //EnemysArray[i].y = 2000; //Fix
    }

    stage.addChild(Ship);

    Ship.addEventListener(Event.ENTER_FRAME, fnEnterFrame);

    function fnEnterFrame(event:Event)
    {
      txtScore.text = "Score: " + score;
    //Make Ship follow cursor
        Ship.x = stage.mouseX;

        //Boundries
        if (Ship.x > 500)
        {
            Ship.x = 550 - 50;
        }

        if (Ship.x <= 0)
        {
            Ship.x = 0 ;
        }

            for(var i = 0; i < 5; i++){
            for(var p = 0; p < 5; p++)
            {
                if(BulletsArr[i].hitTestObject(EnemysArray[p]))
                {
                    remove(BulletsArr[i]);
                    removeChild(EnemysArray[p]);
                }

    for(i = 0; i < 5; i++)
         {  
            BulletsArr[i].y -= 15;
         }

    for(i = 0; i < 5; i++)
         {  
            EnemysArray[i].y += 5;
         }

    for(var z = 0; z < 5; z++)
            {
               if(Ship.hitTestObject(EnemysArray[z])
                 {
                    Ship.parent.removeChild(Ship);
                    EnemysArray[z].parent.removeChild(EnemysArray[z]);
                    trace("Game over");
                 }

            }
for(i = 0; i < 5; i++)
{ 
var found:Boolean = false;
    for(var p = 0; p < 5; p++)
    {
        if(BulletsArr[i].hitTestObject(EnemysArray[p]))
        {
            BulletsArr[i].parent.removeChild(BulletsArr[i]);
            BulletsArr[i] = new Bullet();


            EnemysArray[p].parent.removeChild(EnemysArray[p]);
            EnemysArray[p] = new Enemy();
            //EnemysArray[p].x = 2000;//fix
            //EnemysArray[p].y = 2000;//fix

            score += 50;

            found = true;
            break;
        }

        if(found)
        {
            break;
        }
    }   
            }
        }

    var count_Enemys:int = 0;
    var Timer_3_sec:Timer = new Timer(3000, 0);
    Timer_3_sec.start();

    Timer_3_sec.addEventListener(TimerEvent.TIMER,spawn_ship);

    function spawn_ship(Event:TimerEvent):void
    {
       if(count_Enemys >= 5)
    {
        count_Enemys = 0;       
        EnemysArray[count_Enemys].x = fl_GenerateRandomNumber(450) + 70;
        EnemysArray[count_Enemys].y = 0 - fl_GenerateRandomNumber(250) - 100;
        addChild(EnemysArray[count_Enemys]);
        count_Enemys++;
    }
    else
    {       
        EnemysArray[count_Enemys].x = fl_GenerateRandomNumber(450) + 70;
        EnemysArray[count_Enemys].y = 0 - fl_GenerateRandomNumber(250) - 100;
        addChild(EnemysArray[count_Enemys]);
        count_Enemys++;
    }
}
        `</code>

0 个答案:

没有答案