index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Component({
  2. behaviors: [require('../../common/share-behavior').default],
  3. properties: {
  4. a: Number,
  5. messageData: {
  6. type: Object,
  7. value: {
  8. moveX: 0,
  9. moveZ: 0,
  10. },
  11. observer(newVal, oldVal) {
  12. this.speedX = Math.sign(this.speedX) !== 0 ? Math.sign(this.speedX) * Math.abs(newVal.moveX) * 0.01 : newVal.moveX * 0.01;
  13. this.speedZ = Math.sign(this.speedZ) !== 0 ? Math.sign(this.speedZ) * Math.abs(newVal.moveZ) * 0.01 : newVal.moveZ * 0.01;
  14. }
  15. }
  16. },
  17. data: {
  18. loaded: false,
  19. },
  20. lifetimes: {},
  21. methods: {
  22. handleReady({detail}) {
  23. const xrScene = this.scene = detail.value;
  24. console.log('xr-scene', xrScene);
  25. const xrSystem = wx.getXrFrameSystem();
  26. // 绑定运动目标
  27. this.moveSphereTRS = this.scene.getElementById('move-sphere').getComponent(xrSystem.Transform);
  28. // 绑定内置变量
  29. this.speedX = 0;
  30. this.speedZ = 0;
  31. this.planeWidth = 5;
  32. // 绑定tick事件
  33. xrScene.event.add('tick', this.handleTick.bind(this));
  34. },
  35. handleAssetsProgress: function({detail}) {
  36. console.log('assets progress', detail.value);
  37. },
  38. handleAssetsLoaded: function({detail}) {
  39. console.log('assets loaded', detail.value);
  40. this.setData({loaded: true});
  41. },
  42. handleTick: function () {
  43. if (this.moveSphereTRS) {
  44. const nowPos = this.moveSphereTRS.position;
  45. if(nowPos.x > this.planeWidth) {
  46. this.speedX = -this.speedX;
  47. } else if (nowPos.x < -this.planeWidth){
  48. this.speedX = -this.speedX;
  49. }
  50. if(nowPos.z > this.planeWidth) {
  51. this.speedZ = -this.speedZ;
  52. } else if (nowPos.z < -this.planeWidth){
  53. this.speedZ = -this.speedZ;
  54. }
  55. nowPos.x += this.speedX;
  56. nowPos.z += this.speedZ;
  57. this.triggerEvent('infoListener', {
  58. speedX: this.speedX,
  59. speedZ: this.speedZ,
  60. posX: nowPos.x.toFixed(2),
  61. posZ: nowPos.z.toFixed(2),
  62. });
  63. }
  64. }
  65. }
  66. })