0

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?

  • Please elaborate on "*how to keep a character*". Totally unclear, what are you trying to achieve. – Organis Nov 09 '22 at 18:23
  • It means only when a character touches a Jelly – Isaac Yeap Jie Ling Nov 10 '22 at 08:08
  • Still, not clear. Please don't assume we have **any** idea what you are doing there. Make it a short and comprehensible story about how this **should** work. – Organis Nov 10 '22 at 20:20
  • I tested the one of the controller buttons to move a character, then once a character touches a Jelly, a character will be electrocuted. – Isaac Yeap Jie Ling Nov 11 '22 at 08:36
  • @IsaacYeapJieLing You say _"...Once a character touches a Jelly, a character will be electrocuted"_ okay so what is the problem? What is the current problem to solve by doing a _"...How to keep a character until it hits the jelly"_? Are you saying right now the problem is that the character **does not** keep? If yes then tell us also what does your _"keep"_ mean (it's not clear what you want to happen here)... – VC.One Nov 11 '22 at 12:02
  • I have filled every frame for each animation in a nested symbol. Once it touches a Jelly, it supposed to be electrocuted. e.g. character.gotoAndStop(7); – Isaac Yeap Jie Ling Nov 11 '22 at 14:34
  • I believe the problem lies within you not understanding the flow of events. The line **if (character.electrocuted.currentFrame == 30)** is executed immediately after **character.gotoAndStop(7);**, it does not wait for animation to end, so it does nothing, and the **character.gotoAndStop(1);** never happens. – Organis Nov 11 '22 at 21:22
  • Ok. So how do I fix this? – Isaac Yeap Jie Ling Nov 12 '22 at 00:17
  • You should actually wait for the animation to end. There are two ways you can approach this. One (I recommend this one), you change the paradigm, stop scripting in frames, create classes for all your entities and architecture to manage screens, game modes, game objects, and so on. The character will get states and will dispatch events like OPEN and CLOSED for the gameplay to know, when to wait. Other, you just code the waiting routine as is. The downside, there's a limit how far you can go and your project can grow. I advise avoiding frame scripting for anything but the smallest experiments. – Organis Nov 12 '22 at 11:04
  • Any examples to solve from scripting in frames? – Isaac Yeap Jie Ling Nov 13 '22 at 01:10

1 Answers1

0

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);
    }
}
Organis
  • 7,243
  • 2
  • 12
  • 14