Aframe 每隔一段时间改变天空

Aframe changing sky every interval

我尝试让我的 aframe-sky-component 每 3 秒更改一次他的图像,但它不起作用。这是我到目前为止编码的内容:

 <script type = "text/javascript">
   function startTimer(){
  setInterval(function(){ nextimage(); }, 3000);

   function nextimage() {
     var zahl = 0;

     if (zahl == 0) {
       $("#skyid").attr("src","#sky_2");
       zahl ++;
     }
     if (zahl == 1) {
       $("#skyid").attr("src","#sky_3");  
       zahl ++;
     }
     if (zahl == 2) {
       $("#skyid").attr("src","#sky_4");  
       zahl ++;
     }
     if (zahl == 3) {
       $("#skyid").attr("src","#sky_1"); 
       zahl ++;
     }
     if (zahl == 4) {
       zahl == 0;
     }
     }
   }
  </script>   

我想我有一些逻辑错误谢谢你的帮助:D

每次调用 nextImage 时,zahl 都设置为 0。

您可以将其移动到外部范围:

function startTimer(){
  setInterval(function(){ nextimage(); }, 3000);
  var zahl = 0;
  function nextimage() {
  ....

就像我做的那样 here。现在它不会通过调用 nextImage() 归零,所以它就像一个计数器。


此外,我认为更优雅的解决方案是使用颜色数组:

var colors = ["blue", "red", "green", "yellow"]
function nextimage() {
  $("#skyid").attr("color", colors[zahl])
  zahl = (zahl < colors.length - 1) ? ++zahl : 0 
  //zahl is incremented until its reached the end of the array
}