原文地址:http://www.script-tutorials.com/html5-game-development-lesson-5/

最终我决定准备下一篇游戏开发系列的文章,我们将继续使用canvas来进行HTML5游戏开发系列的文章。今天我准备音乐的例子(有点儿像音乐合成器),它用到了使用CSS3来制作的基于DOM的对话框。为什么我使用独立的对话框--简单,最主要是因为CSS有很多处理标准元素的文本和样式的方法,且它可以使JS的代码简单,同时能提升例子程序的速度。所以你可以对这些对话框应用特定的样式,例如我利用CSS3定制了一个滚动条。

前一篇的的介绍在HTML5游戏开发系列教程4(译)。

第一步:HTML

下面是我们第5课的源代码

index.html

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>HTML5 Game Development - Lesson 5 | Script Tutorials</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-2.0.0.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<header>
<h2>HTML5 Game Development - Lesson 5</h2>
<a href="http://www.script-tutorials.com/html5-game-development-lesson-5/" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
</header>
<div class="container">
<div class="bar">
<button id="options">Options</button>
</div>
<canvas id="scene" width="800" height="600"></canvas>
<div id="controls">
<div id="dialogs" class="dialogs">
<div id="dialog1" class="dialog dialogVisible">
<h1>Welcome to lesson #5</h1>
<textarea>
Please click buttons from 0 to 9 to produce different sounds.
Please click buttons from 0 to 9 to produce different sounds.
Please click buttons from 0 to 9 to produce different sounds.
Please click buttons from 0 to 9 to produce different sounds.
Please click buttons from 0 to 9 to produce different sounds.
Please click buttons from 0 to 9 to produce different sounds.
Please click buttons from 0 to 9 to produce different sounds.
Please click buttons from 0 to 9 to produce different sounds.
</textarea>
<button id="but1">Next</button>
</div>
<div id="dialog2" class="dialog">
<h1>Second page</h1>
<textarea>
Plus, this is are also a demonstration of DOM-based dialog windows with CSS3 and jQuery.
Plus, this is are also a demonstration of DOM-based dialog windows with CSS3 and jQuery.
Plus, this is are also a demonstration of DOM-based dialog windows with CSS3 and jQuery.
Plus, this is are also a demonstration of DOM-based dialog windows with CSS3 and jQuery.
Plus, this is are also a demonstration of DOM-based dialog windows with CSS3 and jQuery.
</textarea>
<button id="but2">Next</button>
</div>
<div id="dialog3" class="dialog">
<h1>Third page</h1>
<button id="but3">First page</button>
<button id="but_close">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>

上面代码中,我们的Canvas里,我添加了控制条(该控制条可以放一些操作按钮),目前它包含一个按钮,用来显示对话框的。在Canvas下面是一系列的对话框信息。

第二步 CSS

下面是CSS的层叠样式

 /* page layout styles */
*{
margin:;
padding:;
}
body {
background-color:#eee;
color:#fff;
font:14px/1.3 Arial,sans-serif;
}
header {
background-color:#212121;
box-shadow: 0 -1px 2px #111111;
display:block;
height:70px;
position:relative;
width:100%;
z-index:;
}
header h2{
font-size:22px;
font-weight:normal;
left:50%;
margin-left:-400px;
padding:22px 0;
position:absolute;
width:540px;
}
header a.stuts,a.stuts:visited{
border:none;
text-decoration:none;
color:#fcfcfc;
font-size:14px;
left:50%;
line-height:31px;
margin:23px 0 0 110px;
position:absolute;
top:;
}
header .stuts span {
font-size:22px;
font-weight:bold;
margin-left:5px;
}
.container {
margin: 20px auto;
overflow: hidden;
position: relative;
width: 800px;
} /* game lesson styles */
.bar {
background-color: #337755;
height:;
opacity: 0.9;
overflow: hidden;
position: absolute;
width: 800px;
z-index:; -moz-transition: 1s;
-ms-transition: 1s;
-o-transition: 1s;
-webkit-transition: 1s;
transition: 1s;
}
.bar button {
padding:3px;
float:right;
}
.barVisible {
height: 30px;
}
#scene {
position:relative;
}
#controls {
height: 600px;
opacity:;
position: absolute;
top:;
width: 800px;
z-index:; -moz-transition: 1s;
-ms-transition: 1s;
-o-transition: 1s;
-webkit-transition: 1s;
transition: 1s;
}
.controlsPanel {
height:80px;
opacity: 0 !important;
width:120px;
z-index: -1 !important; -moz-transform: scale(0.2);
-ms-transform: scale(0.2);
-o-transform: scale(0.2);
-webkit-transform: scale(0.2);
transform: scale(0.2);
}
#controls .dialogs {
height: 400px;
margin: 100px auto;
overflow: hidden;
position: relative;
width: 600px; -moz-transition: 1s;
-ms-transition: 1s;
-o-transition: 1s;
-webkit-transition: 1s;
transition: 1s;
}
#controls .dialog {
background-color: #444;
border-radius: 25px;
display: none;
height: 400px;
opacity: 0.95;
position: absolute;
text-align: center;
width: 600px; -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); -moz-transition: 1s;
-ms-transition: 1s;
-o-transition: 1s;
-webkit-transition: 1s;
transition: 1s;
}
#controls .dialogVisible {
display:block;
}
#controls .dialog > * {
margin-bottom:20px;
}
#controls .dialog h1 {
font-size: 36px;
padding-top: 50px;
position: relative;
text-align: center;
}
#controls .dialog textarea {
display: block;
height: 150px;
margin: 0 auto 20px;
width: 400px;
}
#controls .dialog button {
border-radius:20px;
border-width:2px;
width:100px;
height:60px; -moz-transition: 1s;
-ms-transition: 1s;
-o-transition: 1s;
-webkit-transition: 1s;
transition: 1s;
}
#controls .dialog button:hover {
border-radius:30px;
border-width:4px;
width:120px;
} /* customized scrollbar */
#controls .dialog textarea::-webkit-scrollbar {
width: 12px;
}
#controls .dialog textarea::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
-webkit-border-radius: 10px;
border-radius: 10px;
}
#controls .dialog textarea::-webkit-scrollbar-thumb {
-webkit-border-radius: 10px;
border-radius: 10px;
background: rgba(255,0,0,0.8);
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);
}
#controls .dialog textarea::-webkit-scrollbar-thumb:window-inactive {
background: rgba(255,0,0,0.4);
}

在上面代码的最后部分,你可以看见我是如何用CSS3来定制文本区的滚动条样式,目前这个功能只能在Chrome浏览器上工作(firefox也可以了)

第三步:JS

js/script.js

 //内部变量
var canvas, ctx;
var image;
var sounds = [];
var lastColor = 'rgba(255, 128, 0, 0.5)'; //清楚画布
function clear() {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
} //
function drawScene() {
clear();
ctx.drawImage(image, 0, 0); ctx.fillStyle = lastColor;
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
} //初始化
$(function() {
canvas = document.getElementById('scene');
ctx = canvas.getContext('2d'); var width = canvas.width;
var height = canvas.height; //加载背景图片
image = new Image();
image.src = 'images/synthesizer.png';
image.onload = function() {}
image.onerror = function() {
console.log('Error loading the background image.');
} //每个按键的声音
sounds[0] = new Audio('media/button-1.wav');
sounds[0].volume = 0.9;
sounds[1] = new Audio('media/button-2.wav');
sounds[1].volume = 0.9;
sounds[2] = new Audio('media/button-3.wav');
sounds[2].volume = 0.9;
sounds[3] = new Audio('media/button-4.wav');
sounds[3].volume = 0.9;
sounds[4] = new Audio('media/button-5.wav');
sounds[4].volume = 0.9;
sounds[5] = new Audio('media/button-6.wav');
sounds[5].volume = 0.9;
sounds[6] = new Audio('media/button-7.wav');
sounds[6].volume = 0.9;
sounds[7] = new Audio('media/button-8.wav');
sounds[7].volume = 0.9;
sounds[8] = new Audio('media/button-9.wav');
sounds[8].volume = 0.9;
sounds[9] = new Audio('media/button-10.wav');
sounds[9].volume = 0.9; //对话框按钮上的点击事件
$('#but1').click(function() {
$('.dialog').removeClass('dialogVisible');
$('#dialog2').addClass('dialogVisible');
});
$('#but2').click(function() {
$('.dialog').removeClass('dialogVisible');
$('#dialog3').addClass('dialogVisible');
});
$('#but3').click(function() {
$('.dialog').removeClass('dialogVisible');
$('#dialog1').addClass('dialogVisible');
});
$('#but_close').click(function() {
$('#controls').addClass('controlsPanel');
$('.bar').addClass('barVisible');
});
$('#options').click(function() {
$('#controls').removeClass('controlsPanel');
$('.bar').removeClass('barVisible');
$('.dialog').removeClass('dialogVisible');
$('#dialog1').addClass('dialogVisible');
}); //键盘事件
$(window).keydown(function(event) {
switch (event.keyCode) {
case 48: // ‘0’ key
sounds[0].currentTime = 0;
sounds[0].play();
lastColor = 'rgba(0, 128, 255, 0.5)';
break;
case 49: // ‘1’ key
sounds[1].currentTime = 0;
sounds[1].play();
lastColor = 'rgba(128, 128, 0, 0.5)';
break;
case 50: // '2' key
sounds[2].currentTime = 0;
sounds[2].play();
lastColor = 'rgba(255, 128, 0, 0.5)';
break;
case 51:
sounds[3].currentTime = 0;
sounds[3].play();
lastColor = 'rgba(0, 255, 0, 0.5)';
break;
case 52:
sounds[4].currentTime = 0;
sounds[4].play();
lastColor = 'rgba(128, 255, 0, 0.5)';
break;
case 53:
sounds[5].currentTime = 0;
sounds[5].play();
lastColor = 'rgba(255, 255, 0, 0.5)';
break;
case 54:
sounds[6].currentTime = 0;
sounds[6].play();
lastColor = 'rgba(0, 0, 0, 0.5)';
break;
case 55:
sounds[7].currentTime = 0;
sounds[7].play();
lastColor = 'rgba(0, 128, 0, 0.5)';
break;
case 56:
sounds[8].currentTime = 0;
sounds[8].play();
lastColor = 'rgba(0, 255, 0, 0.5)';
break;
case 57:
sounds[9].currentTime = 0;
sounds[9].play();
lastColor = 'rgba(128, 128, 255, 0.5)';
break; }
});
setInterval(drawScene, 200); //循环重绘
});

结论:

今天,我们复习了在html5中使用声音,并且学习了如何使用CSS3来制作基于DOM的对话框。我将非常高兴看到你们的谢意和评论。好运!

源代码下载地址:http://www.script-tutorials.com/demos/205/source.zip

HTML5游戏开发系列教程5(译)的更多相关文章

  1. HTML5游戏开发系列教程7(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-7/ 今天我们将完成我们第一个完整的游戏--打砖块.这次教程中,将 ...

  2. HTML5游戏开发系列教程6(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-6/ 这是我们最新一篇HTML5游戏开发系列文章.我们将继续使用c ...

  3. HTML5游戏开发系列教程4(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-4/ 这篇文章是我们继续使用canvas来进行HTML5游戏开发系 ...

  4. HTML5游戏开发系列教程8(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-8/ 这是我们最新一篇HTML5游戏开发系列文章.我们将继续使用c ...

  5. HTML5游戏开发系列教程10(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-10/ 最后我们将继续使用canvas来进行HTML5游戏开发系列 ...

  6. HTML5游戏开发系列教程9(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-9/ 今天我们将继续使用canvas来进行HTML5游戏开发系列的 ...

  7. cocos2d-x游戏开发系列教程-前言

    cocos2d-x游戏开发前景: 最近企业对于Cocos2D-X开发人才的用人需求很大,而且所提供的薪资相当可观. 为满足广大向往游戏开发行业同学的需求,特推出适合新手的Cocos2D-X手游开发教程 ...

  8. cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap

    背景 在上一篇博客中,我们提到CMGameScene,但是CMGameScene只是个框架,实际担任游戏逻辑的是CMGameMap类,这个博文就来了解下CMGameMap 头文件 class CMGa ...

  9. cocos2d-x游戏开发系列教程-超级玛丽06-CMGameScene

    背景 在CMMenuScene中,当用户点击开始游戏时,导演让场景进入到CMGameScene 头文件 class CMGameScene : public cocos2d::CCLayer,publ ...

随机推荐

  1. 哪一个不是EL定义的隐式对象?(选择1项)

    哪一个不是EL定义的隐式对象?(选择1项) A cookie B.pageContext C.attributes D initParam 解答:C 1)pageContext:JSP 页的上下文.它 ...

  2. Abstract可以将子类的共性最大限度的抽取出来,放在父类中,以提高程序的简洁性

    Abstract可以将子类的共性最大限度的抽取出来,放在父类中,以提高程序的简洁性. Abstract虽然不能生成对象,但是可以声明,作为编译时类型,但不能作为运行时类型. Final和abstrac ...

  3. 【BZOJ】1047: [HAOI2007]理想的正方形(单调队列/~二维rmq+树状数组套树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1047 树状数组套树状数组真心没用QAQ....首先它不能修改..而不修改的可以用单调队列做掉,而且更 ...

  4. 采用std::thread 替换 openmp

    内容转换的,具体详见博客:https://cloud.tencent.com/developer/article/1094617 及对应的code:https://github.com/cpuimag ...

  5. sys.argv 详细用法

    sys.argv 用于获取命令行参数,用法如下: [root@localhost ~]$ cat 1.py #!/usr/bin/env python #-*- coding:utf-8 -*- im ...

  6. 【黑金原创教程】 FPGA那些事儿 SignalTap II 调试技巧

    简介工具篇系列的第三本教程,讲述各种与SignalTap II 有关的调试技巧. 目录[黑金原创教程] FPGA那些事儿<工具篇III>:File01 - 上线调试与下线调试[黑金原创教程 ...

  7. 三 Android Studio打包EgretApp (SDK选择和下载)

    一 设置项目的sdk路径 二 设置项目使用sdk版本 一 设置项目的sdk路径 设置SDK目录 选择你电脑上的sdk路径 二 在项目中设置SDK版本 在项目中设置编译的sdk版本 在SDK Manag ...

  8. 第九讲 C#练习题

    1.接收一个整数N 如果N是正数就输出1·N 如果是负数就提示出错输出 由丰老师提供 -_-~ 2.编一个程序,从三个红球,五个白球,六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案. ...

  9. 【BZOJ1895】Pku3580 supermemo Splay

    [BZOJ1895]Pku3580 supermemo Description 给出一个初始序列fA1;A2;:::Ang,要求你编写程序支持如下操作: 1. ADDxyD:给子序列fAx:::Ayg ...

  10. PHP 开发环境的搭建和使用03-- 安装mySql

    1/  安装的MySQL版本是5.6.10版本的,直接点击Install 2/ 选择 Execute 3/  更新最新版本成功后,选择 "next" 4/  自定义安装方式,选择C ...