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.

  1. Go to the Phaser download page.
  2. 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.
  3. Save the Phaser code inside a /js directory in the same location as your index.html file.
  4. 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 and WEBGL. We can set one of the latter two explicitly or use AUTO 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 assets
    • create is executed once when everything is loaded and ready
    • update 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:123 次

字数:6324

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文