0

嘿伙计们,所以基本上我想创建多个级别,我设置了如下所示的级别,但由于某种原因,level_2 的 HittestObject 没有启动。我无法确定我做错了什么。级别 2 已启动,但当我试图击中你时,它就直接通过了。

我有一个Goal_1 和一个goal_2,我想和玩家一起击球。

我想有多个级别的goal_1和多个级别的goal_2等等

这是代码:

public class keepFocusEngine extends MovieClip 
{


    //MultiTouch added for touch gestures
    Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;


    public var btnPressedUp:Boolean;
    public var btnPressedDown:Boolean;
    public var btnShootPlayer:Boolean;

    public var btnUp:MovieClip;
    public var btnDown:MovieClip;
    public var shootPlayer:MovieClip;

    private var speed:Number = 6.0;
    public var vx:Number = 0;
    public var vy:Number = 0;
    private var friction:Number = 0.93;
    private var maxspeed:Number = 12;


    public var player:mPlayer;
    public var mGoal_1:goal_1;
    public var mGoal_2:goal_2;

    public var playerLives:Number;
    public var livesText:TextField;

    public var highScoreText:TextField;
    public var nScore:Number;

    public var levelText:TextField;
    public var nLevel:Number;

    public var menuEnd:mcEndGameScreen;
    public var menuStart:mcStartGameScreen;






    public function keepFocusEngine() 
    {
        //Initialize variable to false
        btnDown.visible = false;
        btnUp.visible = false;
        shootPlayer.visible = false;
        livesText.visible = false;
        highScoreText.visible = false;
        levelText.visible = false;



        //Create loader object
        var startGameLoader:Loader = new Loader();
        //Add event listener for complete event
        startGameLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startGameLoaded);
        //Load loader object
        startGameLoader.load(new URLRequest("StartGameScreen.swf"));

        //Create End game loader object
        var endGameLoader:Loader = new Loader();
        //Add event listener
        endGameLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, endGameLoaded);
        //Load end game object
        endGameLoader.load(new URLRequest("EndGameScreen.swf"));



    }

    private function startGameLoaded(e:Event):void 
    {
        //Get a refrece to the loaded movieclip
        menuStart = e.target.content as mcStartGameScreen;
        //Listen for start game event
        menuStart.addEventListener("START_GAME", playGameAgain);
        //Add start screen to the stage
        addChild(menuStart);
    }

    private function endGameLoaded(e:Event):void 
    {
        menuEnd = e.target.content as mcEndGameScreen;
        menuEnd.addEventListener("END_GAME", playGameAgain);
        addChild(menuEnd);
        menuEnd.hideEndScreen();
    }

    private function playGameAgain(e:Event):void 
    {



        //Add Hi Res Stats
        addChild(new Stats());

        //mainPlayerObject added to stage
        player = new mPlayer();
        stage.addChild(player);
        player.x = (stage.stageWidth / 2) - 280;
        player.y = (stage.stageHeight / 2);

        mGoal_1 = new goal_1();
        stage.addChild(mGoal_1);







        //Update Player lives and add 3 lives
        playerLives = 3;
        updatePlayerLives();


        //Update Level progression
        nLevel = 1;
        updatePlayerLevel();


        //Add score to texxt field
        nScore = 0;
        updateHighScore();

        //Initialize stage objects to true 
        btnDown.visible = true;
        btnUp.visible = true;
        shootPlayer.visible = true;
        livesText.visible = true;
        highScoreText.visible = true;
        levelText.visible = true;

        //Hide external screens
        menuEnd.hideEndScreen();
        menuStart.hideStartScreen();



        //setup Listeners when btn's are rolled over and out
        btnUp.addEventListener(TouchEvent.TOUCH_OVER, btnUpMouseOver, false, 0, true); 
        btnUp.addEventListener(TouchEvent.TOUCH_OUT, btnUpMouseOver, false, 0, true);
        btnDown.addEventListener(TouchEvent.TOUCH_OVER, btnDownMouseOver, false, 0, true); 
        btnDown.addEventListener(TouchEvent.TOUCH_OUT, btnDownMouseOver, false, 0, true);
        shootPlayer.addEventListener(TouchEvent.TOUCH_TAP, shootPlayerObject, false, 0, true);

        //setup game loop event listeners
        stage.addEventListener(Event.ENTER_FRAME, gameLoop);



        //To completely end game when back button pushed on android
        NativeApplication.nativeApplication.addEventListener( KeyboardEvent.KEY_DOWN, handleKeyDown );


    }

    private function gameLoop(e:Event):void 
    {
        playerShoot();
        playerControl();
        playerStageBoundaries();

        checkEndGameCondition();
        checkPlayerOffScreen();

        level_1();
    }


    private function level_1():void 
    {


        if(player.hitTestObject(mGoal_1))
           {

            trace("Goal_1 Collision");
            //Remove button for constant movement
            btnShootPlayer = false;
            mGoal_1.destroyGoal_1();
            player.destroyPlayer();
            //Update High Score text
            nScore += 10;
            updateHighScore();

            //Update level
            nLevel++;
            updatePlayerLevel();
            stage.removeEventListener(Event.ENTER_FRAME, gameLoop);
            level_2();




           }else
           {

               checkEndGameCondition();
           }
    }



    public function level_2():void 
    {
       stage.addEventListener(Event.ENTER_FRAME, gameLoop);
        trace("Level_2 Initiated");
        //Keep Text Scores initiated
        updateHighScore();
        updatePlayerLives();

        player = new mPlayer();
        stage.addChild(player);
        player.x = (stage.stageWidth / 2) - 280;
        player.y = (stage.stageHeight / 2);

        mGoal_1 = new goal_1();
        stage.addChild(mGoal_1);







        if (player.hitTestObject(mGoal_1))
        {
            trace("Level 2 Hit test works!");


            nScore += 10;
            updateHighScore();

        }



    }
4

1 回答 1

0

对不起,我花了这么长时间才回复你。

以下是如何设置关卡的示例...

游戏循环()

我只想在其中添加一个 switch 语句,让 gameLoop 知道每一帧更新哪个级别的函数。switch 语句就像一个 if...if else 条件,输入更少。switch 语句的输出取决于nLevel与可能情况的比较。因此,当nLevel等于 1 时,level_1()执行该函数。通过添加到nLevel我们可以然后更新level_2()等等。

    private function gameLoop(e:Event):void
    {
        playerShoot();
        playerControl();
        playerStageBoundaries();

        checkEndGameCondition();
        checkPlayerOffScreen();

        switch(nLevel) {
            case 1:
                level_1();
                break;
            case 2:
                level_2();
                break;
        }
    }


开始下一级()

此函数用于去除关卡函数中的重复代码,只需调用此函数即可重置玩家位置并更新分数和关卡。

    private function startNextLevel():void
    {
        nScore += 10;
        updateHighScore();

        nLevel++;
        updatePlayerLevel();

        updatePlayerLives();

        player.x = (stage.stageWidth / 2) - 280;
        player.y = (stage.stageHeight / 2);
    }


电平函数

这些是您的关卡功能,而不是将玩家添加到每个关卡的舞台,只需重用玩家并重置其位置。

    private function level_1():void
    {

        if (player.hitTestObject(mGoal_1))
        {

            trace("Goal_1 Collision");
            //Remove button for constant movement
            btnShootPlayer = false;
            mGoal_1.destroyGoal_1();
            player.destroyPlayer();

            startNextLevel();
        }
        else
        {
            checkEndGameCondition();
        }
    }

    private function level_2():void
    {
        //Keep Text Scores initiated


        if (player.hitTestObject(mGoal_2))
        {   
            mGoal_2.destroyGoal_2();
            player.destroyPlayer();

            btnShootPlayer = false;
            startNextLevel();
        } else {
            checkEndGameCondition();
        }

    }
于 2013-04-08T19:31:04.877 回答