[英]How do I implement a click counter/scorekeeper with ONLY JS (if it is possible)?
我正在嘗試完成我正在創建的計算機游戲的主要框架,並試圖在其中實現“得分”或價值觀以使其更具功能性。
我有一些實際有效的基本代碼,但不完全是我想要的。
這是我一直在使用的代碼(所有變量都已聲明)
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 600;
this.canvas.height = 480;
//this.canvas.style.cursor = "none"; //hide the original cursor
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.interval = setInterval(updateGameArea, 1);
window.addEventListener('mousedown', function (e) {
myGameArea.x = e.pageX;
myGameArea.y = e.pageY;
console.log("hello");
});
window.addEventListener('mouseup', function (e) {
myGameArea.x = false;
myGameArea.y = false;
});
},
clear : function(){
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.update = function() {
ctx = myGameArea.context;
if (this.type == "text") {
ctx.font = this.width + " " + this.height;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
} else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.clicked = function() {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var clicked = true;
if ((mybottom < myGameArea.y) || (mytop > myGameArea.y) || (myright < myGameArea.x) || (myleft > myGameArea.x)) {
clicked = false;
}
return clicked;
}
}
function updateGameArea() {
myGameArea.clear();
if (myGameArea.x && myGameArea.y) {
if (item1.clicked()) {
console.log("red square says hi");
myScore = myScore + 1;
}
if (item2.clicked()) {
console.log("orange square says hi");
}
if (item3.clicked()) {
console.log("yellow square says hi");
}
if (item4.clicked()) {
console.log("green square says hi");
}
if (item5.clicked()) {
console.log("blue square says hi");
}
if (item6.clicked()) {
console.log("purple square says hi");
}
}
scoreDisplay.text = "Score: " + myScore;
scoreDisplay.update();
myGamePiece.update();
item1.update();
item2.update();
item3.update();
item4.update();
item5.update();
item6.update();
itemArea.update();
orderScreen.update();
}
當我單擊紅色正方形(item1)時,分數會增加,但直到停止單擊或鼠標向上時它才會停止。 我希望我的代碼只需單擊對象一次即可增加該值,而不是鼠標按下時增加該值。
您想要發生的事情背后的邏輯幾乎是這樣的:
JavaScript:
var i = 0;
function addPoint() {
if (document.getElementById("value").value != null) {
document.getElementById("value").value = ++i;
} else { return false }
}
HTML:
<button onclick="addPoint()">+1 Point</button>
<input type="text" id="value" value="0"></input>
如果您想要更“完整”的方法,可以在JavaScript部分執行此操作:
function addPoint() {
var addValue = parseInt(document.getElementById("value").value, 10);
addValue = isNaN(addValue) ? 0 : addValue;
addValue++;
document.getElementById("value").value = addValue;
}
像對事件mousedown
和mouseup
一樣使用事件偵聽器:
這將捕獲畫布內部而不是外部的每次單擊。
在 myGameArea
對象中 定義一個變量 :
var myGameArea = {
canvas : document.createElement("canvas"),
clicks : 0, //HERE
start : function() {
this.canvas.width = 600;
this.canvas.height = 480;
...
然后,在 start()
函數 上添加事件偵聽器 :
var myGameArea = {
canvas : document.createElement("canvas"),
clicks : 0,
start : function() {
this.canvas.width = 600;
this.canvas.height = 480;
//this.canvas.style.cursor = "none"; //hide the original cursor
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.interval = setInterval(updateGameArea, 1);
window.addEventListener('mousedown', function (e) {
myGameArea.x = e.pageX;
myGameArea.y = e.pageY;
console.log("hello");
});
this.canvas.addEventListener("click",(e)=>{ // Use ES6 arrow function
this.clicks++;
});
...
看一個例子:
var myGameArea = { canvas : document.querySelector("canvas"), clicks : 0, start : function(){ this.canvas.addEventListener("click",(e)=>{ // Use ES6 arrow function this.clicks++; console.log(`Click+1, now = ${this.clicks}`); }); } }; myGameArea.start();
canvas{ background: black; }
<canvas width="300" height="300"></canvas>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.