我正在做一個邏輯門模擬器,我已經有了開關、燈等物體和布線等功能。但是我想通過您單擊的按鈕創建像開關和燈這樣的物體,并且將為您創建物件或物體,但它對我不起作用。有人幫不了我。提前致謝。我通過鏈接附加的代碼會將您帶到 p5.js 在線編輯器中的整個代碼和模擬器。
完整代碼: https : //editor.p5js.org/jakubmitrega1/sketches/Mg1BGpimz
相關代碼:
let entities = [];
function createEntity() {
if (mousePressed == "Light") {
entities.push(new Switch(100, 100))
}
if (mousePressed == "Light") {
entities.push(new Light(100, 200))
}
}
<div class="menu">
<button onclick="createEntity('switch')">Switch</button>
<button onclick="createEntity('light')">Light</button>
</div>
uj5u.com熱心網友回復:
一旦你隔離了有問題的代碼,很明顯為什么這不起作用:
let entities = [];
function createEntity() {
// Obvious typo here: "Light" instead of "Switch"
// However, bigger issue, why would mousePressed be equal to any kind of
// string at all? Much less one that will help you determine which button
// was pressed.
if (mousePressed == "Light") {
entities.push(new Switch(100, 100))
}
if (mousePressed == "Light") {
entities.push(new Light(100, 200))
}
}
由于您將物體型別作為引數傳遞給createEntity函式,因此合乎邏輯的做法是讓它實際指定一個引數并檢查它而不是mousePressed:
let entities = [];
function createEntity(type) {
// Note: string comparison is case sensitive.
if (type === "switch") {
alert("Create a Switch");
} else if (type === "light") {
alert("Create a Light");
}
}
<div class="menu">
<button onclick="createEntity('switch')">Switch</button>
<button onclick="createEntity('light')">Light</button>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/388663.html
標籤:javascript html 按钮 p5.js
