Javascript 我正在尝试用 noloop() 暂停球;但是在键向上我想继续球反弹,你能看看我的代码并帮助我吗?
Javascript I am trying to pause the ball with noloop(); but on key up I want to continue the ball bounce can you look at my code and help me?
Javascript 我正在尝试用noloop()暂停球;但是在键向上时,我想继续球反弹,你能看看我的代码并帮助我吗? 我在尝试以帧速率暂停弹跳球并返回弹跳球时遇到问题。我设法获得了一些能够识别至少几个键的代码,但我只需要一个键来停止弹跳球,并且在释放时使用相同的键让球弹跳?有什么想法吗?请帮忙!
<!DOCTYPE html>
<html>
<head>
<title>Player Movement using onkeydown/onkeyup (Enhanced version)</title>
<style type="text/css" media="screen">
canvas { border: 1px solid; }
</style>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1150473-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript" charset="utf-8">
var context;
var x=100;
var y=200;
var dx=5;
var dy=5;
var Key = {
_pressed: {},
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
isDown: function(keyCode) {
return this._pressed[keyCode];
},
onKeydown: function(event) {
this._pressed[event.keyCode] = true;
},
onKeyup: function(event) {
delete this._pressed[event.keyCode];
}
};
window.addEventListener('keyup', function(event) { Key.onKeyup(event); }, false);
window.addEventListener('keydown', function(event) { Key.onKeydown(event); }, false);
var Game = {
fps: 60,
width: 640,
height: 480
};
Game._onEachFrame = (function() {
var requestAnimationFrame = window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
if (requestAnimationFrame) {
return function(cb) {
var _cb = function() { cb(); requestAnimationFrame(_cb); }
_cb();
};
} else {
return function(cb) {
setInterval(cb, 1000 / Game.fps);
}
}
})();
Game.start = function() {
Game.canvas = document.createElement("canvas");
Game.canvas.width = Game.width;
Game.canvas.height = Game.height;
Game.context = Game.canvas.getContext("2d");
document.body.appendChild(Game.canvas);
Game.player = new Player();
Game._onEachFrame(Game.run);
};
Game.run = (function() {
var loops = 0, skipTicks = 1000 / Game.fps,
maxFrameSkip = 10,
nextGameTick = (new Date).getTime(),
lastGameTick;
return function() {
loops = 0;
while ((new Date).getTime() > nextGameTick) {
Game.update();
nextGameTick += skipTicks;
loops++;
}
if (loops) Game.draw();
}
})();
Game.draw = function() {
Game.context.clearRect(0, 0, Game.width, Game.height);
Game.player.draw(Game.context);
};
Game.update = function() {
Game.player.update();
};
function Player() {
this.x = 0;
this.y = 0;
}
Player.prototype.draw = function(context) {
context.clearRect(0,0, 300,300);
context.beginPath();
context.fillStyle="#0000ff";
//Draw a circle of radius 20 at the coordinates 100,100 on the canvas
context.arc(this.x, this.y,20,0,Math.PI*2,true);
context.closePath();
context.fill();
if(this.x<0 || this.x>550) dx=-dx;
if(this.y<0 || this.y>400) dy=-dy;
this.x+=dx;
this.y+=dy;
//noLoop();
};
Player.prototype.moveLeft = function() {
this.x = 100;
this.y = 100;
};
Player.prototype.moveRight = function() {
//if (onKeyup) noloop();
//else if (onKeydown) loops();
};
Player.prototype.moveUp = function() {
this.y -= 1;
};
Player.prototype.moveDown = function() {
this.y += 1;
};
Player.prototype.update = function() {
if (Key.isDown(Key.UP)) this.moveUp();
if (Key.isDown(Key.LEFT)) this.moveLeft();
if (Key.isDown(Key.DOWN)) this.moveDown();
if (Key.isDown(Key.RIGHT)) this.moveRight();
//else if (Key.onKeyup(Key.RIGHT)) this.Loop();
};
</script>
</head>
<body onload="Game.start()">
</body>
</html>
在Hovland教授的帮助下,我发现代码只需要通过破坏来测试和调试。查看序列,有一个循环方法具有与之关联的值,当更改值时,循环序列会停止并且能够在按下键后返回注释稍后发布
<!DOCTYPE html>
<html>
<head>
<title>Player Movement using onkeydown/onkeyup (Enhanced version)</title>
<style type="text/css" media="screen">
canvas { border: 1px solid; }
</style>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1150473-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript" charset="utf-8">
var nextGameTick = (new Date).getTime();
var isPaused = 0;
var loops = 0;
var context;
var x=100;
var y=200;
var dx=5;
var dy=5;
var Key = {
_pressed: {},
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
isDown: function(keyCode) {
return this._pressed[keyCode];
},
onKeydown: function(event) {
this._pressed[event.keyCode] = true;
},
onKeyup: function(event) {
delete this._pressed[event.keyCode];
}
};
window.addEventListener('keyup', function(event) { Key.onKeyup(event); }, false);
window.addEventListener('keydown', function(event) { Key.onKeydown(event); }, false);
var Game = {
fps: 60,
width: 640,
height: 480
};
Game._onEachFrame = (function() {
var requestAnimationFrame = window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
if (requestAnimationFrame) {
return function(cb) {
var _cb = function() { cb(); requestAnimationFrame(_cb); }
_cb();
};
} else {
return function(cb) {
setInterval(cb, 1000 / Game.fps);
}
}
})();
Game.start = function() {
Game.canvas = document.createElement("canvas");
Game.canvas.width = Game.width;
Game.canvas.height = Game.height;
Game.context = Game.canvas.getContext("2d");
document.body.appendChild(Game.canvas);
Game.player = new Player();
Game._onEachFrame(Game.run);
};
Game.run = (function() {
var
skipTicks = 1000 / Game.fps,
maxFrameSkip = 10,
//nextGameTick = (new Date).getTime(),
lastGameTick,
tempTick;
return function() {
loops = 0;
tempTick = (new Date).getTime();
if ( tempTick > nextGameTick) {
Game.update();
nextGameTick += skipTicks;
loops++;
}
if (loops > 0) Game.draw();
else {
console.log("++++++++++++++++++++++++++++++++++");
}
}
})();
Game.draw = function() {
Game.context.clearRect(0, 0, Game.width, Game.height);
Game.player.draw(Game.context);
};
Game.update = function() {
Game.player.update();
};
function Player() {
this.x = 0;
this.y = 0;
}
Player.prototype.draw = function(context) {
context.clearRect(0,0, 300,300);
context.beginPath();
context.fillStyle="#0000ff";
//Draw a circle of radius 20 at the coordinates 100,100 on the canvas
context.arc(this.x, this.y,20,0,Math.PI*2,true);
context.closePath();
context.fill();
if(this.x<0 || this.x>550) dx=-dx;
if(this.y<0 || this.y>400) dy=-dy;
this.x+=dx;
this.y+=dy;
//noLoop();
};
Player.prototype.moveLeft = function() {
loops = -12;
// if(isPaused == 0){
// loops = -1;
// isPaused = 1;
// console.log("paused");
// }
// else if(isPaused == 1){
// loops = 1;
// isPaused = 0;
// console.log("unpaused");
// }
};
Player.prototype.notMoveLeft = function() {
console.log("left arrow up");
//Game.run();
};
Player.prototype.moveRight = function() {
console.log("Foo");
//if (onKeyup) noloop();
//else if (onKeydown) loops();
};
Player.prototype.moveUp = function() {
this.y -= 1;
};
Player.prototype.moveDown = function() {
this.y += 1;
};
Player.prototype.update = function() {
if (Key.isDown(Key.UP)) this.moveUp();
if (Key.isDown(Key.LEFT)) this.moveLeft();
if (Key.isDown(Key.DOWN)) this.moveDown();
if (Key.isDown(Key.RIGHT)) this.moveRight();
if (Key.onKeyup(Key.LEFT)) this.notMoveLeft();
};
</script>
</head>
<body onload="Game.start()">
</body>
</html>
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 如何使我的javascript在页面加载后继续执行
- Javascript 我正在尝试用 noloop() 暂停球;但是在键向上我想继续球反弹,你能看看我的代码并帮助我吗?
- 我可以'Don’我不想让我的程序继续下去;m使用document.write,因为它在html中使用JavaSc
- 谷歌浏览器继续加载我的网站
- 打开一个外部 URL 并继续我自己的网页
- 如何使我的滑块继续循环
- 我主持了一个使用WordPress的网站.我想只使用Javascript创建一个网页(以演示我的项目).如何继续
- 我怎么能等待谷歌远程呼叫返回我的结果之前继续
- 如何继续在我的页面上添加/显示项目,同时滚动垂直栏
- 我怎么能保持我的程序继续超过setTimeout
- 如何使照片的链接添加到我的文件中,并自动继续使用JavaScript
- 是想控制我的动画的FPS一个很好的理由继续使用setTimeout而不是requestAnimationFrame
- 继续在我的node.js服务器上获得以下警告
- 我如何在JavaScript处理期间继续播放我的GIF(因为它冻结)
- Ember js:在我的例子中,为什么路由器中的逻辑会继续,即使其中使用的功能还没有完成
- 为什么 JavaScript 在我关闭我的安卓应用程序后继续
- HTML5:每当我发送文本区域消息来填充电子邮件的正文时,文本区域的名称就会继续.我该如何解决这个问题?
- 我想让我的表单在你按回车键时继续运行