Jump to content

Question about creating and loading Phaser Plugins with Typescript.


Daniel Belohlavek
 Share

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!

Link to comment
Share on other sites

  • 8 months later...

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

http://www.html5gamedevs.com/topic/10016-how-does-one-use-plugins-in-typescript/?p=97735

 

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

J.

Link to comment
Share on other sites

  • 3 months later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...