Aframe每隔一段时间改变天空

问题描述 投票:0回答:1

我尝试我的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

javascript timer intervals aframe sky
1个回答
2
投票

每次调用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
}
© www.soinside.com 2019 - 2024. All rights reserved.