简体   繁体   English

JS对象的异常行为

[英]JS Object odd behavior

I am currently building a attempting to build a Multiplayer game with Phaser and Eureca io. 我目前正在尝试使用Phaser和Eureca io构建多人游戏。 I am at the stage where i am trying to nail the authentication of the players and what they are controlling, i am doing this by on the server having a method that returns the correct player id. 我正处于尝试对玩家及其身份进行身份验证的阶段,我正在服务器上使用具有返回正确玩家ID的方法的方式来进行此操作。

The method on the server is - 服务器上的方法是-

 eurecaServer.exports.getPlayer2Id = function(name , id)
 {

   if(name == "Fly" && id == player2id)
   {
     console.log("correct fly player");
     return player2id;
   }
   else
   {
     console.log("Wrong fly player");
     return id;
   }
}

On the client side at the moment i am only testing, but when i call the function it returns an object. 目前在客户端,我只是在测试,但是当我调用该函数时,它将返回一个对象。 When simply looking at the object in the console it displays this. 当仅在控制台中查看对象时,它将显示此内容。 Object {status: 0, result: null, error: null, sig: "eY0IjunQt7"} so it is telling me my result is null which is odd, but when expanding it i get. Object {status: 0, result: null, error: null, sig: "eY0IjunQt7"}所以它告诉我我的结果为null,这很奇怪,但是在扩展时会得到。

Object {status: 0, result: null, error: null, sig: "eY0IjunQt7"} callback: function() error: null errorCallback: function() onReady: (fn, errorFn) result: "jVcZvzetc8AAK45NAAAH" sig: "eY0IjunQt7"

As you can see when expanding the result is not null, and is exactly what im looking for, i have tried, JSON.stringify but it gives me the first result, where it tells me the result is null. 如您所见,当扩展结果时,结果不为null,而正是我在寻找的东西,我尝试了JSON.stringify,但它给了我第一个结果,告诉我结果为null。

Any ideas how i can get my actual result or why this is happening. 关于如何获得实际结果或为什么这样的任何想法。

Thanks. 谢谢。

Edit: 编辑:

The client side component for server is defined in the main game file 服务器的客户端组件在主游戏文件中定义

var eurecaClientSetup = function() {

var eurecaClient = new Eureca.Client();

eurecaClient.ready(function (proxy) {
    eurecaServer = proxy;

});

Then in the object class it is called here. 然后在对象类中在这里调用它。

this.name = "Fly"
this.id = index;  //Passed in on object creation
this.currPlayer = eurecaServer.getPlayer2Id(this.name, this.id);
console.log(JSON.stringify(this.currPlayer));
console.log(this.currPlayer);

Any server operation is going to be asynchronous, regardless of the framework*. 无论框架*如何,任何服务器操作都将是异步的。 Different frameworks have different ways of getting the result once it is available. 一旦结果可用,不同的框架就具有不同的获取结果的方法。 In Eureca.io, it seems to be using an .onReady() call after the server function name. 在Eureca.io中,似乎在服务器函数名称之后使用了.onReady()调用。

In other words, what you need to do is change your client code to the following: 换句话说,您需要做的是将客户代码更改为以下内容:

this.name = "Fly"
this.id = index;  //Passed in on object creation
var _this = this; // needed because "this" inside function below won't be the same
eurecaServer.getPlayer2Id(this.name, this.id).onReady(function (id) {
    _this.currPlayer = id;
    console.log(_this.currPlayer); // shows the right thing
    // do other stuff here now you have the right player id
});

* Technically, you can do a synchronous/blocking AJAX request, but it is bad practice in 99% of cases. *从技术上讲,您可以执行同步/阻止AJAX请求,但是在99%的情况下,这是不正确的做法。

I'm the author of eureca.io, the answer given by GregL is correct. 我是eureca.io的作者,GregL给出的答案是正确的。 I just want to add that onReady() call will be deprecated, eureca.io support promise like call (using then() function). 我只想补充一下onReady()调用将被弃用,eureca.io支持像call这样的承诺(使用then()函数)。

so you can use this syntax instead, which is more standard. 因此您可以改用这种语法,这是更标准的方法。

this.name = "Fly"
this.id = index;  //Passed in on object creation
var _this = this; // needed because "this" inside function below won't be the same
eurecaServer.getPlayer2Id(this.name, this.id).then(function (id) {
    _this.currPlayer = id;
    console.log(_this.currPlayer); // shows the right thing
    // do other stuff here now you have the right player id
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM