Help with keyboard input

Hi, I’m a beginner to playcanvas and my code isn’t working. i’m trying to get a entity to disappear when i press the arrow keys. When i try this it just says on the launch page “Uncaught TypeError: Cannot read property ‘keyboard’ of undefined”. How do i fix this? Here is my code:

var PlayerSwapTyler = pc.createScript(‘playerSwapTyler’);
var keyboard = new pc.Keyboard(window);

PlayerSwapTyler.prototype.initialize = function() {
this.entity.findByName(“TYLER 10,000 coins”).enabled = false;
};
PlayerSwapTyler.prototype.update = function(dt) {
console.log(this.entity.findByName(“TYLER 10,000 coins”));
this.entity.findByName(“TYLER 10,000 coins”).enabled = false;
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown,this);
this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp,this);
};
if (this.app.keyboard.isPressed(pc.KEY_DOWN)) {
this.entity.findByName(“TYLER 10,000 coins”).enabled = true;
}

Hey @Charlotte_Darracq, and welcome to PlayCanvas!

There are actually quite a few things you shouldn’t be doing in the code you’ve posted. Never initialize events like this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown,this); in the update function. That’s what the init function is for. Unless you really need the console.log in the update function, remove it, because it will unnecessarily slow down the browser.

The following is how you would want to disable an entity on the press of the down arrow key.

 PlayerSwapTyler.prototype.update = function(dt) {
      if (this.app.keyboard.isPressed(pc.KEY_DOWN)) {
         this.entity.findByName(“TYLER 10,000 coins”).enabled = false;
      }
};

Hope this helps!

P.S - Try to use code highlighters while posting code(Enclose code in ```)

Thanks @DevilZ

I changed my code but it still doesn’t work. Do you have any more suggestions? (sorry i can’t figure out the code highlighter)

var PlayerSwapTyler = pc.createScript('playerSwapTyler');
var keyboard = new pc.Keyboard(window);


PlayerSwapTyler.prototype.initialize = function() {
    this.entity.findByName("TYLER 10,000 coins").enabled = false;
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown,this);
this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp,this);
};
PlayerSwapTyler.prototype.update = function(dt) {    
      if (this.app.keyboard.wasPressed(pc.KEY_DOWN)) {
         this.entity.findByName("TYLER 10,000 coins").enabled = true;
      }
};  

by the way i’m trying to make the TYLER 10,000 coins entity show when i press the arrow keys. I put the wrong thing in the first post.

Hi @Charlotte_Darracq,

If you are trying to find your TYLER 10,000 coins entity in your project hierarchy and not as a child in the script running entity try doing this:

this.app.root.findByName("TYLER 10,000 coins").enabled = true;

Do i need to use the var keyboard = new pc.Keyboard(window); script in the front or does that not do anything?

Thank you for your help

No, you don’t have to do that, Playcanvas will do that automatically when the app launches.

Its still not working so i’ll post a link to my project so you can take a look. The one i’m working on right now is the scene called shop and the code is called “player_swap_tyler.js” Here is the linkhttps://playcanvas.com/project/719828/overview/coins–and-

Ah good, so the reason it wasn’t working is because you had it attached to the entity you wanted to disable.

Change the script to:

var PlayerSwapTyler = pc.createScript('playerSwapTyler');

PlayerSwapTyler.prototype.initialize = function() {
    this.app.root.findByName("TYLER 10,000 coins").enabled = false;

};
      
 
PlayerSwapTyler.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_DOWN)) {
         this.app.root.findByName("TYLER 10,000 coins").enabled = true;
    }
};

And remove it from your entity and attach it to the top hierarchy entity, Root:

image

Now when pressing down it will enable the entity.

1 Like

Hi @Leonidas
That fixed it perfectly!
Thanks for your help

1 Like