actionscript-3flashflash-cs6

AS3 tryin to make a function with hitTestObject using if statement


i have a function with code

function ball3test()
{
    if (ball3.hitTestObject(ball1_t))
    {
        goodmc.alpha = 1;
        removeChild(ball3);
        sw = sw + 1;
        ball3t.alpha = 1;
        setTimeout(hidhap, 1000);
        ss = ss + 1;
        ssdone();
    }
    else
    {
        sadmc.alpha = 1;
        ball3.x = ball3X;
        ball3.y = ball3Y;
        setTimeout(hidsad, 1000);
        ssdone();
    }
}

now when i test it if ball3.hitTestObject(ball1_t) the good mc and sad mc show together .. it makes my crazy i don't know whats wrong when the hit test happens the goodmc and sadmc visiable togeher it should be goodmc only not both however when the hit test not happens the sadmc only that show


Solution

  • So, I edited your code into the readability. Always do that, format your code and align it properly: it increases the visibility and transparency of what's going on there tenfold.

    Then, the essence of your problem. I don't see any mistake within the provided code snippet, but I suspect this function is called several times concurrently, and that is why you see it as a mindblowing case of both statements under the conditional clause being executed.

    I advise to add the debugging traces to your code:

    import flash.utils.getTimer;
    
    function ball3test()
    {
        trace("A", getTimer());
        
        if (ball3.hitTestObject(ball1_t))
        {
            goodmc.alpha = 1;
            removeChild(ball3);
            sw = sw + 1;
            ball3t.alpha = 1;
            setTimeout(hidhap, 1000);
            ss = ss + 1;
            ssdone();
            
            trace("B", getTimer());
        }
        else
        {
            sadmc.alpha = 1;
            ball3.x = ball3X;
            ball3.y = ball3Y;
            setTimeout(hidsad, 1000);
            ssdone();
            
            trace("C", getTimer());
        }
    }
    

    Then, upon executing, you will probably see something like:

    A xxx
    B xxy
    A zzz
    C zzq
    

    If I am right, then you will need to figure, why your function is called after it already had been (which is probably by design) and then to seek the means to prevent it.