Question

I am trying to code a script in which a movieclip drops a rope and catches fishes that follows it up if it touches it. here is the issue , i am using hitTestObject to detect collision . Ofcourse the problem is that i trigger the function when it touches but as soon as it doesnt touch the function for moving the movie starts so basically the fish goes up for few seconds and then starts moving straight again .

To try and fix that i tried to make a boolean variable which changes to true or false according to hit and accordingly makes the movieclip moves but also doesnt work because as soon as one mc is not touching the other it changes from true to false or 1 to 0 ..tried both (as in with boolean variable and Number variable) . Any help or putting me on the right direction would be highly appreciated . Thank you so much

// fish capture code
this.addEventListener(Event.ENTER_FRAME,handleCollision);

function handleCollision(e:Event):void
{

    if (ropeHit.hitTestObject(fishy_1_a))
    {
        stopFish1();
        trace(movefish1);
    }
    else
    {
        moveFish1();
    }
}



//code enemy fishy 

//fish 1 A
    function moveFish1()
    {
        if (fishy_1_a.x < 800)
        {
            fishy_1_a.x +=  xSpeed;
        }
        else if (fishy_1_a.x >= 800)
        {
            fishy_1_a.x = -100;
        }
    }

    function stopFish1()
    {
        fishy_1_a.y -=  xSpeed;
    }
Was it helpful?

Solution

Define some flag, that you can test:

function handleCollision(e:Event):void {
    //Check if fishy is caught
    if (!fishy_1_a.catched && ropeHit.hitTestObject(fishy_1_a)) {
        //Change flag
        fishy_1_a.catched = true;
        trace("Gotcha!");
    } 

    if (fishy_1_a.catched) {
        stopFish1();
    }else {
        moveFish1();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top