Actionscript 3 AS3 hitTestObject结束游戏

Actionscript 3 AS3 hitTestObject结束游戏,actionscript-3,adobe,flash-cs5,Actionscript 3,Adobe,Flash Cs5,您好,我尝试创建一个类似于《太空入侵者》的游戏,发现很难创建hitTestObject,因此,如果alienbullet击中了mc_枪,玩家将失去一条生命,一旦失去三条生命,游戏就结束了,并被带到屏幕上,他/她可以在屏幕上输入自己的名字,进入高分表 我仍然要做下面的事情,但不知道从哪里开始 为玩家创造三个生命。 当所有的生命都逝去时,游戏结束。 进入姓名屏幕 高分表 任何帮助都将不胜感激 这是我在文档正文中的编码 stop(); var score:Number = 0; var livesLe

您好,我尝试创建一个类似于《太空入侵者》的游戏,发现很难创建hitTestObject,因此,如果alienbullet击中了mc_枪,玩家将失去一条生命,一旦失去三条生命,游戏就结束了,并被带到屏幕上,他/她可以在屏幕上输入自己的名字,进入高分表

我仍然要做下面的事情,但不知道从哪里开始

为玩家创造三个生命。 当所有的生命都逝去时,游戏结束。 进入姓名屏幕 高分表

任何帮助都将不胜感激

这是我在文档正文中的编码

stop();
var score:Number = 0;
var livesLeft:Number = 3;

var lifesArray:Array = new Array();
lifesArray.push(life1);
lifesArray.push(life2);
lifesArray.push(life3);

addEventListener(Event.ENTER_FRAME, pulse);

function pulse(event:Event):void
{
    checkForHitOnGun();
    checkForHitOnAliens();
    checkForBulletOnBulletHit();
    //checkEndGameCondition 
}

function checkForHitOnGun()
{
    for (var i=0; i<numChildren; i++)
    {
        if (getChildAt(i) is alienbullet)
        {
            var mc_alienbullet:MovieClip = getChildAt(i) as MovieClip;
            for (var j=0; j<numChildren; j++)
            {
                if (getChildAt(j) is gun)
                {
                    var mc_gun:MovieClip = getChildAt(j) as MovieClip;
                    if (mc_alienbullet.hitTestObject(mc_gun))
                    {

                    health -= 10;
                    if (health<1){
                        if(livesLeft<1){
                            gotoAndPlay(2);
                            removeChild(mc_gun);
                            stage.removeEventListener(Event.ENTER_FRAME,pulse);


                        }
                        livesLeft--;
                        lifesArray[livesLeft].visible = false;

                        health=100;
                        if (livesLeft == 0){
                            (endGo);
                            gotoAndPlay(2);
                        }
                    }
                    trace("health =" +health);
                    }
                }
            }
        }
    }
}


function checkForHitOnAliens()
{
    for (var i=0; i<numChildren; i++)
    {
        if (getChildAt(i) is bullet)
        {
            var mc_bullet:MovieClip = getChildAt(i) as MovieClip;
            for (var j=0; j<numChildren; j++)
            {
                if (getChildAt(j) is alien)
                {
                    var mc_alien:MovieClip = getChildAt(j) as MovieClip;
                    if (mc_bullet.hitTestObject(mc_alien))
                    {
                        var tobeDestroyed:MovieClip;
                        if (mc_alien.x > mc_bullet.x)
                        {
                            tobeDestroyed = mc_alien;
                        }
                        else
                        {
                            tobeDestroyed = mc_alien;
                        }
                        var bomb_mc:explosion = new explosion();
                        bomb_mc.x = tobeDestroyed.x;
                        bomb_mc.y = tobeDestroyed.y;

                        var addspaceship:alien = new alien();

                        addspaceship.x = 600;
                        addspaceship.y = Math.random()*(stage.stageHeight);

                        tobeDestroyed.destroy();
                        addChild(bomb_mc);
                        addChild(addspaceship);
                        score++;

                        return;
                    }
                }
                tb_score.text = "" + score;
            }
        }
    }
}

function checkForBulletOnBulletHit()
{
    for (var i=0; i<numChildren; i++)
    {
        if (getChildAt(i) is bullet)
        {
            var mc_bullet:MovieClip = getChildAt(i) as MovieClip;
            for (var j=0; j<numChildren; j++)
            {
                if (getChildAt(j) is alienbullet)
                {
                    var mc_alienbullet:MovieClip = getChildAt(j) as MovieClip;
                    if (mc_bullet.hitTestObject(mc_alienbullet))
                    {
                        var tobeDestroyed:MovieClip;
                        if (mc_bullet.x > mc_alienbullet.x)
                        {
                            tobeDestroyed = mc_bullet;
                        }
                        else
                        {
                            tobeDestroyed = mc_alienbullet;

                        }
                        /*score++;
                        tb_score.text = "" + score;*/
                        var bomb_mc:explosion = new explosion();
                        bomb_mc.x = tobeDestroyed.x;
                        bomb_mc.y = tobeDestroyed.y;

                        tobeDestroyed.destroy();
                        addChild(bomb_mc);
                        /*if (mc_bullet.hitTestObject(mc_alienbullet))
                        {
                        score++;
                        mc_bullet.gotoAndPlay(2);
                        }*/
                        score++;

                        return;
                    }
                    tb_score.text = "" + score;
                }
            }
        }
    }
}
//mc_gun would make the bullet fire only if the gun is clicked. 
//the game is to difficult so i changed it to stage. to make the whole stage the fireing area.
stage.addEventListener(MouseEvent.CLICK, fire);

function fire(event:MouseEvent):void
{
    var bulletfire:bullet = new bullet();
    bulletfire.x = mc_gun.x - 20 + mc_gun.width;
    bulletfire.y = mc_gun.y;
    addChild(bulletfire);
}

mc_banner.addEventListener(MouseEvent.CLICK, startGo);

function startGo(event:MouseEvent):void
{
    mc_banner.visible = false;
    mc_instructions.visible = false;
    mc_highscore.visible = false;
    mc_gun.visible = true;
    tb_score.visible = true;
    addEventListener(Event.ENTER_FRAME, pulse);
    var initialspaceship:alien=new alien();

    initialspaceship.x = 600;
    initialspaceship.y=Math.random()*(stage.stageHeight);
    addChild(initialspaceship);

}

function endGo()
{
    mc_gun.visible = false;
    tb_score.visible = false;
    removeEventListener(Event.ENTER_FRAME, pulse);
    mc_banner.visible = true;
    livesLeft--;
    lifesArray[livesLeft].visible = false;
} 
stop();
var得分:数字=0;
变量livesLeft:Number=3;
var-liferarray:Array=newarray();
liferarray.push(life1);
liferarray.push(life2);
liferarray.push(life3);
addEventListener(事件输入帧、脉冲);
功能脉冲(事件:事件):无效
{
检查HITONGUN();
checkForHitOnAliens();
检查BulletOnBulletHit();
//CheckEndGame条件
}
函数checkForHitOnGun()
{
对于(var i=0;i您的
engGo()
函数是负责减少生命的函数。
这是检查玩家是否永久死亡(0条生命)并结束游戏的最佳场所。
正如我理解阅读您的代码一样,
livesLeft
是存储玩家生命的地方。
liferaray[livesLeft].visible=false;
之后添加以下内容:

if (livesLeft <= 0)
{
    defeat();
}
否则(你需要在一个框架内完成所有工作),你需要清理舞台,并在舞台上添加新的内容

function defeat():void
{
    // First, remove ALL listeners
    removeEventListener(Event.ENTER_FRAME, pulse);
    stage.removeEventListener(MouseEvent.CLICK, fire);

    // Then, we remove everything
    removeChild(...);

    // Finally, you add the new stuff
    addChild(...);
}

如果您使用第二种方法,我建议您将函数解耦,并将它们封装在类中,这样您的游戏逻辑将全部放在一个地方(在一个类中),您的高分屏幕将出现在另一个屏幕上,您可以轻松地交换它们。

您尝试过吗?您在哪里跌倒了?什么是对的,什么是错的?请更具体地说明您的问题,因为您的问题很广泛…@NemoStein我已经开始工作了,如果玩家获得了三条生命,那么当生命全部消失时,会发生什么我会申请结束游戏并清除屏幕上的所有元素,然后这些元素会被带到一个新屏幕,显示输入名称以添加到高分表中。我编辑了代码以显示我的生活,但我认为这是错误的?你说要在endGo中添加生活部分,我会怎么做…?我非常感谢你的帮助。如果我愿意,你能帮我吗要创建生命吗?我理解函数失败,但不知道如何创建生命lifes@DP187,你说的“创建生命”是什么意思?如果你看一下我编辑的代码,那么函数checkForHitOnGun()当玩家被击中时会导致失去一条生命。但是我相信我做得不对…我如何给玩家三条生命,一旦生命在屏幕上出现,就会进入一个页面,用户可以输入他们的名字,并保存到高分表中。我理解你给我的编码。一旦用户失去所有三条生命,就会运行这三条生命fes…我的问题只是如何在三个生命中编码,我将把编码放在哪里?谢谢。今晚的任何帮助都将非常感谢,请和谢谢
function defeat():void
{
    // First, remove ALL listeners
    removeEventListener(Event.ENTER_FRAME, pulse);
    stage.removeEventListener(MouseEvent.CLICK, fire);

    // Then, we remove everything
    removeChild(...);

    // Finally, you add the new stuff
    addChild(...);
}