IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> A-Frame遇到的问题及解决方法 -> 正文阅读

[游戏开发]A-Frame遇到的问题及解决方法

A-Frame官网

官网首页有许多案例,上面有很多各种效果展示。

文档

学习的话,就看最新的英文文档,网上找到了中文文档,跟着写了案例,结果有些效果根本无法实现,例如动画,最后发现中文文档还是很早以前的版本。

<html>

<head>
    <script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
</head>

<body>
    <a-scene device-orientation-permission-ui="enabled: false" cursor="rayOrigin: mouse; fuse: false" raycaster="objects: .raycastable">
        <a-assets>
            <audio id="click-sound" src="https://cdn.aframe.io/360-image-gallery-boilerplate/audio/click.ogg"></audio>
            <!-- Images. -->
            <img id="city" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/city.jpg">
            <img id="city-thumb" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/thumb-city.jpg">
            <img id="cubes" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/cubes.jpg">
            <img id="cubes-thumb" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/thumb-cubes.jpg">
            <img id="sechelt" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/sechelt.jpg">
            <img id="sechelt-thumb" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/thumb-sechelt.jpg">
        </a-assets>

        <!-- 360-degree image. -->
        <a-sky id="image-360" radius="10" src="#city"></a-sky>

        <!-- Link template we will build. -->
        <a-entity id="box1" log="Hello, Box!" class="link" position="1 1 -6" geometry="primitive: plane; height: 1; width: 1" material="shader: flat; src: #cubes-thumb"></a-entity>
        <a-box id="box" position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
        <!-- <a-entity position="-1 0.5 -3" id="box" cursor-listener geometry="primitive: box" material="color: blue"></a-entity> -->
        <!-- clickel="target:#box1" -->
        <!-- Camera + Cursor. -->
        <a-entity camera look-controls>
            <a-entity cursor="fuse: true; fuseTimeout: 500" position="0 0 -1" geometry="primitive: ring; radiusInner: 0.02; radiusOuter: 0.03" material="color: black; shader: flat">
            </a-entity>
        </a-entity>
        <a-entity class="clickable" cursor id="box" cursor-listener geometry="primitive: box" material="color: blue"></a-entity>
    </a-scene>
    <script>
        let sceneEl = document.querySelector('a-scene')
        let sky = document.querySelector('a-sky')
        let box = document.querySelector('#box')
        console.log('imgEl', box);
        // imgEl.emit('click', {}, false)
        AFRAME.registerComponent('cursor-listener', {
            init: function() {
                var lastIndex = -1;
                var COLORS = ['red', 'green', 'blue'];
                this.el.addEventListener('click', function(evt) {
                    lastIndex = (lastIndex + 1) % COLORS.length;
                    this.setAttribute('material', 'color', COLORS[lastIndex]);
                    console.log('I was clicked at: ', evt.detail.intersection.point);
                });
            }
        });
        console.log('this', this);
        box.addEventListener('click', (e) => {
                console.log("点击了天蓝色方块", e);
            })
        AFRAME.registerComponent('log', {
            schema: {
                type: 'string'
            },
            init: function() {
                var stringToLog = this.data;
                console.log(stringToLog);
            }
        });
    </script>
</body>

</html>

写了个小demo结果遇到了很多问题,总结如下:

  1. 如何实现点击场景中的某个元素
  2. 如何正确显示png图片
  3. 如何让初始化时的提示弹窗不显示
  4. 如何使其他元素和sky同步旋转

问题解决方法

  1. 给a-scene标签添加cursor=“rayOrigin: mouse; fuse: false”;设置这个之后鼠标不管悬浮在页面上哪个地方都有有小手的样式,如果不想监听所有的元素的点击事件,我们需要再设置有哪些具体的元素是可以被监听的,需要再加一个属性raycaster=“objects: .clickable” ,objects的值代表的是只有类名为clickable的元素可以被点击。其他设置参考官方文档。这里还需要注意一点,这个clickable需要设置在最内层的元素上才有效

    <--无效的写法--> 
    <a-entity  position="-1 0.5 -3" id="box" material="color: skyblue" class="port-point"  class="clickable">
      <a-image position="-1 0.5 -3" width="2" height="2"  					id="dot" src="#point" alpha-test="0.7"></a-image>
    </a-entity>
     <--正确的写法-->
     <a-entity  position="-1 0.5 -3" id="box" material="color: skyblue" class="port-point">
      <a-image position="-1 0.5 -3" width="2" height="2"  					id="dot" src="#point" alpha-test="0.7"  class="clickable"></a-image>
    </a-entity>
       
    
  2. 这个解决方法是在网上查找得知的需要给图片加上这个属性alpha-test=“0.7”

    <a-image position="-1 0.5 -3" width="2" height="2"  id="dot" src="#point" alpha-test="0.7" class="clickable"></a-image>
    
  3. 在a-scene标签上加上device-orientation-permission-ui=“enabled: false”

  4. 一开始的思路是直接更改a-sky的rotation属性,运行起来sky元素确实如期望那样的旋转了,但是其他元素却固定不动。知道后来突然看到camera,这个是更改视角的,通过设置camera,然后更改旋转角度,所有的元素,全部同步旋转。

<a-entity  camera position="0 0 1" id="box" material="color: skyblue" ></a-entity>
box.setAttribute("animation",`property: rotation;to:${this.x} ${this.y} ${this.z};dur:1000`)
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-04-04 12:42:53  更:2022-04-04 12:46:09 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/24 20:12:51-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码