Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使我的if语句在我的游戏中起作用?_Javascript_Html_If Statement - Fatal编程技术网

Javascript 如何使我的if语句在我的游戏中起作用?

Javascript 如何使我的if语句在我的游戏中起作用?,javascript,html,if-statement,Javascript,Html,If Statement,我正在做一个彩色序列游戏。玩家将获得四种随机颜色,并且必须单击每个块以获得正确的颜色序列。除了获胜的if声明外,一切都正常。出于测试目的,我使用了一个警告,当玩家获胜时会显示“耶”。不幸的是,它不起作用。我尝试了多种方法来实现这一目标。我的if语句位于代码的底部 Javascript: <script> var colors; var o; var t; var th; var f; var oa; var ta; var tha; var fa; function sequence

我正在做一个彩色序列游戏。玩家将获得四种随机颜色,并且必须单击每个块以获得正确的颜色序列。除了获胜的if声明外,一切都正常。出于测试目的,我使用了一个警告,当玩家获胜时会显示“耶”。不幸的是,它不起作用。我尝试了多种方法来实现这一目标。我的if语句位于代码的底部

Javascript:

<script>
var colors;
var o;
var t;
var th;
var f;
var oa;
var ta;
var tha;
var fa;
function sequence(){
oa = colors[Math.floor(Math.random()*colors.length)];
ta = colors[Math.floor(Math.random()*colors.length)];
tha = colors[Math.floor(Math.random()*colors.length)];
fa = colors[Math.floor(Math.random()*colors.length)];
document.getElementById('sequence').innerHTML = "1:" + oa + " 2:" + ta + " 3:" + tha + " 4:" + fa;
};
window.onload = function(){
randomize();
sequence();
};
function randomize(){
colors = ['#ff0000','#ff5500','#ffff00','#00ff00','#0000ff','#8c00ff'];
o = colors[Math.floor(Math.random()*colors.length)];
t = colors[Math.floor(Math.random()*colors.length)];
th = colors[Math.floor(Math.random()*colors.length)];
f = colors[Math.floor(Math.random()*colors.length)];
one = document.getElementById('one').style.background = o;
two = document.getElementById('two').style.background = t;
three = document.getElementById('three').style.background = th;
four = document.getElementById('four').style.background = f;
}
function changeOne(){
o = colors[Math.floor(Math.random()*colors.length)];
document.getElementById('one').style.background = o;
}
function changeTwo(){
t = colors[Math.floor(Math.random()*colors.length)];
document.getElementById('two').style.background = t;
}
function changeThree(){
th = colors[Math.floor(Math.random()*colors.length)];
document.getElementById('three').style.background = th;
}
function changeFour(){
f = colors[Math.floor(Math.random()*colors.length)];
document.getElementById('four').style.background = f;
}
//this is the part I need help with
if(o==oa && t==ta && th==tha && f==fa){
alert("yay!");
}
</script>

变异颜色;
var o;
变量t;
var-th;
var f;
var-oa;
var-ta;
var-tha;
var-fa;
函数序列(){
oa=颜色[Math.floor(Math.random()*colors.length)];
ta=colors[Math.floor(Math.random()*colors.length)];
tha=颜色[Math.floor(Math.random()*colors.length)];
fa=colors[Math.floor(Math.random()*colors.length)];
document.getElementById('sequence').innerHTML=“1:+oa+”2:+ta+”3:+tha+”4:+fa;
};
window.onload=函数(){
随机化();
序列();
};
函数随机化(){
颜色=['#ff0000'、'#ff5500'、'#ffff00'、'#00ff00'、'#0000ff'、'#8c00ff'];
o=颜色[Math.floor(Math.random()*colors.length)];
t=colors[Math.floor(Math.random()*colors.length)];
th=colors[Math.floor(Math.random()*colors.length)];
f=颜色[Math.floor(Math.random()*colors.length)];
one=document.getElementById('one').style.background=o;
two=document.getElementById('two').style.background=t;
three=document.getElementById('three').style.background=th;
four=document.getElementById('four').style.background=f;
}
函数changeOne(){
o=颜色[Math.floor(Math.random()*colors.length)];
document.getElementById('one').style.background=o;
}
函数changeTwo(){
t=colors[Math.floor(Math.random()*colors.length)];
document.getElementById('two').style.background=t;
}
函数转换三(){
th=colors[Math.floor(Math.random()*colors.length)];
document.getElementById('three').style.background=th;
}
函数changefore(){
f=颜色[Math.floor(Math.random()*colors.length)];
document.getElementById('four').style.background=f;
}
//这是我需要帮助的部分
如果(o==oa&&t==ta&&th==tha&&f==fa){
警惕(“耶!”);
}

可能是因为您选择的颜色数组由彼此非常相似的颜色组成。无论如何,我更改了你的代码并添加了一个html。看看它是否是你想要的:

var颜色;
var o;
变量t;
var-th;
var f;
var-oa;
var-ta;
var-tha;
var-fa;
函数序列(){
oa=colors[Math.floor(Math.random()*(colors.length))];
ta=colors[Math.floor(Math.random()*(colors.length))];
tha=颜色[Math.floor(Math.random()*(colors.length))];
fa=colors[Math.floor(Math.random()*(colors.length))];
var oaDiv=document.createElement(“div”);
oaDiv.style.backgroundColor=oa;
var taDiv=document.createElement(“div”);
taDiv.style.backgroundColor=ta;
var thaDiv=document.createElement(“div”);
thaDiv.style.backgroundColor=tha;
var faDiv=document.createElement(“div”);
faDiv.style.backgroundColor=fa;
oaDiv.classList.add(“colorDiv1”);
taDiv.classList.add(“colorDiv1”);
添加(“colorDiv1”);
faDiv.classList.add(“colorDiv1”);
document.getElementById('sequence').appendChild(oaDiv);
document.getElementById('sequence').appendChild(taDiv);
document.getElementById('sequence').appendChild(thaDiv);
document.getElementById('sequence').appendChild(faDiv);
//document.getElementById('sequence').innerHTML=“1:+oa+”2:+ta+”3:+tha+”4:+fa;
};
window.onload=函数(){
随机化();
序列();
};
函数随机化(){
颜色=['#ff0000'、'#d500ff'、'#ffff00'、'#00ff00'、'#0000ff'、'#ff00f3'];
o=颜色[Math.floor(Math.random()*(colors.length))];
t=colors[Math.floor(Math.random()*(colors.length))];
th=colors[Math.floor(Math.random()*(colors.length))];
f=颜色[Math.floor(Math.random()*(colors.length))];
document.getElementById('one').style.backgroundColor=o;
document.getElementById('two').style.backgroundColor=t;
document.getElementById('three').style.backgroundColor=th;
document.getElementById('four').style.backgroundColor=f;
}
document.getElementById('one')。addEventListener(“单击”,函数)(){
o=颜色[Math.floor(Math.random()*(colors.length))];
document.getElementById('one').style.backgroundColor=o;
结果集();
});
document.getElementById('two')。addEventListener(“单击”,函数(){
t=colors[Math.floor(Math.random()*(colors.length))];
document.getElementById('two').style.backgroundColor=t;
结果集();
});
document.getElementById('three')。addEventListener(“单击”),函数(){
th=colors[Math.floor(Math.random()*(colors.length))];
document.getElementById('three').style.backgroundColor=th;
结果集();
});
document.getElementById('four')。addEventListener(“单击”),函数(){
f=颜色[Math.floor(Math.random()*(colors.length))];
document.getElementById('four').style.backgroundColor=f;
结果集();
});
//这是我需要帮助的部分
函数结果集(){
如果(o==oa&&t==ta&&th==tha&&f==fa){
控制台日志(“yay”);
警惕(“耶!”);
}
}
.seq{
位置:绝对位置;
}
.序号2{
位置:相对位置;
利润上限:70像素;
}
.colorDiv1{
显示:块;
高度:50px;
宽度:50px;
浮动:左;
利润率:10px;
}
colorDiv先生{
显示:块;
高度:50px;
宽度:50px;
位置:相对位置;
浮动:左;
利润率:10px;
}

第2节:JavaScript语言基础


请尝试使用浏览器的。
if
语句究竟是如何工作的?if语句在window.onload函数执行之前只运行一次-也许这就回答了您的问题。。。p、 学习格式化代码,但这行不通。还有其他建议吗?试着为你的if语句创建一个函数,并像调用其他不起作用的函数一样调用它……嘿@rez shahr,这是一个很好的起点,但也许你可以将它转换成一个适当的SO片段,然后你和OP就可以了