Initialize the framework - Game development 编辑
This is the first of 16 tutorials to learn how to use Gamedev Phaser. After completing this tutorial you can find the source code for this section at Gamedev-Phaser-Content-Kit/demos/lesson01.html.
Before we can start writing the game's functionality, we need to create a basic structure to render the game inside. This can be done using HTML — the Phaser framework will generate the required <canvas>
element.
The game's HTML
The HTML document structure is quite simple, as the game will be rendered entirely on the <canvas>
element generated by the framework. Using your favourite text editor, create a new HTML document, save it as index.html
, in a sensible location, and add the following code to it:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Gamedev Phaser Workshop - lesson 01: Initialize the framework</title>
<style>* { padding: 0; margin: 0; }</style>
<script src="js/phaser.min.js"></script>
</head>
<body>
<script>
var game = new Phaser.Game(480, 320, Phaser.CANVAS, null, {
preload: preload, create: create, update: update
});
function preload() {}
function create() {}
function update() {}
</script>
</body>
</html>
Downloading the Phaser code
Next, we need to go through the process of downloading the Phaser source code and applying it to our HTML document. This tutorial uses Phaser V2 — it won't work with the current version on Phaser (V3). The V2 library is still available on the Phaser download page, below the links for the V3 download.
- Go to the Phaser download page.
- Choose an option that suits you best — I would recommend the min.js option as it keeps the source code smaller, and you are unlikely need to go through the source code anyway.
- Save the Phaser code inside a
/js
directory in the same location as yourindex.html
file. - Update the
src
value of the first<script>
element as shown above.
Walking through what we have so far
At this point we have a charset
defined, <title>
and some basic CSS in the header to reset the default margin
and padding
. We also have a <script>
element to apply the Phaser source code to the page. The body contains a second <script>
element, where we will write the JavaScript code to render the game and control it.
The <canvas>
element is generated automatically by the framework. We are initializing it by creating a new Phaser.Game
object and assigning it to the game variable. The parameters are:
- The width and height to set the
<canvas>
to. - The rendering method. The three options are
AUTO
,CANVAS
andWEBGL
. We can set one of the latter two explicitly or useAUTO
to let Phaser decide which one to use. It usually uses WebGL if available in the browser, falling back to Canvas 2D if not. - The
id
of the<canvas>
to use for rendering if one already exists on the page (we've specified null because we want Phaser to create its own.) - The names to use for Phaser's three key functions that load and start the game, and update the game loop on every frame; we will use the same names to keep it clean.
preload
takes care of preloading the assetscreate
is executed once when everything is loaded and readyupdate
is executed on every frame.
Compare your code
Here's the full source code of the first lesson, running live in a JSFiddle:
Next steps
Now we've set up the basic HTML and learned a bit about Phaser initialization, let's continue to the second lesson and learn about scaling.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论