Gravix

Call a function in Phaser from external HTML

Recommended Posts

Hello, I'm fairly new to development with Phaser (TypeScript).

I've made a simple login screen in HTML, and I'm trying to make the "Log In" button which is in the external HTML call a function in my Phaser game.

This is the button that will call the function:

<button onclick="login()">Log In</button>

 

Where in my project should I write the function "login", and how can I call it when I click the button? Any help would be greatly appreciated. 😃

I'm using this as a base for my project (I purchased his book): https://github.com/code0wl/Multiplayer-Phaser-game

Share this post


Link to post
Share on other sites

I've created the button via DOM in Phaser/Typescript.

In theory, you can put your function "login" wherever you want... but it have to exist BEFORE the browser see "<button onclick="login()">Log In</button>".

That's why I've followed the DOM solution.

Share this post


Link to post
Share on other sites

As Fenoliù said
You can call JS functions everywhere in Phaser if they were defined global and executed before your phaser game runs.

if you got a Phaser.Button and have a click event on it, just call a method like:

 

myButtonClickMethod() {

  login();
}

if your "login" function is available, it will execute it.

greetings.

Share this post


Link to post
Share on other sites

Thanks for the help, appreciate it! This has been resolved.

I forgot that I could just use the click events... Oops.

My solution was quite simple, in case anyone else has the same question:

this.loginButton = document.getElementById('login-btn') as HTMLLinkElement;
this.loginButton.addEventListener('click', (e) => this.login(e));

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.