为什么我无法在Javascript Aframe.registerComponent中调用此方法?
问题描述:
我想暂停a-entity
的渲染与利用.pause()
方法的自定义功能。这是我的Aframe组件:为什么我无法在Javascript Aframe.registerComponent中调用此方法?
<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类创建一个可接受的函数?
答
你的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'
});
}
注意这将是你的初始化函数,我声明函数setupFadeAnimation的方式后面的逗号。
答
要回答这个问题 - unexpected token: function
是因为你的pauseTextRender
声明是在这种情况下不正确。这将是在不同的上下文绝对正确的,但,这一个,你需要做的:
pauseTextToRender: function(component){
component.pause();
}
所以,你的整个注册会是什么样子:
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();
}
});
(注初始化声明后的逗号还有)
这是因为对象中你有双像这样:
{
name: "value",
anotherName: "another value"
}
...并发生了什么事情对你是,你是给价值function pauseTextRender(component){ ...etc... }
没有给予名称,而不是用逗号分隔的声明。
希望有帮助!