为了在 TweenMax 上进行一些测试,我正在做一个简单的“捕捉颜色”游戏,但是看起来我在代码中犯了一些错误,因为一切都很好,但一个 if 语句应该结束游戏。我想问题不在于颜色更改器(mutator)本身,而在于我放置 if 语句的位置。我对编程还很陌生,所以我很有可能犯了一些愚蠢的错误。
您可以在此处查看整个页面:https://codepen.io/IvanRoselli/pen/vbqjoo
这是我使用的 Javascript
var colorCircle = document.getElementById('circle');
var startBtn = document.getElementById('start-btn');
var catchBtn = document.getElementById('catch-btn');
var mainTitle = document.getElementById('main-title');
var colorChange = new TimelineMax({repeat: -1});
var circleBackgroundColor = document.getElementById('circle').style.backgroundColor;
var colorCircle = document.getElementById('circle');
var startBtn = document.getElementById('start-btn');
var catchBtn = document.getElementById('catch-btn');
var mainTitle = document.getElementById('main-title');
var colorChange = new TimelineMax({repeat: -1});
var circleBackgroundColor =
document.getElementById('circle').style.backgroundColor;
function playPause(){
colorChange.paused(!colorChange.paused());
}
function getIt(){
$('#catch-btn').click(function(){
if(circleBackgroundColor === 'blue'){
mainTitle.innerHTML = "YOU WON!";
return;
} else {
mainTitle.innerHTML = "Try again pressing CATCH";
playPause();
}
})
}
function startGame(){
getIt();
$('#start-btn').css("display", "none");
$('#catch-btn').css("display", "inline");
$(colorCircle).css("cursor", "pointer");
colorChange.from(colorCircle, .5, {backgroundColor: 'orange', ease: SteppedEase.config(1)});
colorChange.to(colorCircle, .5, {backgroundColor: 'red', ease: SteppedEase.config(1)});
colorChange.to(colorCircle, .5, {backgroundColor: 'pink', ease: SteppedEase.config(1)});
colorChange.to(colorCircle, .5, {backgroundColor: 'yellow', ease: SteppedEase.config(1)});
colorChange.to(colorCircle, .5, {backgroundColor: 'blue', ease: SteppedEase.config(1)});
colorChange.to(colorCircle, .5, {backgroundColor: 'green', ease: SteppedEase.config(1)});
colorChange.to(colorCircle, .5, {backgroundColor: 'cyan', ease: SteppedEase.config(1)});
};
$('#start-btn').click(function() {
startGame();
});
基本上,如果您捕捉到蓝色,H1 应该在“您赢了”或“再试一次”(如果您捕捉到其他颜色)之间变化。
预先感谢您的帮助和建议。
最佳答案
I have made some changes and its working fine for me.
https://codepen.io/anon/pen/NoZBPz
问题是它给出了 rgb 值,并且应用程序也没有停止,也改变了 if 条件。
if((document.getElementById('circle').style.backgroundColor) === 'rgb(0, 0, 255)'){
mainTitle.innerHTML = "YOU WON!";
playPause();
return;
} else {
mainTitle.innerHTML = "Try again pressing CATCH";
playPause();
}
关于javascript - 如果语句放错了地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54829361/