我是JavaScript新手,無法找到如何爲我的節奏遊戲調整具有一個功能的多個元素的大小。這是針對我的CSP類的,並且不會使用jQuery。我也僅限於我使用的程序(AppLab)提供的命令。我現在的目標是讓一個圈子的「動畫」增長到所需的大小,以表明它應該被點擊。我需要這些元素出現,而另一個元素正在增長等等。Javascript:在多個元素上使用一個函數
我知道我的代碼可能很糟糕,所以如果還有一種方法來簡化或改進它,我很想知道。
這是我目前的程序代碼和hitIndicator功能是我遇到的最麻煩與一個:
var circleSizeW = 0;
var circleSizeL = 0;
var score = 0;
hitCircle("hitcircle", 300, 6, 206);
hitCircle("image2", 300, 6, 682);
function circleEffects(circleid, whentohit) {
setTimeout(function() {
onEvent(circleid, "click", function() {
playSound("47 (1).mp3", false);
hideElement(circleid);
});
}, whentohit);
}
function hitIndicator(circleid, growthRate) {
var xPos = getXPosition(circleid);
var yPos = getYPosition(circleid);
var t = setInterval(function() {
circleSizeW = circleSizeW + growthRate;
circleSizeL = circleSizeL + growthRate;
xPos = xPos - (growthRate/2);
yPos = yPos - (growthRate/2);
showElement(circleid);
setSize(circleid, circleSizeW, circleSizeL);
setPosition(circleid, xPos, yPos);
if (circleSizeW >= 60) {
clearInterval(t);
circleSizeW = 0;
circleSizeL = 0;
}
}, 50);
}
function scoreSystem(circleid, whentohit) {
setTimeout(function() {
onEvent(circleid, "click", function() {
score = score + 100;
setText("scoreTrack", score);
});
}, whentohit);
}
function hitCircle(circleid, whentohit, growthRate, appearancetime) {
setTimeout(function() {
console.log("hitcircle");
circleEffects(circleid, whentohit);
hitIndicator(circleid, growthRate);
scoreSystem(circleid, whentohit);
}, appearancetime);
我的代碼是隔靴搔癢完成要麼所以還是有很多東西需要被完成。 我不知道如何讓多個圓圈在相似的時間運行該功能,因爲當我嘗試修復錯誤/更改函數參數值時,它們有時會循環兩次,無限循環或接收之前更改的值而之前的圈子還在增長。