Stop after pop up alert

Please help me, I want to make the character stop moving after the pop up appears. sorry because I’m a beginner.

https://playcanvas.com/project/730468/overview/character-move
this is my project

Hi @Unavailable and welcome,

Check the following UI example on adding UI buttons/popups and how to execute code as a response to a button click:

When the buttons is clicked one way to stop your player from moving, is to disable the playerMovement script:

this.app.root.findByName('Player').script.playerMovement.enabled = false;
1 Like

I still don’t understand. it was created in CollisionAlert.js right?

Hi @Unavailable!

You put it into the “onPress” function which only triggers when you press pc.KEY_SPACE.

CollisionAlert.prototype.onPress = function (event) { this.app.root.findByName('Player').script.playerMovement.enabled = false; };

Try putting the line into the onCollisionStart function :slight_smile:

1 Like

Yeah, this exactly, and most likely you also want
“this.app.root.findByName(‘Player’).script.playerMovement.enabled = true;” where you currently have it in the Space key trigger function so that they can continue walking after they make the popup go away.

1 Like

I tried it but after I pressed the space bar the character still moves, is there something wrong?

CollisionAlert.prototype.onPress = function (event) { 
    if(this.app.keyboard.isPressed(pc.KEY_SPACE)){
        this.app.root.findByName('Player').script.playerMovement.enabled = true;
    }
};

This part => enabled = true; means movement is enabled. If you wish to disable it try enabled = false;

The thought was: you might want to “release” the player with SPACE if he get’s stopped by the collisionbox.

1 Like

yes I want to make after pressing the space after the pop up character can move again. but after pressing the space character moves in place. and after pressing SPACEBAR again the character continues to move forward.

i think it doesn’t work

You have to bind the pc.EVENT_KEYDOWN event and not a specific key.

var CollisionAlert = pc.createScript('collisionAlert');
CollisionAlert.attributes.add('targetEntity', {type: 'entity'});
CollisionAlert.attributes.add('Player', {type: 'entity'});

// CollisionAlert.attributes.add('Player1', {type : 'entity'});

// // initialize code called once per entity
CollisionAlert.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);    
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onPress, this);
    
};

CollisionAlert.prototype.onCollisionStart = function(entity) {    
    var player = this.Player.name;
    var target = this.targetEntity.name;
    var playerCollision = this.entity.findByName('Player');
    if (entity.other.rigidbody) {
        this.app.root.findByName('Player').script.playerMovement.enabled = false;
        setTimeout(function() { alert("this " + player + " hit the " + target + " ,press space to close"); }, 200);
    }

};
     
CollisionAlert.prototype.onPress = function (event) {
    
    if(event.key === pc.KEY_SPACE) {
        console.log("SpacePress");
        this.app.root.findByName('Player').script.playerMovement.enabled = true;
    }
};

But this creates a new problem: You won’t get the SpaceKey event from the alert. So you have to press Space 2 times.

I would suggest instead of using alerts you could set up a simple 2D Screen to show your Text.

Can I disable other scripts like Player Movement? i tried to disable movementSound and playerAnimation with the same text it doesn’t work.

Sure you can. But you can’t access them with “script”:

        this.app.root.findByName('Player').script.playerMovement.enabled = false;

Try sound or animation instead. But first read up on how to use them right: