p5.js 中逻辑门模拟器的对象生成错误
Error in object generation to logic gates simulator in p5.js
在我的逻辑门模拟器中,我正在做另一个对象生成到 canvas 我正在做一个频率发生器但是当我在按钮上有 onclick 函数以便生成它时,我的频率确实不工作,整个对象不工作。有人会告诉我谢谢你。
整个项目的link在线p5.js编辑器在这里:https://editor.p5js.org/jakubmitrega1/sketches/Mg1BGpimz
实体生成函数:
function createEntity(type) {
if (type === "switch") {
entities.push(new Switch(100, 100));
console.log("Vytvořil si switch!")
} else if (type === "light") {
entities.push(new Light(100, 200));
console.log("Vytvořil si light!")
} else if (type === "generator") {
entities.push(new Generator(100, 300));
console.log("Vytvořil si light!")
}
}
生成器代码:
class Generator extends Entity {
constructor (x, y, frequency) {
super(x, y, 32, 32);
this.frequency = frequency;
this.state = 0;
this.output = new Pin(x, y, OUTPUT);
}
draw() {
this.output.pos.x = this.pos.x+32;
this.output.pos.y = this.pos.y;
this.output.draw();
this.state = millis()%(2000/this.frequency)<(1000/this.frequency) ? 1 : 0;
push();
translate(this.pos.x, this.pos.y);
if (this.mouseOver()) {
stroke('white');
} else {
noStroke();
}
fill('#888');
rectMode(CENTER);
rect(0, 0, 32, 32);
noStroke();
fill(255);
textAlign(CENTER, CENTER);
textSize(24);
text(this.state, 0, 0);
textSize(12);
textAlign(CENTER, TOP);
text('Freq: ' + this.frequency + ' hz', 0, 20);
pop();
}
mousePressed() {
this.output.mousePressed();
}
mouseReleased() {
this.output.mouseReleased();
}
}
HTML代码:
<body>
<div class="menu">
<button onclick="switchMode('hand')">Hand <i class="far fa-hand-paper"></i></button>
<button onclick="switchMode('move')">Move <i class="fas fa-arrows-alt"></i></button>
<button onclick="switchMode('wire')">Wire <i class="fas fa-plug"></i></button>
<button onclick="switchMode('jsonexport')">JSON export <i class="fas fa-file-export"></i></button>
<button onclick="createEntity('switch')">Switch</button>
<button onclick="createEntity('light')">Light</button>
<button onclick="createEntity('generator')">Generator</button>
</div>
<br>
</body>
这一行:
entities.push(new Generator(100, 300));
你没有将频率传递给发电机
entities.push(new Generator(100, 300, 10));
应该可以吗?
在我的逻辑门模拟器中,我正在做另一个对象生成到 canvas 我正在做一个频率发生器但是当我在按钮上有 onclick 函数以便生成它时,我的频率确实不工作,整个对象不工作。有人会告诉我谢谢你。
整个项目的link在线p5.js编辑器在这里:https://editor.p5js.org/jakubmitrega1/sketches/Mg1BGpimz
实体生成函数:
function createEntity(type) {
if (type === "switch") {
entities.push(new Switch(100, 100));
console.log("Vytvořil si switch!")
} else if (type === "light") {
entities.push(new Light(100, 200));
console.log("Vytvořil si light!")
} else if (type === "generator") {
entities.push(new Generator(100, 300));
console.log("Vytvořil si light!")
}
}
生成器代码:
class Generator extends Entity {
constructor (x, y, frequency) {
super(x, y, 32, 32);
this.frequency = frequency;
this.state = 0;
this.output = new Pin(x, y, OUTPUT);
}
draw() {
this.output.pos.x = this.pos.x+32;
this.output.pos.y = this.pos.y;
this.output.draw();
this.state = millis()%(2000/this.frequency)<(1000/this.frequency) ? 1 : 0;
push();
translate(this.pos.x, this.pos.y);
if (this.mouseOver()) {
stroke('white');
} else {
noStroke();
}
fill('#888');
rectMode(CENTER);
rect(0, 0, 32, 32);
noStroke();
fill(255);
textAlign(CENTER, CENTER);
textSize(24);
text(this.state, 0, 0);
textSize(12);
textAlign(CENTER, TOP);
text('Freq: ' + this.frequency + ' hz', 0, 20);
pop();
}
mousePressed() {
this.output.mousePressed();
}
mouseReleased() {
this.output.mouseReleased();
}
}
HTML代码:
<body>
<div class="menu">
<button onclick="switchMode('hand')">Hand <i class="far fa-hand-paper"></i></button>
<button onclick="switchMode('move')">Move <i class="fas fa-arrows-alt"></i></button>
<button onclick="switchMode('wire')">Wire <i class="fas fa-plug"></i></button>
<button onclick="switchMode('jsonexport')">JSON export <i class="fas fa-file-export"></i></button>
<button onclick="createEntity('switch')">Switch</button>
<button onclick="createEntity('light')">Light</button>
<button onclick="createEntity('generator')">Generator</button>
</div>
<br>
</body>
这一行:
entities.push(new Generator(100, 300));
你没有将频率传递给发电机
entities.push(new Generator(100, 300, 10));
应该可以吗?