0

我在这里遇到问题,因为我试图在屏幕上显示祝贺消息时删除矩形的任何实例。代码有点乱,但我一直在弄乱它来试图解决这个问题。如果我将其更改addChildstage.addChild这会给我由于在shootMob函数中拍摄和删除它们而导致的错误。我也将其称为,rectangle.parent.removeChild(rectange)但这也行不通。有任何想法吗?

function spawnMob1():void  
{  
    if (gameLevel==1)
    {
    var rectangle:MovieClip = new ball_mc();
        rectangle.x = randomRange(583);
    rectangle.y = randomRange(100);
    addChild(rectangle);

    var mobMoveX:Tween = new Tween(rectangle, "x", None.easeOut, rectangle.x, character_mc.x, 6, true);
    var mobMoveY:Tween = new Tween(rectangle, "y", None.easeOut, rectangle.y, character_mc.y, 6, true);


    rectangle.visible = true;

    rectangle.addEventListener(MouseEvent.CLICK, shootMob);
    function shootMob(e:MouseEvent): void
    {
        if (contains(rectangle)) 
        {
            removeChild(rectangle);
            score_txt.text = String(score);
        }
        score+=20;
        trace("Score:" + score);
    }

        if (score>=60)
        {
            trace("Congratulations!");
            congratulations1_mc.visible = true;

            trace("GameLevel:" + gameLevel);


            removeChild(rectangle);
//          rectangle.visible = false;

            gameLevel = 0;

        }


    character_mc.addEventListener(Event.ENTER_FRAME, charHit);

    function charHit(event:Event): void
    {
        if (rectangle.hitTestObject(character_mc))
        {
            if (contains(rectangle)) 
                        {
                       removeChild(rectangle);
            if (health>0)
            {
                health-=5;
                health_txt.text = String(health);
                trace("boop");
                trace("Health:" + health);
            } else if (health<=0) {
                    trace("You have died");
                    death_mc.visible = true;
                    gameLevel=0;

            }
    }

}
}
4

2 回答 2

0

你放错了一个}. shootMob应该包括(score>=60)块。

于 2013-05-23T19:47:30.380 回答
0
Writing one function inside another function is a bad coding style. What about this instead:

    import flash.events.MouseEvent;
    import flash.events.Event;
    import fl.transitions.Tween;
    import flash.text.TextField;
    import flash.display.MovieClip;

 var score_txt:TextField = new TextField(50,50,0xFFFFF);
    var score:Number;
    var gameLevel:int = 1;

 var rectangle:MovieClip;

    //function spawnMob1():void  
    //{  
        if (gameLevel==1)
        {
   rectangle = new ball_mc()

    rectangle.x =  randomRange(583);
    rectangle.y =  randomRange(100);
    addChild(rectangle);

    var mobMoveX:Tween = new Tween(rectangle,"x",None.easeOut,rectangle.x,character_mc.x,6,true);
    var mobMoveY:Tween = new Tween(rectangle,"y",None.easeOut,rectangle.y,character_mc.y,6,true);
     rectangle.visible = true;

    rectangle.addEventListener(MouseEvent.CLICK, shootMob);

}

    function shootMob(e:MouseEvent):void
    {
        if (contains(rectangle))
        {
            removeChild(rectangle);
            score_txt.text = String(score);
        }
        score +=  20;
        trace("Score:" + score);

    if (score>=60)
    {
        trace("Congratulations!");
        congratulations1_mc.visible = true;
        trace("GameLevel:" + gameLevel);
        if(rectangle || congratulations1_mc.visible == true)
        removeChild(rectangle);
        gameLevel = 0;
    }
}

    character_mc.addEventListener(Event.ENTER_FRAME, charHit);

    function charHit(event:Event):void
    {
        if (rectangle.hitTestObject(character_mc))
        {
            if (contains(rectangle))
            {
                removeChild(rectangle);
                if (health>0)
                {
                    health -=  5;
                    health_txt.text = String(health);
                    trace("boop");
                    trace("Health:" + health);
                }
                else if (health<=0)
                {
                    trace("You have died");
                    death_mc.visible = true;
                    gameLevel = 0;

                }
            }
        }
    }
于 2013-05-24T06:33:35.467 回答