2017-07-06 54 views
0

我有setAttribute的問題。我設置了一個數組作爲旋轉的值,但它不起作用。 gradRoll的提醒是number。但是gradRoll的類型是undefined。爲什麼?有誰能夠幫助我?提前致謝。js setAttribute與Array

AFRAME.registerComponent('clockhand_roll',{ 
    schema:{ 
     type:'string', default: "secondClockhand" 
    }, 

    init:function(){ 
     var data = this.data; 
     var el=this.el; 

     var oTime= new Date(); 
     var curTime; 

     var clockhandPositonArray; 

     if(data == "secondClockhand"){ 
     curTime = oTime.getSeconds(); 
     }else if(data == "minuteClockhand"){ 
     curTime = oTime.getMinutes(); 
     }else if(data == "hourClockhand"){ 
     curTime = oTome.getHours; 
     }else { 
     alert("invalid input!"); 
     } 

     //alert(curTime); 
     //clockhandPositonArray=getClockhandPosition(curTime, data); 
     //alert(el.getAttribute("rotation")); 

     //el.setAttribute("rotation", 'clockhandPositonArray'); 

     setClockhandPosition(el, curTime, data); 

    } 
    }); 

    function setClockhandPosition(el, curTime, typeOfClockhand){ 
    var gradForOneUnit; 
    var gradRoll; 
    var rotationArray; 

    if(typeOfClockhand == "secondClockhand" || typeOfClockhand == "minuteClockhand"){ 
     gradForOneUnit=360/60; 
    }else if(typeOfClockhand == "hourClockhand"){ 
     gradForOneUnit=360/12; 
    } 

    gardRoll=curTime*gradForOneUnit; 

    //alert(gardRoll); //number: 354!!!!!!!!!!!!!!!!!!!! why?????? 
    //alert(typeof(gradRoll)); //undefined!!!!!!!!!!!!! why?????? 

    rotationArray=[curTime*gradForOneUnit, 0, 0]; 

    //alert(typeof(rotationArray));//object 
    //alert(typeof(el.getAttribute("rotation"))); //object 

    //alert(rotationArray); //number: 162 , 0, 0 

    el.setAttribute("rotation", rotationArray); //dosn't work. 
    } 
+1

查找錯別字' gradRoll/gardRoll' –

+0

@Nathan P. thanks –

+0

rotationArray = {x:curTime * gradForOneUnit,y:0,z:0};它的工作原理 –

回答

0

批號:
el.setAttribute("rotation", 'x'+'y' + 'z'); 方法,因爲據我所知,這是多一點效率省略解析('x y z' to {x,y,z}),並在第一時間使用對象:
el.setAttribute("rotation", {x:gradRoll,y:0,z:0});

0

用途:

el.setAttribute("transform", "rotate3d,x,y,z,angle)"); 

或合併rotateXrotateYrotateZ

+1

非常感謝。旋轉應該是一個對象。 –

0

gradRoll是未定義的,因爲變量名稱是gardRoll,這是您在第一次警報中重複但在第二次警報中重複的錯字。

好像你只需要編輯旋轉的X值,試試這個:

el.setAttribute("rotation", gardRoll + ' 0 ' + '0'); 

請注意,我用的變量,在它的錯字。

這有點髒我知道,但它對我有用,當我有位置組件類似的問題。人暗示

+1

非常感謝。 –