Fot the next few days I would like to have some fun with phaser ce, and make some plug-in examples. In this post I will be covering plug-ins that create the beginnings of a simple runner game. I hope to make a few posts like this where I start writing about how to go about making something that is starting to look like an actual game, rather than just simple demos that do not do much of anything interesting.
This is a post on a phaser ce plug in-in that I made to serve as an example on how to make phaser plug ins to help keep things more organized as I start to make more complex projects in phaser. This is not a getting started post on phaser ce so if you are new to phaser you might want to start with my getting started post first. I am also using a lot of other features in phaser that I am not covering in detail in this post at least however I will link to other relevant posts where needed. I also wrote a post on plugins in general as well that might be of interest as well.
So for this example I made two plugins one for the runner sprite, and another for platforms that the runner can jump on to from the floor.
So I start off making the plugin by having everything in a function literal. When I use this plug in I will call this function in the create method of a state that makes use of it. At the top of the function I started off my making a createGuySheet helper that will create a simple box sprite sheet that will be used to represent the guy.
For this helper I am using a canvas solution for making a simple sprite sheet for the sake of this demo, for more info on using canvas to make sprite sheets check out my post on the subject.
Here I have a method that will create the guy sprite when called. In here I am setting up physics values, such as gravity. In addition I am also attaching a guyJump event handler for when the screen is clicked or pressed, or if the up arrow on a keyboard is pressed.
This the hander that is attached to the onDown signal of the keyboard cursor keys instance, and also to the onDown of game.input as well so that it will fire each time the screen is pressed, or clicked, and if the keyboard up arrow button is pressed on desktop systems.
Here I am creating the actual plug-in object, and creating an init method that will be called once when the plugin is used in a project. Once I have everything set up I just need to add the plugin object to the game that will happen once the main factory function plugin_runner is called.
Now that I have my runner plug-in I can then create additional plug-ins that can also work on there own, or add additional functionality to my runner plug-in. In this example I will be writing about a platforms plug in that will define the logic for a pool of platforms that are place at the far right of the game world off screen, and then move across the screen to provide something that the running guy can jump onto.
Just like the runner plugin I start off by defining a factory function that will contain everything that will be used in the body of the plugin.
After that I again start off with a way to make a sprite sheet to be used with the plug-in. If I where to continue developing this I might make it so I can pass a key for an external sprite sheet, but for this example just a simple canvas solution will get the job done.
Here I have a method that will create a pool of platform sprites by making a group, and using the group.create method to create a bunch of platform sprites. I then also use the Sprite.kill method to place the platforms in a dead state to begin with, I will then use the Sprite.revive method to revive the platform sprites and place them into position as needed. I also enable physics for each platform, and set up some physics values for collision and to make sure that they do not move when the guy jumps onto them.
Here I have a method that will be called on each frame tick from with the plugin. I could just place the code in the update method for the plugin, but I prefer to pull code into methods like this to make the project more fine grain for the purpose of making this easier to follow.
Here platform sprites will be revived every once in a while depending on the amount of distance that has passed sense the last platform was revived. I also update all alive platforms as will with group.forEachAlive, and while doing so also kill old platforms that have went off screen making them available to be revived again.
Here Once again I tie everything together when making the plugin object that will be added to the game when this factory function is called.
I also have an update method for this plugin as well where I check for collision with the platforms pool, and the guy if the guy is present. I feature test for the guy to make it so this plugin can be used by itself.
So this was a fun quick little project, when I use the plug-ins I do get what it is that I would expect. The platforms move from the right to the left, and I can get the little guy sprite to jump up onto them. I am interested in making more plug-ins in which I explore other ideas for games as well. If you would like for me to get some more work done on these, and maybe have a playable demo on the page please let me know in the comments. If no one expresses interest I will not have much desire to continue developing these plug-ins into somewhat that starts to resemble an actual game.