We Recommend These Resources

Today
we continue a series of articles on game development in HTML5 using
canvas. Today we going to learn next elements: animation with sprites
and basic work with sound. In our demonstration you will see a flying
dragon. We will hear the sounds of wings all time (we will loop this
sound), and another sound – dragon’s roar (on mouseup event). And
finally we will teach our dragon be closer to the mouse cursor (when we
hold down the mouse).
Our previous article you can read here: Developing Your First HTML5 Game – Lesson 3. Our new script is new enhanced version of previous one.
Here are our demo and downloadable package:
Live Demodownload in packageOk, download the example files and lets start coding !
Step 1. HTML
Here are all html of my demo.
index.html
| 04 | <meta charset="utf-8" /> |
| 05 | <title>HTML5 Game Development - Lesson 4 | Script Tutorials</title> |
| 07 | <link href="css/main.css" rel="stylesheet" type="text/css" /> |
| 13 | <script type="text/javascript" src="js/script.js"></script> |
| 16 | <div class="container"> |
| 17 | <canvas id="scene" width="1000" height="600"></canvas> |
| 21 | <h2>HTML5 Game Development - Lesson 4</h2> |
Step 2. CSS
Here are used CSS styles.
css/main.css
I will not publish styles today – this is just page layout styles, nothing special. Available in package.
Step 3. JS
js/script.js
| 006 | var dragonW = 75; // dragon width |
| 007 | var dragonH = 70; // dragon height |
| 008 | var iSprPos = 0; // initial sprite frame |
| 009 | var iSprDir = 4; // initial dragon direction |
| 010 | var dragonSound; // dragon sound |
| 011 | var wingsSound; // wings sound |
| 012 | var bMouseDown = false; // mouse down state |
| 015 | // ------------------------------------------------------------- |
| 018 | function Dragon(x, y, w, h, image) { |
| 026 | // ------------------------------------------------------------- |
| 029 | function clear() { // clear canvas function |
| 030 | ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
| 033 | function drawScene() { // main drawScene function |
| 034 | clear(); // clear canvas |
| 038 | if (iBgShiftX <= 0) { |
| 041 | ctx.drawImage(backgroundImage, 0 + iBgShiftX, 0, 1000, 940, 0, 0, 1000, 600); |
| 043 | // update sprite positions |
| 049 | // in case of mouse down - move dragon more close to our mouse |
| 051 | if (iLastMouseX > dragon.x) { |
| 054 | if (iLastMouseY > dragon.y) { |
| 057 | if (iLastMouseX < dragon.x) { |
| 060 | if (iLastMouseY < dragon.y) { |
| 066 | ctx.drawImage(dragon.image,
iSprPos*dragon.w, iSprDir*dragon.h, dragon.w, dragon.h, dragon.x -
dragon.w/2, dragon.y - dragon.h/2, dragon.w, dragon.h); |
| 069 | // ------------------------------------------------------------- |
| 073 | canvas = document.getElementById('scene'); |
| 074 | ctx = canvas.getContext('2d'); |
| 076 | var width = canvas.width; |
| 077 | var height = canvas.height; |
| 079 | // load background image |
| 080 | backgroundImage = new Image(); |
| 081 | backgroundImage.src="images/hell.jpg"; |
| 082 | backgroundImage.onload = function() { |
| 084 | backgroundImage.onerror = function() { |
| 085 | console.log('Error loading the background image.'); |
| 088 | // 'Dragon' music init |
| 089 | dragonSound = new Audio('media/dragon.wav'); |
| 090 | dragonSound.volume = 0.9; |
| 092 | // 'Wings' music init |
| 093 | wingsSound = new Audio('media/wings.wav'); |
| 094 | wingsSound.volume = 0.9; |
| 095 | wingsSound.addEventListener('ended', function() { // looping wings sound |
| 096 | this.currentTime = 0; |
| 101 | // initialization of dragon |
| 102 | var oDragonImage = new Image(); |
| 103 | oDragonImage.src="images/dragon.gif"; |
| 104 | oDragonImage.onload = function() { |
| 106 | dragon = new Dragon(400, 300, dragonW, dragonH, oDragonImage); |
| 108 | $('#scene').mousedown(function(e) { // binding mousedown event (for dragging) |
| 109 | var mouseX = e.layerX || 0; |
| 110 | var mouseY = e.layerY || 0; |
| 111 | if(e.originalEvent.layerX) { // changes for jquery 1.7 |
| 112 | mouseX = e.originalEvent.layerX; |
| 113 | mouseY = e.originalEvent.layerY; |
| 118 | if (mouseX > dragon.x- dragon.w/2 && mouseX < dragon.x- dragon.w/2 +dragon.w && |
| 119 | mouseY > dragon.y- dragon.h/2 && mouseY < dragon.y-dragon.h/2 +dragon.h) { |
| 127 | $('#scene').mousemove(function(e) { // binding mousemove event |
| 128 | var mouseX = e.layerX || 0; |
| 129 | var mouseY = e.layerY || 0; |
| 130 | if(e.originalEvent.layerX) { // changes for jquery 1.7 |
| 131 | mouseX = e.originalEvent.layerX; |
| 132 | mouseY = e.originalEvent.layerY; |
| 135 | // saving last coordinates |
| 136 | iLastMouseX = mouseX; |
| 137 | iLastMouseY = mouseY; |
| 139 | // perform dragon dragging |
| 145 | // change direction of dragon (depends on mouse position) |
| 146 | if (mouseX > dragon.x && Math.abs(mouseY-dragon.y) < dragon.w/2) { |
| 148 | } else if (mouseX < dragon.x && Math.abs(mouseY-dragon.y) < dragon.w/2) { |
| 150 | } else if (mouseY > dragon.y && Math.abs(mouseX-dragon.x) < dragon.h/2) { |
| 152 | } else if (mouseY < dragon.y && Math.abs(mouseX-dragon.x) < dragon.h/2) { |
| 154 | } else if (mouseY < dragon.y && mouseX < dragon.x) { |
| 156 | } else if (mouseY < dragon.y && mouseX > dragon.x) { |
| 158 | } else if (mouseY > dragon.y && mouseX < dragon.x) { |
| 160 | } else if (mouseY > dragon.y && mouseX > dragon.x) { |
| 165 | $('#scene').mouseup(function(e) { // binding mouseup event |
| 166 | dragon.bDrag = false; |
| 170 | dragonSound.currentTime = 0; |
| 174 | setInterval(drawScene, 30); // loop drawScene |
How
it work (shortly): Firstly we define canvas, context, then we load
background image, two sounds, then we initialize our dragon and binding
different mouse events. In our main loop draw function I am shifting
background image (loop), then update sprite positions, and finally –
draw our dragon. In our code you can find several new interesting
methods:
1. Loop background sound
| 2 | wingsSound = new Audio('media/wings.wav'); |
| 3 | wingsSound.volume = 0.9; |
| 4 | wingsSound.addEventListener('ended', function() { // looping wings sound |
2. Draw sprites
| 01 | var oDragonImage = new Image(); |
| 02 | oDragonImage.src="images/dragon.gif"; |
| 03 | oDragonImage.onload = function() { |
| 06 | // update sprite positions |
| 13 | ctx.drawImage(dragon.image,
iSprPos*dragon.w, iSprDir*dragon.h, dragon.w, dragon.h, dragon.x -
dragon.w/2, dragon.y - dragon.h/2, dragon.w, dragon.h); |
So,
we loading initial image (with set of all sub-images), then – draw part
of that image, then shifting its positions, and draw again (loop).
Step 4. Custom files
images/dragon.gif, images/hell.jpg, media/dragon.wav and media/wings.wav
All these files available in our package
Live Demodownload in package
Conclusion
Are you like our new handy dragon?
I will be glad to see your thanks and comments. Good luck!
source: http://www.script-tutorials.com/html5-game-development-lesson-4/