为什么我无法在Javascript Aframe.registerComponent中调用此方法?

时间:2017-08-04 18:31:21

标签: javascript aframe

我正在尝试暂停使用a-entity方法的自定义函数呈现.pause()。这是我的Aframe组件:

<body style="margin : 0px; overflow: hidden;">
<script>
  AFRAME.registerComponent('intro', {

    schema: {
      array: { type: "array", default: ["Hi! I'm Ducky and welcome to", "Black", "Duck"] }
    },

    init: function() {
      const self = this;
      pauseTextRender(self);
    }

    function pauseTextRender(component) {
      component.pause();
    }  
 });
</script>
</body>

这是最低限度的。当我检查控制台时,我收到错误Uncaught SyntaxError: Unexpected token function。我对Javascript不太熟悉但是如何为Aframe类创建一个可接受的函数?

2 个答案:

答案 0 :(得分:0)

您的Javascript语法不正确。这就是为什么你得到语法错误。尝试做这样的事情:

,
/**
 * Setup fade-in + fade-out.
 */
setupFadeAnimation: function() {
  var data = this.data;
  var targetEl = this.data.target;

  // Only set up once.
  if (targetEl.dataset.setImageFadeSetup) {
    return;
  }
  targetEl.dataset.setImageFadeSetup = true;

  // Create animation.
  targetEl.setAttribute('animation__fade', {
    property: 'material.color',
    startEvents: 'set-image-fade',
    dir: 'alternate',
    dur: 500,
    from: '#FFF',
    to: '#000'
  });
}

注意你的init函数之后的逗号以及我声明函数setupFadeAnimation的方式。

答案 1 :(得分:0)

To answer the question - unexpected token: function is because your pauseTextRender declaration is incorrect in this context. It would be absolutely correct in a different context but, for this one, you need to do :

pauseTextToRender: function(component){
    component.pause();
}

So your entire registration would look like :

AFRAME.registerComponent('intro', {

    schema: {
      array: { 
        type: "array", 
        default: ["Hi! I'm Ducky and welcome to", "Black", "Duck"] 
      }
    },

    init: function() {
      const self = this;
      pauseTextRender(self);
    },

    pauseTextRender: function(component) {
      component.pause();
    }  
 });

(Note the comma after the init declaration as well)

This is because inside an object you have pairs like so :

{
    name: "value",
    anotherName: "another value"
}

...and what was happening for you was that you were giving the value function pauseTextRender(component){ ...etc... } without giving the name, and not separating your declarations with a comma.

Hope that helps!