【问题标题】:I want to define a variable for the attribute of aframe animation我想为aframe动画的属性定义一个变量
【发布时间】:2021-07-05 00:35:09
【问题描述】:

我写了下面的代码

最后,当将touchPoint设置为to时,它不起作用。

// getModel
const model = document.getElementById('model') 
// getPlaceGround
const ground = document.getElementById('ground')
// click ground
ground.addEventListener('click', (event) => {
  console.log('clicked ground')
  // get touch position
  const touchPoint = event.detail.intersection.point
  console.log(touchPoint) // touchPoint
  console.log(touchPoint.x) // touchPoint.x
  console.log(touchPoint.y) // touchPoint.y
  console.log(touchPoint.z) //// touchPoint.z
  
 

试过了
1:不好
2:不好
3:不好
4:不好
5:很好但是,我想设置变量
6:很好但是,我想添加动画

 model.setAttribute('animation', 'property: position; to: touchPoint')
 model.setAttribute('animation', 'property: position; to: touchPoint.x touchPoint.y touchPoint.z ')
 model.setAttribute('animation', `property: position; to: ${touchPoint}`)
 model.setAttribute(`'animation', 'property: position; to: ${touchPoint}'`)
 model.setAttribute('animation', 'property: position; to: -4 -4 2')
 model.setAttribute('position', touchPoint)

【问题讨论】:

    标签: javascript animation aframe


    【解决方案1】:

    这是一个有效的jsFiddle 但首先,让我们谈谈您的尝试:

    1. 只是一个字符串而不是变量
    model.setAttribute('animation', 'property: position; to: touchPoint')
    // to = tochPoint <--- hardcoded string not a variable
    
    model.setAttribute('animation', 'property: position; to: touchPoint.x touchPoint.y touchPoint.z ')
    // to = touchPoint.x touchPoint.y touchPoint.z <--- hardcoded string not a variable
    

    您刚刚将 touchPoint 添加为硬编码字符串,而不是变量

    1. 巨大的物体
    model.setAttribute('animation', `property: position; to: ${touchPoint}`)
    // to = {.....} <--- object but expected a string 
    
    model.setAttribute(`'animation', 'property: position; to: ${touchPoint}'`)
    // to = {.....} <--- object but expected a string 
    

    touchPoint 是一个包含许多其他内容的对象,但您的“to”属性需要一个字符串。

    解决方案:

    model.setAttribute('animation', `property: position; to: ${touchPoint.x} ${touchPoint.y} ${touchPoint.z}`)
    
    // to = -4 -4 -2
    

    希望能帮到你,有什么问题可以私信我

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      相关资源
      最近更新 更多