43
Game On! With HTML5

Game On! With HTML5

  • Upload
    livana

  • View
    51

  • Download
    0

Embed Size (px)

DESCRIPTION

Game On! With HTML5. AUTHD on oreilly.com. 40% off print book and 50% off the ebook . How a Game Works. Frame 3. Frame 4. Frame 1. Frame 2. Time Passed. The Canvas Element. < canvas id =" game_canvas ">. DOM Structure. DOM Structure. # container. # game_canvas. - PowerPoint PPT Presentation

Citation preview

Page 1: Game On! With HTML5

Game On!With HTML5

Page 2: Game On! With HTML5

AUTHDon oreilly.com

40% off print book and 50% off the ebook.

Page 3: Game On! With HTML5

How a Game Works

Time Passed

Frame 1 Frame 2 Frame 3 Frame 4

Page 4: Game On! With HTML5

The Canvas Element

<canvas id="game_canvas"></canvas>

Page 5: Game On! With HTML5

#container#game_canvas

#snowball_btn

DOM Structure

Page 6: Game On! With HTML5

<div id="container"> <canvas id="game_canvas" width="768" height="1366"></canvas>

<div id="snowball_btn" onclick="game.system.dominput.snowball();"></div> </div>

DOM – Code Example

Page 7: Game On! With HTML5

Resize with CSS@-ms-viewport { width: device-width; height: device-height; }

OR

@media screen and (orientation: portrait) { @-ms-viewport { width: 768px; height: 1366px; } /* CSS for portrait layout goes here */}

Page 8: Game On! With HTML5

All systems in the game have an Initialize, Update, and Draw method:

Initialize, Update, Draw pattern

Page 9: Game On! With HTML5

Drawing the Snowball to the canvas.canvascontext.drawImage( image, //image (‘snowball.png’); 0, 0, //source position 256, 261, //source size 50, 100, //destination position(canvas) 256, 261); //destination size

Game Canvas256px

261px

50px

100px

256px

261px

Source

Drawing Images to a Canvas Example

Page 10: Game On! With HTML5

AnimationLogicInputsEntitiesCollision

Building Blocks

Page 11: Game On! With HTML5

Entities

Page 12: Game On! With HTML5

Update

Collision

Not colliding.

Colliding.

Page 13: Game On! With HTML5

var audiosample = new Audio("media/Sounds/path.mp3"); audiosample.play();

Sound

Page 14: Game On! With HTML5

Only Draw what you Need:Any call to the canvas draw is expensive. The best way to optimize drawing is

to draw less.Hikers are composited with their custom face image to make only one draw call

instead of two. The sky is just applied to the background-img property because it is static.

Re-use Objects:Re-using objects instead of deleting them minimizes calls to the garbage

collector.

Pass only integers to the canvas draw API: Floats cause anti-aliasing to trigger.

Performance

Page 15: Game On! With HTML5

Watch out for Audio:Audio tags take up memory that could be used for gameplay

Read as little as possible, and never read from the same canvas

Using image data is much faster than drawing

Test, test, test: Test on many devices with different processors (don’t forget about Atom and ARM)

test, test, test: Really, test at every stage!

Performance

Page 16: Game On! With HTML5

Windows 8 appdemo

Page 17: Game On! With HTML5

Page Structure

Page 18: Game On! With HTML5

Navigation Project Template

Page 19: Game On! With HTML5
Page 20: Game On! With HTML5

WinJS

Page 21: Game On! With HTML5

WinJS.Utilities.query("a").listen("click", function (evt) { // dont load this link in the main window evt.preventDefault();

// get target element var link = evt.target;

//call navigate and pass the href of the link WinJS.Navigation.navigate(link.href); });

Overriding Link Behavior

Page 22: Game On! With HTML5

WinJS.UI.Pages.define("/home.html", { // This function is called whenever a user navigates to this page.

ready: function (element, options) { //run code unique to this page }

});

WinJS.UI.Pages.define

Page 23: Game On! With HTML5

Data Binding with WinJS.Binding.Listvar hikerArray = [{ title: 'John Smith', color:'#333333', body_img:'/images/body_333333.png', face_img:'/images/default_face.png', }, { title: 'Erin Dodge', color:'#00c0ac', body_img: '/images/body_00c0ac.png', face_img:'/images/default_face.png', }, { title: 'James Parker', … } ];

var hikerBindingList = new WinJS.Binding.List(hikerArr);

Data Binding with WinJS.Binding.List

Page 24: Game On! With HTML5

var publicMembers = { itemList: hikerBindingList };

WinJS.Namespace.define("DataHikers", publicMembers);

WinJS.Namespace.define

Page 25: Game On! With HTML5

WinJS.UI.ListView

Page 26: Game On! With HTML5

<div id="hikerListView" data-win- control="WinJS.UI.ListView" data-win-options="{ itemDataSource: DataHikers.itemList.dataSource, itemTemplate: select(‘#hikersTemplate’) }“></div>

WinJS.UI.ListView

Page 27: Game On! With HTML5

<div id="hikersTemplate" data-win-control="WinJS.Binding.Template"> <div class=“template_item"> <div class=“img_wrap"> <img class="face" data-win-bind="src: face_img" /> <img class="body" data-win-bind="src: body_img" /> </div> <div class=“overlay"> <div class=“text" data-win-bind="innerText: title"></div> </div> </div></div>

Define the Template

Page 28: Game On! With HTML5

WinJS.UI.processAll();

Define the Template

Page 29: Game On! With HTML5

<div id=“hikersAppBar" data-win-control="WinJS.UI.AppBar"></div>

WinJS.UI.AppBar

Page 30: Game On! With HTML5

<div id=“hikersAppBar" data-win-control="WinJS.UI.AppBar"> <button data-win-control="WinJS.UI.AppBarCommand" data-win-options="{id:'cmdAdd', label: 'New Hiker', icon: 'add'}"></button></div>

Adding Commands to the AppBar

Page 31: Game On! With HTML5

<div id=“hikersAppBar" data-win-control="WinJS.UI.AppBar"> <button data-win-control="WinJS.UI.AppBarCommand" data-win-options="{id:'cmdAdd', label: 'New Hiker', icon: 'add'}"></button></div>

Command Options

Page 32: Game On! With HTML5

// get add command elementvar cmdAdd = document.getElementById('cmdAdd');

cmdAdd.addEventListener('click', function (ev) { // respond to add hiker command});

Adding Functionality

Page 33: Game On! With HTML5

The Accelerometer

accelerometer.addEventListener("readingchanged", function(evt){ // handle change});

Page 34: Game On! With HTML5

Displaying Snapped View#snappedview { display:none;}

@media screen and (-ms-view-state: snapped) { #contentHost {/*hide main content*/ display:none; } #snappedview {/*show snapped view content*/ display:block; }}

Page 35: Game On! With HTML5

var viewStates = Windows.UI.ViewManagement.ApplicationViewState; var newViewState = Windows.UI.ViewManagement.ApplicationView.value; if (newViewState === viewStates.snapped) { ... //Application is in snapped view };

Custom Logic for Snapview

Page 36: Game On! With HTML5

var userInformation = Windows.System.UserProfile.UserInformation;var userPic = userInformation.getAccountPicture();

var img = new Image();var imgurl = URL.createObjectURL(userPic);img.src = imgurl;

Getting User Profile Data

Page 37: Game On! With HTML5

The contact picker launches a UI that will return a single contact.

Using ContactPicker

Page 38: Game On! With HTML5

// get reference to ContactPickervar picker = Windows.ApplicationModel.Contacts.ContactPicker();

// open the pickerpicker.pickSingleContactAsync().then(function (contact) {

// handle contact information});

Open the contact picker and handle the result

Page 39: Game On! With HTML5

contact.getThumbnailAsync().done(function (thumbnail) { // generate a URL for the thumbnail image thumbURL = URL.createObjectURL(thumbnail);

// use thumbURL to update the src of an image for // display face_img.src = thumbURL;});

Getting contact thumbnails

Page 40: Game On! With HTML5

Download the Microsoft Ad SDK:http://advertising.microsoft.com/windowsadvertising/developer

<div id="ad_bottom_rail“ data-win-control="MicrosoftNSJS.Advertising.AdControl"

data-win-options="{applicationId: 'd25517cb-12d4-4699-8bdc-52040c712cab',

adUnitId: '10043074'}"></div>

<script src="/MSAdvertisingJS/ads/ad.js"></script>

Implementing Ads using the Microsoft SDK

Page 41: Game On! With HTML5

var notifications = Windows.UI.Notifications;var template = notifications.TileTemplateType.tileWideSmallImageAndText02;var tileXml = notifications.TileUpdateManager.getTemplateContent(template);

var line1 = tileXml.getElementsByTagName("text")[0];line1.appendChild(tileXml.createTextNode(“Yeti Stats"));

Live Tile

Page 42: Game On! With HTML5

var tileNotification = new notifications.TileNotification(tileXml);

notifications.TileUpdateManager.createTileUpdaterForApplication().update(tileNotification);

Creating a Notification

Page 43: Game On! With HTML5