这个想法是,这个游戏在随机位置每15毫秒生成一个圆圈。它似乎不能识别变量initialize,也不会创建新的圆,甚至根本不会生成圆
<!DOCTYPE html>
<html>
<head>
<script src="wack-a-circle.js"></script>
</head>
<body onload="initialize();">
<canvas id="canvas" width="848" height="600" style="border:1px solid black;"></canvas>
</body>
</html>现在是javascript
var canvas;
var circles = [];
var circleRadius = 50;
var score = 0;
var xaxis = getRandom(848)
var yaxis = getRandom(600)
function wackableCircle(){
xaxis = getRandom(848)
yaxis = getRandom(600)
if(circles.length > 10){
alert("You lose!\nScore: " + score)
}
setTimeout(function() {circles.push(new wackableCircle()}, 1000 - (score / 5));
}
function getRandom(max){
return Math.floor((Math.random() * max));
}
function initialize(){
canvas = document.getElementById("canvas");
canvas.addEventListener("mousedown", onMouseDown)
//add new circle every 1000ms
setTimeout(function () {circles.push(new wackableCircle())}, 1000);
setInterval(function () {updateCanvas()}, 15); //Update canvas every 15ms.
}
//Find difference between two numbers.
function numberDifference (a, b){
return Math.abs(a - b);
}
function onMouseDown(event){
for(var i = 0; i < circles.length; i++){
var wackableCircle = circles[i];
if(numberDifference(event.clientX, xaxis) < circleRadius){
//The circle's X and the mouse's X are within 50.
if(numberDifference(event.clientY, yaxis) < circleRadius){
//The circle's Y and the mouse's Y are also with in 50.
circles.splice(i, 1);
}
}
}
alert("click at " + event.clientX + ", " + event.clientY);
}
function updateCanvas(){
//clear canvas
var canvasContext = canvas.getContext("2d");
canvasContext.clearRect(0, 0, 848, 600);
for (var i = 0; i < circles.length; i++){
wackableCircle = circles[i];
//Draw circle at wackableCircle's x and y.
canvasContext.beginPath();
canvasContext.arc(xaxis, yaxis, circleRadius, 0 ,Math.PI * 2);
canvasContext.fill();
}
}发布于 2015-11-10 18:36:03
我可以让你更容易的走一步,我想这回答了你关于显示的问题。不过,这并不能让整个系统正常工作。
更新
setTimeout(function() {circles.push(new wackableCircle()}, 1000 - (score / 5));使用
setTimeout(function() { circles.push(new wackableCircle()) }, 1000 - (score / 5));在新的wackableCircle()之后,您缺少一个右方括号。
再近一步..。
var canvas;
var circles = [];
var circleRadius = 50;
var score = 0;
var xaxis = getRandom(848)
var yaxis = getRandom(600)
function wackableCircle(){
return { x: getRandom(848), y : getRandom(600)};
}
function getRandom(max){
return Math.floor((Math.random() * max));
}
function initialize(){
canvas = document.getElementById("canvas");
canvas.addEventListener("mousedown", onMouseDown)
//add new circle every 1000ms
setInterval(function () {circles.push(new wackableCircle())}, 1000 - (score / 5));
setInterval(function () {updateCanvas()}, 15); //Update canvas every 15ms.
}
//Find difference between two numbers.
function numberDifference (a, b){
return Math.abs(a - b);
}
function onMouseDown(event){
for(var i = 0; i < circles.length; i++){
var w = circles[i];
if(numberDifference(event.clientX, w.x) < circleRadius){
//The circle's X and the mouse's X are within 50.
if(numberDifference(event.clientY, w.y) < circleRadius){
//The circle's Y and the mouse's Y are also with in 50.
circles.splice(i, 1);
}
}
}
alert("click at " + event.clientX + ", " + event.clientY);
}
function updateCanvas(){
//clear canvas
var canvasContext = canvas.getContext("2d");
canvasContext.clearRect(0, 0, 848, 600);
for (var i = 0; i < circles.length; i++){
w = circles[i];
//Draw circle at wackableCircle's x and y.
canvasContext.beginPath();
canvasContext.arc(w.x, w.y, circleRadius, 0 ,Math.PI * 2);
canvasContext.fill();
}
}您的代码中存在一个重大缺陷,即wackableCircle()实际上没有返回任何内容。我现在让它返回一个带有随机x&y坐标的简单对象,我们可以对其进行查询。您的版本使用的是代码中其他地方定义的变量,而不是您打算创建的对象的随机变量。加速圆圈出现的逻辑可能在这里并不正确,但你会明白的:)
发布于 2015-11-10 18:39:53
尝尝这个
<!DOCTYPE html>
<html>
<body>
<canvas id="canvas" width="848" height="600" style="border:1px solid black;"></canvas>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="wack-a-circle.js"></script>
<script>
$(function () {
initialize();
});
</script>
</body>
</html>发布于 2015-11-10 19:20:07
你有几个问题,
其中包括:
的分号变量名称
在您的updateCanvas()函数中,您将执行以下操作:
wackableCircle = circles[i];这使得名为wackableCircle的函数未定义,请考虑使用以下命令将其设置为局部变量
var wackableCircle = circles[i];附言-我看不到这是在哪里使用的?
正如Darren Dourley提到的,你的超时函数中缺少一个)。更新
setTimeout(function() {circles.push(new wackableCircle()}, 1000 - (score / 5));使用
setTimeout(function() { circles.push(new wackableCircle()) }, 1000 - (score / 5));这并不是在作秀--但是你遗漏了很多;,如果你缩小你的代码,这将会破坏它。
单行函数使得错误很难被发现,将函数分成多行(特别是setTimeout函数更容易看到问题),下面是修复后的代码
var canvas;
var circles = [];
var circleRadius = 50;
var score = 0;
var xaxis = getRandom(848);
var yaxis = getRandom(600);
function wackableCircle() {
xaxis = getRandom(848);
yaxis = getRandom(600);
if (circles.length > 10) {
alert("You lose!\nScore: " + score)
}
setTimeout(
function () {
circles.push(new wackableCircle())
}, 1000 - (score / 5)
);
}
function getRandom(max) {
return Math.floor((Math.random() * max));
}
function initialize() {
canvas = document.getElementById("canvas");
canvas.addEventListener("mousedown", onMouseDown);
//add new circle every 1000ms
setTimeout(function () {
circles.push(new wackableCircle())
}, 1000);
setInterval(function () {
updateCanvas()
}, 15); //Update canvas every 15ms.
}
//Find difference between two numbers.
function numberDifference(a, b) {
return Math.abs(a - b);
}
function onMouseDown(event) {
for (var i = 0; i < circles.length; i++) {
var wackableCircle = circles[i];
if (numberDifference(event.clientX, xaxis) < circleRadius) {
//The circle's X and the mouse's X are within 50.
if (numberDifference(event.clientY, yaxis) < circleRadius) {
//The circle's Y and the mouse's Y are also with in 50.
circles.splice(i, 1);
}
}
}
alert("click at " + event.clientX + ", " + event.clientY);
}
function updateCanvas() {
//clear canvas
var canvasContext = canvas.getContext("2d");
canvasContext.clearRect(0, 0, 848, 600);
for (var i = 0; i < circles.length; i++) {
var wackableCircle = circles[i];
//Draw circle at wackableCircle's x and y.
canvasContext.beginPath();
canvasContext.arc(xaxis, yaxis, circleRadius, 0, Math.PI * 2);
canvasContext.fill();
}
}https://stackoverflow.com/questions/33627474
复制相似问题