我试图利用three.js的EventDispatcher将各种事件添加到我的对象中。我的代码的问题是我要返回回调接口,必须做一些奇怪的事情才能到达我的实际对象。我所需要的只是作为事件函数目标的Object3D。但是此函数将返回回调接口。
Typescript建议我为此使用索引签名,但我宁愿获取实际对象而不是回调。我有什么想法要实现这种效果吗?
export interface Callback {
[x: string]: any; // I want to avoid this
type: string;
event(callback: Callback): void;
}
this.scene.addObject('planetCore',
new THREE.Mesh(
new THREE.IcosahedronBufferGeometry(0.3, 1),
this.materialLibrary.getMaterial('planetCore')),
{
type: 'update', event: (callback: Callback) => { // this object should be the Object3D instead...
const target = callback.target as THREE.Object3D; // Also want to avoid this
target.rotateX(-0.002);
target.rotateY(-0.003);
}
});
addObject(name: string, object: THREE.Object3D, callback?: Callback) {
this.sceneData.set(name, object);
super.add(object);
if (callback) {
THREE.EventDispatcher.call(object);
callback.target = object;
object.addEventListener(callback.type, callback.event);
}
}