我正在尝试制作一个简单的游戏,其中背景每 20 次跳跃就会改变一次。有这个问题。当跳跃计数器达到 20 时,背景不会改变。我不知道我做错了什么。我尝试使用“document.getElementById('sample').innerhtml”将 CSS 代码更改为更改背景的更新 CSS。它似乎不起作用。我检查了 JavaScript 控制台,它说的是“未捕获的 TypeError:无法在 index.html:15 处设置属性 'innerHTML' of null”。目前使用一个按钮来测试跳跃。
这是整个文件:(我知道它很乱)
<html>
<head>
<title>Background Slide Test</title>
<script type="text/javascript">
var jump_count = 0;
//Mechanics for the character to jump (Jump counter and character movement)
function incline(){
jump_count++;
document.getElementById('counter').innerHTML = "<h1 class='counter'>Jump Count: " + jump_count + "</h1>";
document.getElementById('jump_animation').innerHTML = ".jump{animation:jump 0.4s linear;}@keyframes jump{0%{transform:translate3d(0, 0, 0);}50%{transform:translate3d(0, -300px, 0);}100%{transform:translate3d(0, 0, 0);}}";
}
//If, else if, else: Changes the scene according to amount of jumps
if (jump_count < 20){
//This willchange the background to be 'field.png'
document.getElementById('background').innerHTML = "html{background-color:black;animation:day_cycle 180s linear infinite;}.background{background:url('test.png') repeat-x;height:100%;width:3050px;animation:slide 3s linear infinite;}@keyframes slide{0%{transform:translate3d(0, 0, 0);}100%{transform:translate3d(-1600px, 0, 0);}}@keyframes day_cycle{0% {background-color:#0063fd;}15% {background-color:orange;}30% {background-color:red;}45% {background-color:#4B0082;}60% {background-color:red;}75% {background-color:orange;}100% {background-color:#0062fd;}}";
} else if (jump_count > 20 && jump_count < 40){
} else {
//Nothing happens
}
//This is where when the space button is pressed, the sprite will jump
//This does nothing yet
function space(){
if (e.key === 37){
jump_count++;
} else {
//Nothing will happen
}
}
</script>
万千封印
开心每一天1111
相关分类