Button Call Event

https://www.youtube.com/watch?v=clUj8sUkHRs

I have completed the control of the animation according to this tutorial, but I can only use liang press key to control it. How to use a single button to control two animation states (close and open)

Here is my code. Do I need to rewrite a button control script now? What should I do

   var Door = pc.createScript('door');

type or paste code here

   Door.prototype.initialize = function() {

    this.state = 'closed';

    this.entity.anim.baseLayer.activeStateCurrentTime = 0;

    this.entity.anim.playing = false;

    this.entity.anim.on('door-opened', () => {

        this.state = 'opened';

        this.entity.anim.playing = false;

    });

    this.entity.anim.on('door-closed', () => {

        this.state = 'closed';

        this.entity.anim.playing = false;

    });

};

// update code called every frame

Door.prototype.update = function(dt) {

    if (this.app.keyboard.wasPressed(pc.KEY_A)) {

        if (this.state === 'opened' && !this.entity.anim.playing)

        {

            this.entity.anim.playing = true;

        }

    }

   

    if (this.app.keyboard.wasPressed(pc.KEY_S)) {

        if (this.state === 'closed' && !this.entity.anim.playing)

        {

            this.entity.anim.playing = true;

        }

    }

};

Hi @henry_tang!

Do you use the same animation?

As far I can see, the code you share is not the same as the code from the tutorial. You are missing the part where you reset the animation time (line 28 of the tutorial). Also line 3 here will give a problem.

Apart from that, I think you can call the code with the same key or button, because you check with a statement what is the current state.

1 Like

Hello, my effect is different from the video. At the beginning, I didn’t execute the animation, and the animation state was controlled by my A/S switch. Since there is no A/S when viewing on my phone, I need button control.

You will need to change it a bit so that you know what state the door is in when you press your button. If it’s closed, then open it. If it’s open then close it