我对编程相对较新,并且正在使用Code.org进行教学。我相信代码是javascript,虽然我不太确定。我所做的程序是一个简单的连接点程序,我现在正试图实现级别。然而,一旦我在一个级别结束并且所有变量都被重置并且屏幕被更改,我就无法点击这些点。
这是因为在此部分代码运行之后:
//Finish the level, change the level
if(clickedX.length >= (level*2+4) - 1 && clickedY.length >= (level*2+4) - 1){
for(var i = 1; i < level*2+4; i++){
deleteElement(i + "Text");
}
clearCanvas("mainCanvas" + level);
level++;
setScreen("level" + level);
setActiveCanvas("mainCanvas" + level);
dotsX = [];
dotsY = [];
levelSector = [10];
levelDiv = 300/(level*2+4);
clickedX = [];
clickedY = [];
createDotCoords();
}
&#13;
它不会返回它最初到达那里的点击事件监听器。这是整个计划,不是很长。我已经用尽了自己的解决方案,发现我现在卡住了。任何帮助是极大的赞赏。感谢。
var dotsX = [];
var dotsY = [];
var level = 1;
var clickedX = [];
var clickedY = [];
var levelSector = [10];
var levelDiv = (300/(level*2+4));
setActiveCanvas("mainCanvas" + level);
createDotCoords();
//Searches an array for a specific number
//Adds and subtracts 4 to allow for the entire circle to be clicked
function searchVar(variable,needle) {
for(var i = 0; i < variable.length; i++){
if(variable[i] === needle){
return true;
}
else{
for(var x = 1; x < 4; x++){
if(variable[i] + x === needle){
return true;
}
if(variable[i] - x === needle){
return true;
}
}
}
}
}
//Create Dot Coordinates and Draw
function createDotCoords(){
levelDiv = Math.round(levelDiv);
for(var i = 1; i < level*2+4; i++){
appendItem(levelSector,levelDiv * i);
appendItem(dotsX,randomNumber(levelSector[i - 1],levelSector[i]));
appendItem(dotsY,randomNumber(40,410));
circle(dotsX[i - 1],dotsY[i - 1],4);
textLabel(i + "Text", i);
setPosition(i + "Text",dotsX[i - 1],dotsY[i - 1],3,4);
}
}
//Get mouse click location and check if the location is the same
//as the dot location. Create a line between that one and the
//previous dot if clicked already
onEvent("mainCanvas" + level, "click", function(event){
var clickX = event.offsetX;
var clickY = event.offsetY;
var booX = false;
var booY = false;
console.log("X " + clickX + " Y " + clickY);
if (searchVar(dotsX,clickX)){
booX = true;
console.log("x is true");
}
if (searchVar(dotsY,clickY)){
booY = true;
console.log("y is true");
}
if(booY && booX){
if(clickedY.length === 0){
appendItem(clickedY,clickY);
}
if(clickedY.length >= 1){
if(searchVar(clickedY,clickY)){
console.log("already in Y");
}
else{
appendItem(clickedY,clickY);
}
}
if(clickedX.length === 0){
appendItem(clickedX,clickX);
}
if(clickedX.length >= 1){
if(searchVar(clickedX,clickX)){
console.log("already in X");
}
else{
appendItem(clickedX,clickX);
}
}
if(clickedX.length === 1 && clickedY.length === 1){
console.log("only one dot clicked");
setStrokeColor("green");
circle(dotsX[0],dotsY[0],4);
setStrokeColor("black");
}
else{
setStrokeColor("green");
circle(dotsX[clickedX.length - 1], dotsY[clickedY.length - 1], 4);
setStrokeColor("black");
line(dotsX[clickedX.length - 2], dotsY[clickedY.length - 2], dotsX[clickedX.length - 1], dotsY[clickedY.length - 1]);
}
}
//Finish the level, change the level
if(clickedX.length >= (level*2+4) - 1 && clickedY.length >= (level*2+4) - 1){
for(var i = 1; i < level*2+4; i++){
deleteElement(i + "Text");
}
clearCanvas("mainCanvas" + level);
level++;
setScreen("level" + level);
setActiveCanvas("mainCanvas" + level);
dotsX = [];
dotsY = [];
levelSector = [10];
levelDiv = 300/(level*2+4);
clickedX = [];
clickedY = [];
createDotCoords();
}
});
&#13;
关于格式化/一般逻辑/编程礼仪的任何提示,这样的事情,也会非常有用。
答案 0 :(得分:0)
我的猜测是,在你重置变量和画布时,某些东西会妨碍点击监听器,或者响应中显示的内容。你能在这里发布项目的分享链接吗?我可以看看代码。 (我每天都在App Lab工作。所以我可以帮助代码,或者弄清楚它是否是一个真正的错误。)