Search code examples
actionscript-3flashdevelophittest

AS3 hitTestObject will not register


Hey guys so basically i want to create mulltiple levels and i set up the levels as shown below but for some reason the HittestObject for level_2 is not initiating. I cant pinpoint what i am doing wrong. Level 2 is initiated but when i try to hit thee obect it just goes right through.

i have a Goal_1 and a goal_2 that i want to hittes with the player.

i want to have multiple levels with the goal_1 and multiple levels with the goal_2 and so on

Here is the Code:

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();

        }



    }

Solution

  • Sorry it's taken so long for me to get back to you.

    Here is an example of how to set up your levels...

    gameLoop()

    I would just add a switch statement in there to let the gameLoop know which level function to update every frame. The switch statement is like an if...if else conditional with less typing. The output of the switch statement is decided by the comparison of nLevel with the possible cases. So, when nLevel equals 1, the level_1() function is executed. By adding to nLevel we can then update level_2() and so on.

        private function gameLoop(e:Event):void
        {
            playerShoot();
            playerControl();
            playerStageBoundaries();
    
            checkEndGameCondition();
            checkPlayerOffScreen();
    
            switch(nLevel) {
                case 1:
                    level_1();
                    break;
                case 2:
                    level_2();
                    break;
            }
        }
    


    startNextLevel()

    This function is used to remove the repetetive code in your level functions, just call this function and it will reset player position and update the score and level.

        private function startNextLevel():void
        {
            nScore += 10;
            updateHighScore();
    
            nLevel++;
            updatePlayerLevel();
    
            updatePlayerLives();
    
            player.x = (stage.stageWidth / 2) - 280;
            player.y = (stage.stageHeight / 2);
        }
    


    Level Functions

    These are your level functions, instead of adding the player to the stage each level, just reuse the player and reset it's position.

        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();
            }
    
        }