javascript - mousePressed 在 JavaScript 中的构造函数内部 - p5.js
问题描述
我目前正在研究 JavaScript 中的构造函数 - p5.js。我的问题是,mousePressed()
当我点击画布上的正确位置时,该函数不会执行其中的代码。基本上我想要的是当我在杯子上单击鼠标时,杯子中的内容会消失。当我按下按钮时,我想把杯子装满原来的样子。
这是我的文件:
var bierglas;
var füllung;
var xPos = 200;
var yPos = 100;
function setup() {
createCanvas(650, 450);
bierglas = new Cup();
füllung = new Content();
}
function draw() {
background(51);
füllung.show();
füllung.button();
füllung.move();
bierglas.square();
bierglas.henkel();
}
//
// This draws my cup
//
function Cup() {
this.x = xPos;
this.y = yPos;
this.width = 150;
this.height = 300;
this.square = function() {
fill(255, 150);
rect(this.x, this.y, this.width, this.height);
};
this.henkel = function() {
arc(this.x + this.width, this.y + this.height / 2, 150, 120, 0, HALF_PI);
arc(this.x + this.width, this.y + this.height / 2, 150, 120, 0, 0);
};
}
//
// This is for the mousePressed function, the content of the cup and the button
//
function Content() {
this.x = xPos;
this.y = yPos;
this.width = 150;
this.height = 300;
this.buttonX = width - width / 4;
this.buttonY = height - height / 6;
this.buttonWidth = width / 8;
this.buttonHeight = height / 6;
this.show = function() {
fill(0, 200, 200);
rect(this.x, this.y * 2, this.width, this.height - this.y);
};
this.button = function() {
fill(255, 0, 0);
rect(this.buttonX, this.buttonY, this.buttonWidth, this.buttonHeight);
};
this.move = function() {
mousePressed();
};
}
function mousePressed() {
if (
mouseX < this.x + this.width &&
mouseX > this.x &&
mouseY < this.y + (this.height - this.y) &&
mouseY > this.y
) {
this.y * 2;
console.log("Auf Bierglas getippt");
}
if (
mouseX > this.buttonX &&
mouseX < this.buttonX + this.buttonWidth &&
mouseY > this.buttonY &&
mouseY < this.buttonY + this.buttonHeight
) {
this.y = yPos;
console.log("Auf Button getippt");
}
}
解决方案
你应该养成检查开发者控制台错误的习惯。您会看到您的mousePressed()
函数确实在触发,但它遇到了错误。
让我们看一下mousePressed()
函数的前几行:
function mousePressed() {
if (
mouseX < this.x + this.width &&
mouseX > this.x &&
mouseY < this.y + (this.height - this.y) &&
mouseY > this.y
) {
this.y * 2;
你在这里有几个问题。你认为this
是什么?我猜你认为它是 的一个实例Content
,但事实并非如此。因此,this
不包含 , , 或 等变量x
,y
这width
就是height
您收到错误的原因。另外,请注意this.y * 2
实际上并没有对结果值做任何事情。
如果我是你,我会把你的问题分解成更小的步骤,并一次一步地采取这些步骤。尝试得到一个使用mousePressed()
工作的非常简单的草图:
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
}
function mousePressed(){
console.log("mouse pressed");
}
接下来,尝试创建一个简单的草图,其中包含一个对象,该对象具有您从草图级函数调用的mousePressed()
函数。这是一个例子:
var myObject = new MyObject();
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
}
function mousePressed(){
myObject.mousePressed();
}
function MyObject(){
this.mousePressed = function(){
console.log("object mouse pressed");
};
}
像这样以小步骤向前迈进,并记住始终检查开发人员控制台是否有错误。祝你好运。
推荐阅读
- python - 切片类似于 numpy np.ix_ 的 2D 张量
- flutter - 在 Flutter 应用中使用 Stream 填充 TextEditingController
- javascript - 用 am pm 格式化字符串日期
- node.js - mongodb 接受所有数据类型值但是定义的类型是数字它应该只接受数字类型
- python - 如何将 Splunk 自定义报告命令的输出通过管道传输到另一个 Splunk 命令?
- javascript - 将 plotly 条形图保存到 json 并导入 html 文件会删除条形标签
- html - 如何将我的表情符号与标题文本对齐?
- java - 无法在 Spring 2.5 App 上运行 Java 7。说它需要 Java 1.5 或更高版本
- javascript - 无法使用 @kubernetes/client-node 获取集群内的所有作业
- java - 如何使用 Kotlin 和 Retrofit 在 Android 中绕过 HTTPS SSL 证书验证