0

Hey guys so I ran into a Bug with my code I have a Movie Clip Object called mainFish that is setup with the array aFishArray and I have the array setup in a for loop to check a HitTest with playerHook. Now everything works completely fine but the issue that I am having is when say two of the Fish hit the hook at the same time one of of the fish hooks on the hook and the other just stays on the screen and i run into my sound Object repeating over again and other errors. Here is my functions that I use to track the fish:

Here is my functions inside the ENTER_FRAME Game Loop:

//Check if fish is null then run function 
        if (mainFish == null)
        {
            checkPlayerHitFish();

        }else
        {
         trackFish();
        }

and the functions:

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

                mainFish = currentFish;
                //Stop the fish from moving
                currentFish.stopFish();

                //fishing reel sound
                fishingReelSoundChannel;
                fishReelSound = new fishingReel();
                fishingReelSoundChannel = fishReelSound.play(0, 9999);

                fishingReelBoolean = true;



            }

        }

    }

and the trackFish Function:

private function trackFish():void 
    {
        mainFish.x = playerHook.x;
        mainFish.y = playerHook.y + 15;

    }

Can anyone see if I am doing anything wrong or how to go about fixing this issue?

1 Answer 1

1

The problem is that while you're only allowing one fish to be moved by the hook, you're still checking for hits for all the fish. This both stops them, and creates a new sound file.

The way to avoid this is to stop checking for fish collisions after a fish has already been hooked. To do this you can break out of the loop once you hook a fish:

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

            mainFish = currentFish;
            //Stop the fish from moving
            currentFish.stopFish();

            //fishing reel sound
            fishingReelSoundChannel;
            fishReelSound = new fishingReel();
            fishingReelSoundChannel = fishReelSound.play(0, 9999);

            fishingReelBoolean = true;

            //break out of the loop
            break;
        }
    }
}

Notice the break near the bottom. This will cause you to "break" out of the loop, preventing any further iterations. That way it will stop checking for collisions after the first successful hit test, and only react to one fish.

1
  • Yup. I'm an idiot. I completely forgot about breaking for further iterations. I was doing that on my last Project. Thank you so much for this. I appreciate your help!
    – Nathan
    Feb 14, 2015 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.