本文介绍了网络框架为新用户分配不同的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 networked-aframe 设置的多人 A-Frame 环境中遇到问题.

I have a problem in my multiplayer A-Frame environment that I set up with networked-aframe.

我希望连接的前三个用户具有不同的生成位置,并且任何其他用户都可以在第三个位置生成,但我无法使其正常工作.以下是我目前在 Html 中的内容:

I want the first three users that connect to have a different spawn location and any additional user to spawn at the third location, but I can't get it to work. Here's what I have so far in Html:

 <a-entity id="player"
networked="template:#avatar-template;showLocalTemplate:true;"
camera
positioner=""
wasd-controls
look-controls>
<a-cursor></a-cursor>
</a-entity>

这是它的 JS 组件:

And here is the JS Component for it:

AFRAME.registerComponent('positioner', {
init: function() {

        var el = this.el,
            counter = 0;

        if (counter == 0) {
            el.setAttribute('position', {x:-16,  y:6,  z:-10});
            counter++;
        }
        else if(counter == 1) {
            el.setAttribute('position', {x:-10,  y:6,  z:-10});
            counter++;
        }
        else {
            el.setAttribute('position', {x:-5,  y:6,  z:-10});
            counter++;
        }
}
});

我发现我必须同步其他组件,我正在这样做:

I found out that I have to sync additional components which I am doing like this:

var avatarSchema = {
template: '#avatar-template',
components: [
'positioner',
'position',
'rotation',
'scale',
{
  selector: '.head',
  component: 'material'
},
{
  selector: '.hairs',
  component: 'show-child'
}


]
};
       NAF.schemas.add(avatarSchema);

这是一个很大的项目,所以有更多的代码.如果您需要其他任何东西,例如场景设置或头像设置,请告诉我,我会添加.

It's a big project so there's a lot more code. If you need anything else like the scene setup or the avatar setup let me know and I will add it.

推荐答案

我有一个解决方法,因为我不完全了解组件的工作原理.
我发现参考NAF.entities.entities
下面有一个玩家列表我制作了一个附加到场景的组件:

I have a workaround, for I don't fully understand how the component works.
I found there is a player list under the reference NAF.entities.entities
I made a component attached to the scene:

AFRAME.registerComponent('foo',{
  init:function(){
    setTimeout(function(){
      console.log(Object.keys(NAF.entities.entities));
      console.log(Object.keys(NAF.entities.entities)[0]);
      console.log(Object.keys(NAF.entities.entities).length);
    },5000);
  }
});

第一个日志给了我一个玩家 ID 的数组.
第二个给出第一个元素的 id.
最后一个给了我玩家的数量.

The first log gives me an array of the players id's.
The second one gives the id of the first element.
The last gives me the number of the players.

您可以检查加载的玩家数量,并根据加载的玩家设置他的位置属性.

You can check for the number of players on load, and depending which player loads, You set his position attribute.

if(NAF.. == 1) this.el.setAttribute('position',firstPos);
if(NAF.. == 2) this.el.setAttribute('position',secondPos);
if(NAF.. > 2) this.el.setAttribute('position',third);


这里的工作故障:glitch.com/edit/#!/sudden-antler


当然,与其等待 5 秒,我应该听一个加载的事件,等我不那么忙了,我会调查一下.
如果有人设法使用此组件正确执行此操作,请发表评论,因为我尝试在 onConnect 上做位置(为时过早,其他玩家实体仍未加载),尝试等到场景加载,但我得到的最接近的是在固定时间后,当我获得玩家数量时动态创建播放器.


Working glitch here: glitch.com/edit/#!/sudden-antler


Of course instead of waiting 5 seconds i should be listening to a loaded event, I'll look into it once im not so busy.
If anyone manages to do this properly with this component, please comment, because i tried to do the position onConnect ( too early, the other players entities are still not loaded ), tried waiting till the scene loads, but the closest i got is by dynamically creating the player when i get the number of players, after a fixed time.

这篇关于网络框架为新用户分配不同的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 06:49