HitTestObject between a Jelly and a character

55 views Asked by At

I have a code for a character and a Jelly.

Character:

up_btn.addEventListener(MouseEvent.CLICK, function(){
    character.y-=10;
})
down_btn.addEventListener(MouseEvent.CLICK, function(){
    character.y+=10;
})
left_btn.addEventListener(MouseEvent.CLICK, function(){
    character.x+=10;
})
right_btn.addEventListener(MouseEvent.CLICK, function(){
    character.x-=10;
})

Jelly:

var JellyHitted:Boolean;
function hitJelly(e: Event):void {
    if (character.hitTestObject(Jelly1)) {
        JellyHitted = true;
        character.gotoAndStop(7);
        if (character.electrocuted.currentFrame == 30) {
            character.gotoAndStop(1);
        }
    }
    else{
        JellyHitted = false;
    }

Jelly Test Any ideas how to keep a character until it hits the jelly?

1

There are 1 answers

1
Organis On

Ok, I cannot test it, but the idea is the following:

function hitJelly(e:Event):void
{
    if (character.hitTestObject(Jelly1))
    {
        character.gotoAndStop(7);
        
        // Now you need to monitor the animation for a while.
        addEventListener(Event.ENTER_FRAME, onElectrcuted);
    }
}

// Electrocution handler.
function onElectrocuted(e:Event):void
{
    // I assume you want animation to end, rather then to reach a certain frame.
    if (character.electrocuted.currentFrame >= character.electrocuted.totalFrames)
    {
        // Stop monitoring.
        removeEventListener(Event.ENTER_FRAME, onElectrcuted);
        
        // Return to normal.
        character.gotoAndStop(1);
    }
}