Daniel Belohlavek

Question about creating and loading Phaser Plugins with Typescript.

Recommended Posts

So I'm fairly new to Phaser and there are some topics I still haven't learn how to manage with TS.


I saw some examples of the Isometric Plugin where Phaser plugins are loaded as follows:

preload: function () {        (...)        // Add and enable the plug-in.        game.plugins.add(new Phaser.Plugin.Isometric(game));        (...)}

But reading the docs I realized that the class Phaser.Game does not contain a property "plugins". So I kept searching and I found the Phaser.PluginManager class that does have a "plugins" property but seems to take weird extra parameter. My question is:


How can I create my own plugins and load them with typescript? I understand that my plugin class should extend Phaser.Plugin but I don't understand how to create an instance of the plugin in-game.


Thanks for reading!

Share this post

Link to post
Share on other sites

that code should work I think, but see my answer here



i've had to create a .d.ts file for any plugin i use, mapping its functions. 


eg for the ScreenShake plugin (https://github.com/dmaslov/phaser-screen-shake) the following to be sufficient for getting it to compile ok in Visual Studio Code. (I'm new to this so I'm not claiming it to be perfect, but it works to get it up and running)

declare module Phaser {        module Plugin {		         export class ScreenShake extends Phaser.Plugin {				     constructor(game:Phaser.Game, parent:any); // not sure what "parent" type should be.. can anybody help here?	     setup(obj:Object):void;	     shake(count:number):void;	}    }}  

then in my code I call

var ss = <Phaser.Plugin.ScreenShake> this.game.plugins.add(Phaser.Plugin.ScreenShake);ss.shake(10);

note the need for type casting from Phaser.Plugin to Phaser.Plugin.ScreenShake


hope this helps


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.

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.


  • Recently Browsing   0 members

    No registered users viewing this page.