As we previously discussed, there are a few different ways to build the actual game. When building a DOM-only game, we wouldn’t drop out into another JS library. However, since we are interested in making a game that is pixel-manipulation-heavy, we’ll need to use a different path.
One way we can build an intense game is to talk directly to the <canvas> element. We can use primitives to load our images, sprites, animations, our own event loop, etc. etc.
Alternatively, we can use a game library that was purpose-built to power web games. Rather than need to handle all the dirty-work of building a game library and the game, we’ll use a free, open-source gaming library called phaser.
Phaser handles a LOT of the dirty-work for us and allows us to use a DSL (domain-specific language) to interact with our elements. It also allows us to interact directly with the <canvas> element in the cases where we need to drop down into the element itself (very rare).
Phaser takes over all of the tedious tasks, such as:
- game physics
- preloading assets
- sprite handling
- animation
- game input (keyboard/touch)
- sound handling
- game scaling
- etc.
Rather than building the interface to the DOM element, we can let Phaser do it’s job and we can focus on building our game.
INCREDIBLY simple introduction to Phaser
Phaser has some excellent documentation available on the web, so we won’t dive deeply into building the game with phaser. However, we will provide a basic introduction to phaser so you can get started right away.
Creating the game
Before we can actually build a game, we need to place the game on the page. There are a few different ways to do this. The simplest way to create a phaser game is by passing the game object when we create the game. This looks like:
1
2
3
4
5
6
7
8
9
10
11
|
var game =
new Phaser.Game(
800, 600, // width x height
Phaser.AUTO, // the game context, 2D/3D
'game_canvas', // id of the DOM element to add the game
{
preload: function() {},
create: function() {},
update: function() {}
}
)
|
In the code, we’re creating a game by passing a few arguments. The first two are the width and the height of the canvas we’re working on. The third argument is the type of context to create with the <canvas> element. If we pass Phaser.AUTO, phaser will take care of creating the element appropriate for the browser loading the game.
We use Phaser.AUTO pretty much always. It’s a good default.The fourth argument is a string id of the DOM element we want to append the <canvas> element. If we don’t pass an element id or we pass a blank one, phaser will attach it’s element to the <body> element.
Finally, the last argument is the game object. This object contains all of the game methods phaser needs to run the game. We’ll cover a few of the core game methods shortly.
This method of building objects is the absolute simplest method. It’s appropriate to build small games/samples with this type of game. On the other hand, anything more a bit more complex, we’ll likely need to break out into building our game in a different way.
Building the game with state objects
Rather than having a single game object, phaser allows us to add state objects where we can build our game in states. To build a state object-based game, we’ll remove the last argument and attach states to the phaser game, like so:
3
4
5
6
7
8
9
10
11
12
|
var game =
new Phaser.Game(
800, 600, // width x height
Phaser.AUTO, // the game context, 2D/3D
'game_canvas' // id of the DOM element to add the game
);
var menuState = {
preload: function() {},
create: function() {}
};
game.state.add('MainMenu', menuState);
game.state.start('MainMenu'); // start the MainMenu state
|
In this way, we can set up completely different states for our game with new objects. It’s a much better way to create complex games. We’ll use this method to create state objects.
Let’s build the MainMenu state. It will look something like this:
game functions
The preload() function is where we can load all of our game assets. Since we can’t really start playing a game until our images, sprites, sounds, etc. are loaded, we’ll need to load them. The preload() function is where we can do this. It’s the second method called on the state (after the init() function).
To preload an image, we’ll define it with a name key and the url to load the asset:
- function preload() {
- game.load.image('hero', 'assets/player_blue.png');
- };
- function preload() {
- game.load.spritesheet('explosion', 'explode.png', 128, 128, 16);
- };
In this case, the explosion spritesheet (the name of the asset registered with the phaser game) contains 16 different images, each at 128×128.
For reference, the explode.png file looks like:
This explode.png is available as part of the phaser example repo.To load a audio:
- function preload() {
- game.load.audio('dink', 'assets/dink.mp3');
- };
To load a bitmap fonts:
- function preload() {
- game.load.bitmapFont('architectsDaughter',
- 'assets/fonts/r.png',
- 'assets/fonts/r.fnt');
- };
- function preload() {
- game.load.script('webfont',
- '//ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js');
- };
The create() function is where we’ll actually go ahead and start placing these examples on the page. This is where we’ll set up our assets, place the sprites on the page, load animations, etc. Let’s start with our scrolling background of the MainMenu state.
The menu background looks like it’s moving up and to the right as though it’s a starfield
Phaser gives us an easy way to create this effect by setting a property called
autoScroll. First, we need to place the menu on the screen. We can do this pretty simply:- function create() {
- var world = game.world;
- var background = game.tile.tileSprite(0, 0,
- world.width, world.height, 'menu_background');
- }
To set up the autoscrolling, we’ll call the phaser method autoScroll() on the background image and give it two coordinates, the first one is the amount to scroll on the x axis and the second on the y axis:
- function create() {
- var background = game.tile.tileSprite(0, 0,
- world.width, world.height, 'menu_background');
- background.autoScroll(-50, -20);
- }
- function create() {
- // background
- var textPadding = 20;
- var sprite = game.add.sprite(world.centerX,
- world.centerY - textPadding, 'logo');
- }
- function create() {
- // background
- // logo
- var newGameBtn = game.add.text(world.centerX,
- world.centerY + textPadding, 'New game');
- }
3
4
5
6
7
8
9
|
function create() {
// background
// logo
var newGameBtn = game.add.text(world.centerX,
world.centerY + textPadding, 'New game', {
font: '16px Architects Daughter',
align: 'center',
fill: '#fff'
});
}
|
- function create() {
- // background
- // logo
- var newGameBtn = game.add.text(world.centerX,
- world.centerY + textPadding, 'New game');
- newGameBtn.inputEnabled = true;
- }
For instance:
- function create() {
- // background
- // logo
- var newGameBtn = game.add.text(world.centerX,
- world.centerY + textPadding, 'New game');
- newGameBtn.inputEnabled = true;
- // Add input handlers
- newGame.events.onInputOver.add(overNewgame, this);
- newGame.events.onInputOut.add(outNewgame, this);
- newGame.events.onInputDown.add(onNewgameDown, this);
- }
A tween is essentially an effect that takes the starting position, the ending postion, and a duration. It’s job is to try to smoothly set the appropriate element properties for the duration of time so that it ends up at the ending position by the end of the interval.
Adding a tween in phaser is incredibly easy to do. In our overNewgame function, let’s add a tween to the newGameBtn. This function will get called when the input is hovering over the button:
- function overNewgame() {
- game.add.tween(newGameBtn.scale)
- .to({
- x: 1.3,
- y: 1.3
- },
- 300,
- Phaser.Easing.Exponential.Out,
- true);
- };
The fifth parameter is the easing to use.We won’t go in-depth into them in this tutorial.
The final parameter is the autostart parameter. If this is set to true, then the tween will start automatically. If we don’t set this, then the tween won’t fire until we call start on the tween. Since this is a relatively simple one, we can simply set it up here and have it autostart.
Lastly, to handle the sound when the menu item is hovered over, we’ll call play on the audio element:
3
4
5
6
7
8
|
function overNewgame() {
game.add.tween(newGameBtn.scale)
.to({
x: 1.3,
y: 1.3
}, 300, Phaser.Easing.Exponential.Out, true);
dink.play(); // audio file
};
|
The update()function
The update function is the game loop function. It is called everytime the game itself needs to update. We don’t need it for the MainMenu state, but we will for the game loop.
To see the entire MainMenu state, check out this gist: http://j.mp/13y6H2z
Source: newsletter
If you feel useful for you and for everyone, please share it!
Suggest for you:



No comments:
Post a Comment