Frage

hey everyone so I have a function called checkPlayerHitFish(); that controls the hitTest when ever the fish come in contact with the playerHook.

There are multiple fish that are added to the stage and controlled by a timer.

Here is the code for the function :

stage.addEventListener(Event.ENTER_FRAME, gameLoop);

private function gameLoop(e:Event):void 
    {
        checkPlayerHitFish();
        checkFishHitBucket();
        checkMainFishOffScreen();
    }


private function checkPlayerHitFish():void 
    {


        //loop through all our fishes
            for (var j:int = 0; j < aFishArray.length; j++)
            {
                //get current fish in j loop
                var currentFish:mcMainFish = aFishArray[j];

                //test if current fish is hitting current playerhook
                if (currentFish.hitTestObject(playerHook))
                {

                    trace("hit initialized");
                    currentFish.stopFish();
                    currentFish.x = playerHook.x;
                    currentFish.y = playerHook.y;




                }
            }

    }

as you can see in the if statement once the fish hits the hook then its position changes and the fish is now attached to the hook and following it. This is exactly what I wanted it to do.

Now what I am having trouble with is once one of the fish is attached to the hook I dont want anymore fish to be allowed to hitTest with the hook. So basically i want the hitTest to stop once it already has one fish attached to it.

I cant seem to figure this out i was thinking maybe adding a boolean or removing the eventListener but when ever i add removeEventlistener inside the functions if statement it works but it stops the fish from being attached to the hook. So it does pick up the fish it hit but then it just stops being attached to it and doesnt follow it anymore.

Any idea on how to do this so i can pick up one fish and then stop all the other hitTest with the other fishes from happening. I want this to happen because i want the user to be able to only pick up one fish at a time and then drop them off in a bucket then once dropped off be able to go back and pick up another fish.

War es hilfreich?

Lösung

The simplest solution is, as you say, a boolean variable to indicate if a fish is caught, but you also need a persistent reference to the caught fish, and rather than also have a boolean, you can simply check if this reference is null or not:

At the very top add this:

var caughtFish:mcMainFish;//This will be 'null' at first, but that's what you need.

Then, amend your game loop function:

private function gameLoop(e:Event):void 
    {
        if(caughtFish == null)
            checkPlayerHitFish();
        else
            trackFish();//We'll make this function in a moment

    checkFishHitBucket();
    checkMainFishOffScreen();
}

This way, the program will only run the loop to check if a fish is caught if none are currently caught. Now, you can amend the condition inside checkPlayerHitFish:

if (currentFish.hitTestObject(playerHook))
{

    caughtFish = currentFish;//Make caughtFish point to this fish
    currentFish.stopFish();//Not sure what this function does?

}

...finally, you can add a new function, trackFish(), which controls the position of the fish if, and only if, caughtFish is not null - since we just made it point to currentFish, it is no longer null!

function trackFish(){
    caughtFish.x = playerHook.x;
    caughtFish.y = playerHook.y;
}

This will also allow you to remove the listener - it broke it before because there was no persistent reference to a caught fish (caughtFish), so every frame it would test for a collision again. Once the player catches a fish, it no longer needs to check for collisions.

When the player has the fish in the bucket, you can simply add this statement:

caughtFish = null;

...and this will set the program to look for collisions again.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top