javascript – HTML 5画布键按时间事件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – HTML 5画布键按时间事件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3356字,纯文字阅读大概需要5分钟。
内容图文
![javascript – HTML 5画布键按时间事件](/upload/InfoBanner/zyjiaocheng/772/0b4df6a11af8494e8e7a47ca2889502c.jpg)
嗨,大家好我正在研究HTML 5中的stick hero(着名的安卓游戏)克隆,我试图在javascript中实现用于捕获按键时间(右箭头 – ASCII 39)的代码并相应地增长.
<!doctype html>
<html>
<head>
<title> Stuck Hero !</title>
<script src='jquery-2.1.1.min.js'> </script>
</head>
<body>
<canvas id='stuck' height='300' width='500'> </canvas>
</body>
<script>
var stick_size=10;
$(document).ready(function(){
var canvas=$("#stuck")[0];
var ctx=canvas.getContext('2d');
var w=$("#stuck").width();
var h=$("#stuck").height();
var score=0;
var stick=0;
var timeout=0;
console.log(w +" is width and "+h+" is the height ");
paint(w,h,ctx);
score_update(ctx);
});
function score_update(ctx){
//function to print the score
ctx.font = "20px Arial";
ctx.strokeText("Your Score is :",1,300);
console.log("Console Score Printed Successfully");
}
function paint(width,height,ctl) {
var rand = Math.floor(Math.random() * 101);
ctx=ctl;
// function to print different background styles
if(rand %3 ==0) {
ctx.fillStyle = "#F0F0F0";
}
else if(rand %2 == 0){
ctx.fillStyle = "#D0D0D0";
}
else{
ctx.fillStyle = "#C0C0C0";
}
drawing(ctx,width,height);
console.log("Console was Painted Successfully");
}
function drawing(ctx,width,height) {
//filling rectangle
ctx.fillRect(0,0,width,height);
//drawing our hero
ctx.beginPath();
ctx.arc(20, 200, 15 , 0, 2 * Math.PI, false);
ctx.fillStyle = 'green';
ctx.fill();
console.log("Shapes were drawn Successfully");
}
//growing stick
function grow_stick() {
for(lv=200;lv>stick_size;lv++) {
ctx.beginPath();
ctx.moveTo(20,200);
console.log("Moved to 20 , 200 in Canvas");
ctx.lineTo(20,lv);
console.log("Drawing Line from 20 to "+lv);
ctx.stroke();
console.log("Stroke is Done");
}
console.log("Stick was growing Successfully");
}
function clearcanvas(ctl,width,height){
//function to clear the screen data
ctl.clearRect ( 0 , 0 ,width,height );
}
$(document).keydown(function(e){
var key = e.which;
if(key == "39"){ //38 is the code for up key
//event.preventDefault();
console.log("Down key is pressed");
timeout= setInterval(grow_stick);
}
}).bind('keyup', function(e){
if(e.which == "39") {
console.log("Down key pressed is released ");
clearTimeout(timeout);
}
});
</script>
</html>
我有一些问题和澄清
>如何跟踪按键事件?
I tried to set a timeout function when key is pressed and I will release the timeout function when the key is released (but having some issues with cleartimeout)
>一旦密钥发布,如何不接受任何事件,我还要做其他动画?
Not tried yet and have no idea
>另一个常见问题
How to prepare my mindset as a HTML5 Game developer, any reference link or idea is welcomed, I know basic programming
编辑:jsfiddle
解决方法:
>尝试使用这样的间隔:
var myInt=null
$(document).keydown(function(e){
var key = e.which;
if(key == "39"){ //38 is the code for up key
//event.preventDefault();
console.log("Down key is pressed");
myInt = setInterval(function(){ myAnimation() }, 40);
//40 milliseconds because of 25 images per sec
}
}).bind('keyup', function(e){
if(e.which == "39") {
console.log("Down key pressed is released ");
clearInterval(myInt);
}
function myAnimation(){
//your animation that last 40 milliseconds
}
>你可以在keyeup事件中设置某种布尔值,并在if(if(keyreleased == false){// do normal stuff} else {// skip})的每个事件中检查它
动画完成后,将布隆设置恢复为默认值
var keyreleased=false;
.bind('keyup', function(e){
keyreleased=true;
}
someEvent(){
if(keyreleased!=true){
//do stuff
}
//animation done
keyreleased=false;
}
>我不知道^^
内容总结
以上是互联网集市为您收集整理的javascript – HTML 5画布键按时间事件全部内容,希望文章能够帮你解决javascript – HTML 5画布键按时间事件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。