Creating a Simple Windows 8 Game with JavaScript Part 3

Gauloran

Kıdemli Moderatör
7 Tem 2013
8,119
599
local
Starting the Game

[FONT=&quot]
To start the game, we need to know when the page is ready to run. For that, we use the DOMContentLoaded event to tell us when the page structure has been loaded and scripts are ready to run. This is different from the onload event, which waits for all referenced content to be downloaded.[/FONT]​
[FONT=&quot]
In default.js, add an initialize() function and have it called by DOMContentLoaded. While we’re at it, let’s add the basis of the game loop as well:[/FONT]​
[FONT=&quot]
image015.png
[/FONT]​
[FONT=&quot]
Note: the app.oncheckpoint function is collapsed to make things easier to read.[/FONT]​
Variables

[FONT=&quot]
To work with the canvas, store images, and create bitmaps, we’re going to need a bunch of variables. Also, because the original game assumed a 800x480 screen, we need to scale the images we draw to the actual screen size.[/FONT]​
[FONT=&quot]
Add the following variables to default.js:[/FONT]​
[FONT=&quot]
image016.png
[/FONT]​
Initializing Canvas and Using PreloadJS

[FONT=&quot]
Earlier, I’d mentioned canvas is only updated via JavaScript. To connect to the canvas, you need to first find the element, then retrieve it’s 2D context. That context exposes the drawing functions. We’ll also scale the canvas to match our full screen size.[/FONT]​
[FONT=&quot]
Update initialize() as follows:[/FONT]​
[FONT=&quot]
image017.png
[/FONT]​
[FONT=&quot]
Now we need to load our images so we can draw them to the canvas. There are many ways to do this, but PreloadJS is helpful because we can list what we’ll use and it ensures they are loaded before we reference them. If we don’t do this, we may not reliably get details like image sizes at runtime, creating bugs.[/FONT]​
[FONT=&quot]
PreloadJS works by reading an array of resources, then calling a function when complete. We’ll specify all of the images we’ll be using.[/FONT]​
[FONT=&quot]
Extend the initialize() function as follows:[/FONT]​
[FONT=&quot]
image018.png
[/FONT]​
[FONT=&quot]
When PreloadJS has readied our assets, the prepareGame() method will be called.[/FONT]​
Using EaselJS to Create and Draw Images

[FONT=&quot]
Now we need to get those images to the screen (via the canvas). Fortunately, EaselJS has a number of features we’ll find useful:[/FONT]

  • A Stage class that manages the canvas and the scene we’re drawing
  • Bitmap, Text, and SpriteSheet classes, useful for representing items to draw
  • Point class to help position items on the canvas
  • A Ticker class to help manage the game loop (think of it as the heartbeat of the game)
[FONT=&quot]
We’ll get to the Ticker a bit later, but now let’s add the Stage so we can start populating it with content. In default.js, add the following to the initialize() function:[/FONT]​
[FONT=&quot]
image019.png
[/FONT]​
[FONT=&quot]
This creates the stage and connects it to our game’s canvas element. Now we can add items (called children) to the stage.[/FONT]​
[FONT=&quot]
Right below the initialize() function, add a prepareGame() function. (Remember we told PreloadJS to call prepareGame when it’s done loading assets). For now, let’s just add one item – the background:[/FONT]​
[FONT=&quot]
image020.png
[/FONT]​
[FONT=&quot]
What’s going on here?[/FONT]

  • Line 62 - preload.getResult() is asking PreloadJS for the image it has already loaded for us
  • Line 63 – Create an EaselJS Bitmap instance, using the image as it’s source
  • Lines 64 & 65 – Scale the Bitmap to the resolution of our screen (relative to 800x480 of the original assets)
  • Line 66 – Add the Bitmap to the Stage as a child
  • Line 68 – Ask the Stage to tell the canvas about everything it knows
[FONT=&quot]
Let’s run the game. After the splash screen, we now see:[/FONT]​
[FONT=&quot]
image021.png
[/FONT]​
A Quick Change with CSS

[FONT=&quot]
As you can see, the background image we added is transparent, so our background color is showing through. The black background is spooky, but quite not what we’re looking for.[/FONT]​
[FONT=&quot]
One thing we can do is to change which WinJS CSS base we’re using. By default, we use ui-dark.css, but a quick change in default.html to point to ui-light.css, and things automatically pick up new styles:[/FONT]​
[FONT=&quot]
image022.png
[/FONT]​
[FONT=&quot]
A quick run now shows:[/FONT]​
[FONT=&quot]
image023.png
[/FONT]​
[FONT=&quot]
However, let’s try for a more sky-like color… say, "azure". We can override the WinJS background color by setting our own via CSS. Open /css/default.css and change the body style as shown:[/FONT]​
[FONT=&quot]
image024.png
[/FONT]​
[FONT=&quot]
Running again:[/FONT]​
[FONT=&quot]
image025.png
[/FONT]​
[FONT=&quot]
A beautiful sky, ready for war![/FONT]​
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.