windows 8 game development using html5 and javascript

43
Windows 8 Game development using HTML5 and JavaScript Martin Beeby Technical Evangelist Microsoft @thebeebs

Upload: ham

Post on 24-Feb-2016

95 views

Category:

Documents


1 download

DESCRIPTION

Windows 8 Game development using HTML5 and JavaScript. Martin Beeby Technical Evangelist Microsoft @thebeebs. Agenda. Why Windows 810 ’ Store and distribution Options HTML5, DirectX, C#, Middleware Engines Basics of Canvas20’ Animation Collision Detection Sprites Using CreateJS 20’ - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Windows 8 Game development using HTML5 and JavaScript

Windows 8 Game development using HTML5 and JavaScriptMartin BeebyTechnical Evangelist Microsoft@thebeebs

Page 2: Windows 8 Game development using HTML5 and JavaScript

Agenda

AgendaWhy Windows 8 10’Store and distributionOptions HTML5, DirectX, C#, Middleware Engines

Basics of Canvas 20’AnimationCollision DetectionSprites

Using CreateJS 20’Building a Windows 8 Game with a leaderboard in Azure.

Page 3: Windows 8 Game development using HTML5 and JavaScript

Why Windows 8

Page 4: Windows 8 Game development using HTML5 and JavaScript

The storeDesigned for DiscoveryUnprecedented reachFlexible business modelsTransparent termsBest economics

Page 5: Windows 8 Game development using HTML5 and JavaScript

Flexibility of optionsOne time PurchaseUse Your Existing CommercePurchases over timeAd Supported

Page 6: Windows 8 Game development using HTML5 and JavaScript

Basics of Canvas

Page 7: Windows 8 Game development using HTML5 and JavaScript

In the beginning there was DomContentLoadeddocument.addEventListner(“DomContentLoaded”, init, false);

Page 8: Windows 8 Game development using HTML5 and JavaScript

Game loopDOMContentLoaded

() init()

draw()

exit

Page 9: Windows 8 Game development using HTML5 and JavaScript

<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title>Canvas</title>

<script src="js/1.js"></script>

</head><body> <canvas id="gameCanvas" width="800" height="600">

</canvas></body> </html>

Page 10: Windows 8 Game development using HTML5 and JavaScript

(function() { var game = {}; var supportsCanvas, canvasElement, canvasContext;

game.init = function () { supportsCanvas = !!document.createElement('canvas').getContext; canvasElement = document.getElementById("gameCanvas"); canvasContext = canvasElement.getContext('2d');; console.log('Game Loaded, And canvas support is: ' + supportsCanvas); game.draw(); }

game.draw = function () { canvasContext.fillStyle = "rgba(0,0,0,1)"; canvasContext.fillRect(20, 20, 10, 10); }

document.addEventListener("DOMContentLoaded", game.init, false);

})();

Page 11: Windows 8 Game development using HTML5 and JavaScript
Page 12: Windows 8 Game development using HTML5 and JavaScript

Game loopDOMContentLoaded

() init()

gameLoop()

draw()

exit

Page 13: Windows 8 Game development using HTML5 and JavaScript

(function() { var game = {}; var supportsCanvas, canvasElement, canvasContext;

game.init = function () { supportsCanvas = !!document.createElement('canvas').getContext; canvasElement = document.getElementById("gameCanvas"); canvasContext = canvasElement.getContext('2d');; console.log('Game Loaded, And canvas support is: ' + supportsCanvas); game.startGameLoop(); }

game.startGameLoop = function () { window.requestAnimationFrame(game.gameLoop); },

game.gameLoop = function () { game.draw(); window.requestAnimationFrame(game.gameLoop); },

game.draw = function () { canvasContext.fillStyle = "rgba(0,0,0,1)"; canvasContext.fillRect(Math.random() * 400, Math.random() * 300, 10, 10); } document.addEventListener("DOMContentLoaded", game.init, false);

})();

Page 14: Windows 8 Game development using HTML5 and JavaScript
Page 15: Windows 8 Game development using HTML5 and JavaScript

Game loopDOMContentLoade

d() init()

gameLoop()

draw()

exit

clear()

Page 16: Windows 8 Game development using HTML5 and JavaScript

(function() { var game = {}; var supportsCanvas, canvasElement, canvasContext;

game.init = function () { supportsCanvas = !!document.createElement('canvas').getContext; canvasElement = document.getElementById("gameCanvas"); canvasContext = canvasElement.getContext('2d'); console.log('Game Loaded, And canvas support is: ' + supportsCanvas); window.requestAnimationFrame(game.gameLoop); }

game.gameLoop = function () { game.clear(); game.draw(); window.requestAnimationFrame(game.gameLoop); } game.draw = function () { canvasContext.fillStyle = "rgba(0,0,0,1)"; canvasContext.fillRect(Math.random() * 400, Math.random() * 300, 10, 10); }

game.clear = function () { canvasContext.clearRect(0, 0, canvasContext.canvas.width, canvasContext.canvas.height); }

document.addEventListener("DOMContentLoaded", game.init, false);

})();

Page 17: Windows 8 Game development using HTML5 and JavaScript
Page 18: Windows 8 Game development using HTML5 and JavaScript

(function() { var game = {}; var supportsCanvas, canvasElement, canvasContext; var lilSquareList = [];

game.init = function () { supportsCanvas = !!document.createElement('canvas').getContext; canvasElement = document.getElementById("gameCanvas"); canvasContext = canvasElement.getContext('2d'); console.log('Game Loaded, And canvas support is: ' + supportsCanvas); window.requestAnimationFrame(game.gameLoop); }

game.gameLoop = function () { game.clear(); var square = { w: 10, h: 10, xSpeed: randomXToY(-10, 10), ySpeed: randomXToY(-10, 10), x: canvasContext.canvas.width / 2, y: canvasContext.canvas.height / 2, };

lilSquareList.push(square); game.draw(); window.requestAnimationFrame(game.gameLoop); }

game.draw = function () { for (var i = 0; i < lilSquareList.length; i++) { var square = lilSquareList[i];

canvasContext.fillStyle = "rgba(255,3,255,0.5)"; canvasContext.fillRect(square.x, square.y, square.w, square.h);

square.x = square.x - square.xSpeed; square.y = square.y + square.ySpeed; square.size = square.size - 0.1; } } game.clear = function () { canvasContext.clearRect(0, 0, canvasContext.canvas.width, canvasContext.canvas.height); }

function randomXToY(minVal, maxVal, floatVal) { var randVal = minVal + (Math.random() * (maxVal - minVal)); return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal); }

document.addEventListener("DOMContentLoaded", game.init, false);

})();

Page 19: Windows 8 Game development using HTML5 and JavaScript
Page 20: Windows 8 Game development using HTML5 and JavaScript

var gravity = -1; game.draw = function () { for (var i = 0; i < lilSquareList.length; i++) { var square = lilSquareList[i];

canvasContext.fillStyle = "rgba(255,3,255,0.5)"; canvasContext.fillRect(square.x, square.y, square.w, square.h);

square.x = square.x - square.xSpeed; square.y = (square.y + square.ySpeed) square.ySpeed = square.ySpeed - gravity; square.size = square.size - 0.1; } }

Page 21: Windows 8 Game development using HTML5 and JavaScript
Page 22: Windows 8 Game development using HTML5 and JavaScript

game.draw = function () { var square = { w: 10, h: 10, xSpeed: randomXToY(-10, 10), ySpeed: randomXToY(-10, 10), x: canvasContext.canvas.width / 2, y: canvasContext.canvas.height / 2, };

lilSquareList.push(square);

for (var i = 0; i < lilSquareList.length; i++) { var square = lilSquareList[i];

canvasContext.fillStyle = "rgba(255,3,255,0.5)"; canvasContext.fillRect(square.x, square.y, square.w, square.h);

square.x = square.x - square.xSpeed; square.y = (square.y + square.ySpeed) square.ySpeed = square.ySpeed - gravity; square.size = square.size - 0.1; if (square.y > (canvasContext.canvas.height) - 100) { square.ySpeed = -(square.ySpeed); } } }

Page 23: Windows 8 Game development using HTML5 and JavaScript
Page 24: Windows 8 Game development using HTML5 and JavaScript

CreateJSA JavaScript library that makes working with the HTML5 Canvas element easy.

Page 25: Windows 8 Game development using HTML5 and JavaScript
Page 26: Windows 8 Game development using HTML5 and JavaScript

<!DOCTYPE html><html><head> <meta charset="utf-8" /> <title>_2dPlatformer</title> <script type="text/javascript" src="js/easeljs-0.4.2.min.js"></script> <script src="js/1.js"></script></head><body> <canvas id="gameCanvas" width="800" height="600"> </canvas></body> </html>

Page 27: Windows 8 Game development using HTML5 and JavaScript

var supportsCanvas, canvasElement, canvasContext, imgMonsterARun, stage, spriteSheet, bmpAnimation; game.init = function () { supportsCanvas = !!document.createElement('canvas').getContext; canvasElement = document.getElementById("gameCanvas"); canvasContext = canvasElement.getContext('2d'); stage = new Stage(canvasElement); console.log('Game Loaded, And canvas support is: ' + supportsCanvas);

imgMonsterARun = new Image(); imgMonsterARun.onload = game.setupCharacters; imgMonsterARun.onerror = game.error; imgMonsterARun.src = "images/MonsterARun.png"; }

Page 28: Windows 8 Game development using HTML5 and JavaScript

Image Sprites

Page 29: Windows 8 Game development using HTML5 and JavaScript

game.setupCharacters = function () { spriteSheet = new SpriteSheet({ images: [imgMonsterARun], frames: { width: 64, height: 64, regX: 32, regY: 32 }, animations: { walk: [0, 9, "walk"] } });

bmpAnimation = new BitmapAnimation(spriteSheet);bmpAnimation.gotoAndPlay("walk");bmpAnimation.shadow = new Shadow("#454", 0, 5, 4); // Shadows are

slow bmpAnimation.name = "monster1"; bmpAnimation.direction = 90; bmpAnimation.vX = 4; bmpAnimation.x = 16; bmpAnimation.y = 32;

bmpAnimation.currentFrame = 0;stage.addChild(bmpAnimation);

game.gameLoop(); }

Page 30: Windows 8 Game development using HTML5 and JavaScript

game.gameLoop = function () { game.clear(); game.draw(); window.requestAnimationFrame(game.gameLoop); }

Page 31: Windows 8 Game development using HTML5 and JavaScript

game.draw = function () { // Hit testing the screen width, otherwise our sprite would disappear if (bmpAnimation.x >= canvasContext.canvas.width - 16) { // We've reached the right side of our screen bmpAnimation.direction = -90; } if (bmpAnimation.x < 16) { // We've reached the left side of our screen // We need to walk right now bmpAnimation.direction = 90; }

// Moving the sprite based on the direction & the speed if (bmpAnimation.direction == 90) { bmpAnimation.x += bmpAnimation.vX; } else { bmpAnimation.x -= bmpAnimation.vX; } stage.update(); }

Page 32: Windows 8 Game development using HTML5 and JavaScript
Page 33: Windows 8 Game development using HTML5 and JavaScript

game.setupCharacters = function () { spriteSheet = new SpriteSheet({ images: [imgMonsterARun],

frames: { width: 64, height: 64, regX: 32, regY: 32 }, animations: { walk: [0, 9, "walk", 4] } });

SpriteSheetUtils.addFlippedFrames(spriteSheet, true, false, false);

bmpAnimation = new BitmapAnimation(spriteSheet); bmpAnimation.gotoAndPlay("walk_h"); bmpAnimation.shadow = new Shadow("#454", 0, 5, 4); bmpAnimation.name = "monster1"; bmpAnimation.direction = 90; bmpAnimation.vX = 4; bmpAnimation.x = 16; bmpAnimation.y = 32; bmpAnimation.currentFrame = 0; stage.addChild(bmpAnimation); game.gameLoop(); }

Page 34: Windows 8 Game development using HTML5 and JavaScript

game.draw = function () { // Hit testing the screen width, otherwise our sprite would disappear if (bmpAnimation.x >= canvasContext.canvas.width - 16) { // We've reached the right side of our screen // We need to walk left now to go back to our initial position bmpAnimation.direction = -90; bmpAnimation.gotoAndPlay("walk"); } if (bmpAnimation.x < 16) { // We've reached the left side of our screen // We need to walk right now bmpAnimation.direction = 90; bmpAnimation.gotoAndPlay("walk_h");

} if (bmpAnimation.direction == 90) {

bmpAnimation.x += bmpAnimation.vX; } else { bmpAnimation.x -= bmpAnimation.vX; }

stage.update(); }

Page 35: Windows 8 Game development using HTML5 and JavaScript
Page 36: Windows 8 Game development using HTML5 and JavaScript

game.init = function () { supportsCanvas = !!document.createElement('canvas').getContext; canvasElement = document.getElementById("gameCanvas"); canvasContext = canvasElement.getContext('2d'); stage = new Stage(canvasElement); console.log('Game Loaded, And canvas support is: ' + supportsCanvas);

var manifest = [{ src: "images/MonsterARun.png", id: "imgMonsterARun" },{ src: "images/MonsterAIdle.png", id: "imgMonsterAIdle" } ];

preload = new PreloadJS(); preload.onComplete = game.setupCharacters; preload.loadManifest(manifest); preload.getResult("imgMonsterARun"); }

PreloadJS

Page 37: Windows 8 Game development using HTML5 and JavaScript

game.setupCharacters = function () { var image = preload.getResult("imgMonsterARun").result;

spriteSheet = new SpriteSheet({ images: [image], frames: { width: 64, height: 64, regX: 32, regY: 32 },

animations: { walk: [0, 9, "walk", 4] } });

SpriteSheetUtils.addFlippedFrames(spriteSheet, true, false, false); bmpAnimation = new BitmapAnimation(spriteSheet);

bmpAnimation.gotoAndPlay("walk_h"); //animate bmpAnimation.shadow = new Shadow("#454", 0, 5, 4);

bmpAnimation.name = "monster1"; bmpAnimation.direction = 90; bmpAnimation.vX = 4; bmpAnimation.x = 16; bmpAnimation.y = 32;

bmpAnimation.currentFrame = 0;

stage.addChild(bmpAnimation); var imgMonsterAIdle = preload.getResult("imgMonsterAIdle").result;

spriteSheetIdle = new SpriteSheet({ images: [imgMonsterAIdle], frames: { width: 64, height: 64, regX: 32, regY: 32 }, animations: { idle: [0, 10, "idle", 4] } });

bmpAnimationIdle = new BitmapAnimation(spriteSheetIdle); bmpAnimationIdle.name = "monsteridle1"; bmpAnimationIdle.x = 16; bmpAnimationIdle.y = 32;

game.gameLoop(); }

Page 38: Windows 8 Game development using HTML5 and JavaScript

game.draw = function () {if (bmpAnimation.x >= canvasContext.canvas.width - 16) {

// We've reached the right side of our screen bmpAnimation.direction = -90;

bmpAnimation.gotoAndPlay("walk"); }

if (bmpAnimation.x < 16) { bmpAnimation.direction = 90;

bmpAnimation.gotoAndStop("walk"); stage.removeChild(bmpAnimation); bmpAnimationIdle.gotoAndPlay("idle"); stage.addChild(bmpAnimationIdle);

} if (bmpAnimation.direction == 90) {

bmpAnimation.x += bmpAnimation.vX; } else { bmpAnimation.x -= bmpAnimation.vX; }

stage.update(); }

Page 39: Windows 8 Game development using HTML5 and JavaScript
Page 40: Windows 8 Game development using HTML5 and JavaScript
Page 41: Windows 8 Game development using HTML5 and JavaScript
Page 42: Windows 8 Game development using HTML5 and JavaScript

http://bit.ly/Windows8GameLinks to EaselJS, and source code of both projects with instructions

Page 43: Windows 8 Game development using HTML5 and JavaScript

© 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a

commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.