我正在尝试制作这个 "fps" 游戏,但出于某种原因,mouseClicked 功能无法正常工作
I am trying to make this "fps" game, and for some reason the mouseClicked function is not working
当我出于某种原因尝试执行此代码时,这部分代码:(RX1 = random x 1, RY1 = random y 1)(“+50 部分在那里,因为矩形的宽度为 50)
if(mouseClicked && mouseX > RX1 && mouseX < RX1 + 50 && mouseY > RY1 && mouseY < RY1 + 50){
fill(0, 0, 0);
rect(RX1, RY1, 50, 50);
}
不起作用,程序只是忽略它,我把它放在我能想到的任何地方,非常感谢有人的帮助,谢谢!
这是剩余的代码,以备不时之需(除了 if mouseclicked 部分之外的所有内容都有效
//random x and y variables for targets
var RX1 = random(25, 375);
var RX2 = random(25, 375);
var RX3 = random(25, 375);
var RX4 = random(25, 375);
var RY1 = random(25, 375);
var RY2 = random(25, 375);
var RY3 = random(25, 375);
var RY4 = random(25, 375);
//the juicy stuff
draw = function() {
//coloring
background(255, 255, 255);
fill(255, 0, 0);
//targets:
rect(RX1, RY1, 50, 50);
rect(RX2, RY2, 50, 50);
rect(RX3, RY3, 50, 50);
rect(RX4, RY4, 50, 50);
//gun
fill(0, 0, 0);
//front sight
rect(mouseX - 5, mouseY, 10, 20);
fill(255, 255, 255);
ellipse(mouseX + 0.5, mouseY + 5, 5, 5);
fill(0, 0, 0);
//back sights
rect(mouseX + 10, mouseY, 20, 20);
rect(mouseX - 30, mouseY, 20, 20);
//slide
rect(mouseX - 35, mouseY + 20, 70, 50);
//grip ledge
fill(77, 77, 77);
rect(mouseX - 35, mouseY + 71, 70, 20);
fill(0, 0, 0);
//grip
rect(mouseX - 30, mouseY + 90, 60, 100);
};
//on mouse click/during shooting
if(mouseClicked && mouseX > RX1 && mouseX < RX1 + 50 && mouseY > RY1 && mouseY < RY1 + 50 ){
fill(0, 0, 0);
rect(RX1, RY1, 50, 50);
}
P.S。我知道我只为一个矩形制作了最后一个片段,没有一个可以以任何方式工作
我想我已经让程序响应鼠标点击。问题似乎是您检查鼠标单击的 if 语句不在 draw 函数之外,该函数永远循环,因此它从不检查 if 语句。我尝试将该 if 语句放在 mousePressed 函数中(无论您在代码中的什么位置,它都会在单击鼠标时被调用),它现在似乎可以工作(或者至少它在 if 语句中执行代码):
//variables
//random x and y variables for targets
var RX1 = random(25, 375);
var RX2 = random(25, 375);
var RX3 = random(25, 375);
var RX4 = random(25, 375);
var RY1 = random(25, 375);
var RY2 = random(25, 375);
var RY3 = random(25, 375);
var RY4 = random(25, 375);
//the juicy stuff
draw = function() {
//coloring
background(255, 255, 255);
fill(255, 0, 0);
//targets:
rect(RX1, RY1, 50, 50);
rect(RX2, RY2, 50, 50);
rect(RX3, RY3, 50, 50);
rect(RX4, RY4, 50, 50);
//gun
fill(0, 0, 0);
//front sight
rect(mouseX - 5, mouseY, 10, 20);
fill(255, 255, 255);
ellipse(mouseX + 0.5, mouseY + 5, 5, 5);
fill(0, 0, 0);
//back sights
rect(mouseX + 10, mouseY, 20, 20);
rect(mouseX - 30, mouseY, 20, 20);
//slide
rect(mouseX - 35, mouseY + 20, 70, 50);
//grip ledge
fill(77, 77, 77);
rect(mouseX - 35, mouseY + 71, 70, 20);
fill(0, 0, 0);
//grip
rect(mouseX - 30, mouseY + 90, 60, 100);
};
//on mouse click/during shooting
mousePressed = function() {
if(mouseX > RX1 && mouseX < RX1 + 50 && mouseY > RY1 && mouseY < RY1 + 50){
fill(0, 0, 0);
rect(RX1, RY1, 50, 50);
}
};
当我出于某种原因尝试执行此代码时,这部分代码:(RX1 = random x 1, RY1 = random y 1)(“+50 部分在那里,因为矩形的宽度为 50)
if(mouseClicked && mouseX > RX1 && mouseX < RX1 + 50 && mouseY > RY1 && mouseY < RY1 + 50){
fill(0, 0, 0);
rect(RX1, RY1, 50, 50);
}
不起作用,程序只是忽略它,我把它放在我能想到的任何地方,非常感谢有人的帮助,谢谢!
这是剩余的代码,以备不时之需(除了 if mouseclicked 部分之外的所有内容都有效
//random x and y variables for targets
var RX1 = random(25, 375);
var RX2 = random(25, 375);
var RX3 = random(25, 375);
var RX4 = random(25, 375);
var RY1 = random(25, 375);
var RY2 = random(25, 375);
var RY3 = random(25, 375);
var RY4 = random(25, 375);
//the juicy stuff
draw = function() {
//coloring
background(255, 255, 255);
fill(255, 0, 0);
//targets:
rect(RX1, RY1, 50, 50);
rect(RX2, RY2, 50, 50);
rect(RX3, RY3, 50, 50);
rect(RX4, RY4, 50, 50);
//gun
fill(0, 0, 0);
//front sight
rect(mouseX - 5, mouseY, 10, 20);
fill(255, 255, 255);
ellipse(mouseX + 0.5, mouseY + 5, 5, 5);
fill(0, 0, 0);
//back sights
rect(mouseX + 10, mouseY, 20, 20);
rect(mouseX - 30, mouseY, 20, 20);
//slide
rect(mouseX - 35, mouseY + 20, 70, 50);
//grip ledge
fill(77, 77, 77);
rect(mouseX - 35, mouseY + 71, 70, 20);
fill(0, 0, 0);
//grip
rect(mouseX - 30, mouseY + 90, 60, 100);
};
//on mouse click/during shooting
if(mouseClicked && mouseX > RX1 && mouseX < RX1 + 50 && mouseY > RY1 && mouseY < RY1 + 50 ){
fill(0, 0, 0);
rect(RX1, RY1, 50, 50);
}
P.S。我知道我只为一个矩形制作了最后一个片段,没有一个可以以任何方式工作
我想我已经让程序响应鼠标点击。问题似乎是您检查鼠标单击的 if 语句不在 draw 函数之外,该函数永远循环,因此它从不检查 if 语句。我尝试将该 if 语句放在 mousePressed 函数中(无论您在代码中的什么位置,它都会在单击鼠标时被调用),它现在似乎可以工作(或者至少它在 if 语句中执行代码):
//variables
//random x and y variables for targets
var RX1 = random(25, 375);
var RX2 = random(25, 375);
var RX3 = random(25, 375);
var RX4 = random(25, 375);
var RY1 = random(25, 375);
var RY2 = random(25, 375);
var RY3 = random(25, 375);
var RY4 = random(25, 375);
//the juicy stuff
draw = function() {
//coloring
background(255, 255, 255);
fill(255, 0, 0);
//targets:
rect(RX1, RY1, 50, 50);
rect(RX2, RY2, 50, 50);
rect(RX3, RY3, 50, 50);
rect(RX4, RY4, 50, 50);
//gun
fill(0, 0, 0);
//front sight
rect(mouseX - 5, mouseY, 10, 20);
fill(255, 255, 255);
ellipse(mouseX + 0.5, mouseY + 5, 5, 5);
fill(0, 0, 0);
//back sights
rect(mouseX + 10, mouseY, 20, 20);
rect(mouseX - 30, mouseY, 20, 20);
//slide
rect(mouseX - 35, mouseY + 20, 70, 50);
//grip ledge
fill(77, 77, 77);
rect(mouseX - 35, mouseY + 71, 70, 20);
fill(0, 0, 0);
//grip
rect(mouseX - 30, mouseY + 90, 60, 100);
};
//on mouse click/during shooting
mousePressed = function() {
if(mouseX > RX1 && mouseX < RX1 + 50 && mouseY > RY1 && mouseY < RY1 + 50){
fill(0, 0, 0);
rect(RX1, RY1, 50, 50);
}
};