使用“as”来转换对象返回 null:myObject = newObject as MyObject; // 无效的

发布于 2024-08-30 18:05:56 字数 5792 浏览 9 评论 0原文

我正在尝试在 AS3 中创建一个自定义对象,以将信息传入和传出服务器,在本例中为 Red5。在下面的屏幕截图中,您将看到我能够从 as3 发送对象请求,并从 java 服务器成功接收它。但是,当我尝试使用“as”将接收到的对象转换为我定义的 objectType 时,它​​的值为 null。据我了解,当使用“as”时,您正在检查变量是否是指定数据类型的成员。如果变量不是,则返回 null。

此屏幕截图说明我已成功从 red5 接收到我的对象“o”,并且我正要将其转换为 LobbyData 的(据称)相同的数据类型 testObject:

替代文字 放大

但是,当 testObject = o as LobbyData; 运行时,它返回空值。 :(

替代文本 放大

下面您将看到我在 java 服务器和 as3 客户端上的规范。我确信这两个对象在各方面都是相同的,但出于某种原因,Flash 并不这么认为。我已经把头发拔了很长时间了,有人有什么想法吗?

AS3 对象:

import flash.utils.IDataInput;
 import flash.utils.IDataOutput;
 import flash.utils.IExternalizable;
 import flash.net.registerClassAlias; 

 [Bindable]
 [RemoteClass(alias = "myLobbyData.LobbyData")]
 public class LobbyData implements IExternalizable
 { 

 private var sent:int; // java sentinel
 private var u:String; // red5 username
 private var sen:int; // another sentinel?
 private var ui:int;  // fb uid
 private var fn:String; // fb name
 private var pic:String; // fb pic
 private var inb:Boolean; // is in the table?
 private var t:int; // table number
 private var s:int; // seat number

 public function setSent(sent:int):void 
 {
  this.sent = sent;
 }

 public function getSent():int
 {
  return sent;
 }  

 public function setU(u:String):void
 {
  this.u = u;
 }

 public function getU():String
 {
  return u;
 }  

 public function setSen(sen:int):void
 {
  this.sen = sen;
 }

 public function getSen():int
 {
  return sen;
 }  

 public function setUi(ui:int):void
 {
  this.ui = ui;
 }

 public function getUi():int
 {
  return ui;
 }

 public function setFn(fn:String):void
 {
  this.fn = fn;
 }

 public function getFn():String
 {
  return fn;
 }

 public function setPic(pic:String):void
 {
  this.pic = pic;
 }

 public function getPic():String
 {
  return pic;
 }  

 public function setInb(inb:Boolean):void
 {
  this.inb = inb;
 }

 public function getInb():Boolean
 {
  return inb;
 }  

 public function setT(t:int):void
 {
  this.t = t;
 }

 public function getT():int
 {
  return t;
 }

 public function setS(s:int):void
 {
  this.s = s;
 }

 public function getS():int
 {
  return s;
 }

 public function readExternal(input:IDataInput):void
 {
  sent = input.readInt();
  u = input.readUTF();
  sen = input.readInt();
  ui = input.readInt();
  fn = input.readUTF();
  pic = input.readUTF();
  inb = input.readBoolean();
  t = input.readInt();
  s = input.readInt();  
 }


 public function writeExternal(output:IDataOutput):void
 {
  output.writeInt(sent);
  output.writeUTF(u);
  output.writeInt(sen);
  output.writeInt(ui);
  output.writeUTF(fn);
  output.writeUTF(pic);
  output.writeBoolean(inb);
  output.writeInt(t);
  output.writeInt(s);
 }
    }

Java 对象:

    package myLobbyData;
    import org.red5.io.amf3.IDataInput;
    import org.red5.io.amf3.IDataOutput;
    import org.red5.io.amf3.IExternalizable;




    public class LobbyData implements IExternalizable
    {
 private static final long serialVersionUID = 115280920;

 private int sent; // java sentinel
 private String u; // red5 username
 private int sen; // another sentinel?
 private int ui;  // fb uid
 private String fn; // fb name
 private String pic; // fb pic
 private Boolean inb; // is in the table?
 private int t; // table number
 private int s; // seat number

 public void setSent(int sent)
 {
  this.sent = sent;
 }

 public int getSent()
 {
  return sent;
 }  

 public void setU(String u)
 {
  this.u = u;
 }

 public String getU()
 {
  return u;
 }  

 public void setSen(int sen)
 {
  this.sen = sen;
 }

 public int getSen()
 {
  return sen;
 }  

 public void setUi(int ui)
 {
  this.ui = ui;
 }

 public int getUi()
 {
  return ui;
 }

 public void setFn(String fn)
 {
  this.fn = fn;
 }

 public String getFn()
 {
  return fn;
 }

 public void setPic(String pic)
 {
  this.pic = pic;
 }

 public String getPic()
 {
  return pic;
 }  

 public void setInb(Boolean inb)
 {
  this.inb = inb;
 }

 public Boolean getInb()
 {
  return inb;
 }  

 public void setT(int t)
 {
  this.t = t;
 }

 public int getT()
 {
  return t;
 }

 public void setS(int s)
 {
  this.s = s;
 }

 public int getS()
 {
  return s;
 }



 @Override
 public void readExternal(IDataInput input) 
 {
  sent = input.readInt();
  u = input.readUTF();
  sen = input.readInt();
  ui = input.readInt();
  fn = input.readUTF();
  pic = input.readUTF();
  inb = input.readBoolean();
  t = input.readInt();
  s = input.readInt();  
 }

 @Override
 public void writeExternal(IDataOutput output)
 {
  output.writeInt(sent);
  output.writeUTF(u);
  output.writeInt(sen);
  output.writeInt(ui);
  output.writeUTF(fn);
  output.writeUTF(pic);
  output.writeBoolean(inb);
  output.writeInt(t);
  output.writeInt(s);
 }

    }

AS3 客户端:

public function refreshRoom(event:Event)
{
var resp:Responder=new Responder(handleResp,null);
ncLobby.call("getLobbyData", resp, null);
}
public function handleResp(o:Object):void
{
var testObject:LobbyData=new LobbyData;    
testObject = o as LobbyData;  
trace(testObject);
}

Java 客户端

 public LobbyData getLobbyData(String param)
 {
LobbyData lobbyData1 = new LobbyData();
 lobbyData1.setSent(5);
 lobbyData1.setU("lawlcats");
 lobbyData1.setSen(5);
 lobbyData1.setUi(5);
 lobbyData1.setFn("lulz");
 lobbyData1.setPic("lulzagain");
 lobbyData1.setInb(true);
 lobbyData1.setT(5);
 lobbyData1.setS(5);
 return lobbyData1;
}

I am trying to create a custom object in AS3 to pass information to and from a server, which in this case will be Red5. In the below screenshots you will see that I am able to send a request for an object from as3, and receive it successfully from the java server. However, when I try to cast the received object to my defined objectType using 'as', it takes the value of null. It is my understanding that that when using "as" you're checking to see if your variable is a member of the specified data type. If the variable is not, then null will be returned.

This screenshot illustrates that I am have successfully received my object 'o' from red5 and I am just about to cast it to the (supposedly) identical datatype testObject of LobbyData:

alt text
Enlarge

However, when testObject = o as LobbyData; runs, it returns null. :(

alt text
Enlarge

Below you will see my specifications both on the java server and the as3 client. I am confident that both objects are identical in every way, but for some reason flash does not think so. I have been pulling my hair out for a long time, does anyone have any thoughts?

AS3 Object:

import flash.utils.IDataInput;
 import flash.utils.IDataOutput;
 import flash.utils.IExternalizable;
 import flash.net.registerClassAlias; 

 [Bindable]
 [RemoteClass(alias = "myLobbyData.LobbyData")]
 public class LobbyData implements IExternalizable
 { 

 private var sent:int; // java sentinel
 private var u:String; // red5 username
 private var sen:int; // another sentinel?
 private var ui:int;  // fb uid
 private var fn:String; // fb name
 private var pic:String; // fb pic
 private var inb:Boolean; // is in the table?
 private var t:int; // table number
 private var s:int; // seat number

 public function setSent(sent:int):void 
 {
  this.sent = sent;
 }

 public function getSent():int
 {
  return sent;
 }  

 public function setU(u:String):void
 {
  this.u = u;
 }

 public function getU():String
 {
  return u;
 }  

 public function setSen(sen:int):void
 {
  this.sen = sen;
 }

 public function getSen():int
 {
  return sen;
 }  

 public function setUi(ui:int):void
 {
  this.ui = ui;
 }

 public function getUi():int
 {
  return ui;
 }

 public function setFn(fn:String):void
 {
  this.fn = fn;
 }

 public function getFn():String
 {
  return fn;
 }

 public function setPic(pic:String):void
 {
  this.pic = pic;
 }

 public function getPic():String
 {
  return pic;
 }  

 public function setInb(inb:Boolean):void
 {
  this.inb = inb;
 }

 public function getInb():Boolean
 {
  return inb;
 }  

 public function setT(t:int):void
 {
  this.t = t;
 }

 public function getT():int
 {
  return t;
 }

 public function setS(s:int):void
 {
  this.s = s;
 }

 public function getS():int
 {
  return s;
 }

 public function readExternal(input:IDataInput):void
 {
  sent = input.readInt();
  u = input.readUTF();
  sen = input.readInt();
  ui = input.readInt();
  fn = input.readUTF();
  pic = input.readUTF();
  inb = input.readBoolean();
  t = input.readInt();
  s = input.readInt();  
 }


 public function writeExternal(output:IDataOutput):void
 {
  output.writeInt(sent);
  output.writeUTF(u);
  output.writeInt(sen);
  output.writeInt(ui);
  output.writeUTF(fn);
  output.writeUTF(pic);
  output.writeBoolean(inb);
  output.writeInt(t);
  output.writeInt(s);
 }
    }

Java Object:

    package myLobbyData;
    import org.red5.io.amf3.IDataInput;
    import org.red5.io.amf3.IDataOutput;
    import org.red5.io.amf3.IExternalizable;




    public class LobbyData implements IExternalizable
    {
 private static final long serialVersionUID = 115280920;

 private int sent; // java sentinel
 private String u; // red5 username
 private int sen; // another sentinel?
 private int ui;  // fb uid
 private String fn; // fb name
 private String pic; // fb pic
 private Boolean inb; // is in the table?
 private int t; // table number
 private int s; // seat number

 public void setSent(int sent)
 {
  this.sent = sent;
 }

 public int getSent()
 {
  return sent;
 }  

 public void setU(String u)
 {
  this.u = u;
 }

 public String getU()
 {
  return u;
 }  

 public void setSen(int sen)
 {
  this.sen = sen;
 }

 public int getSen()
 {
  return sen;
 }  

 public void setUi(int ui)
 {
  this.ui = ui;
 }

 public int getUi()
 {
  return ui;
 }

 public void setFn(String fn)
 {
  this.fn = fn;
 }

 public String getFn()
 {
  return fn;
 }

 public void setPic(String pic)
 {
  this.pic = pic;
 }

 public String getPic()
 {
  return pic;
 }  

 public void setInb(Boolean inb)
 {
  this.inb = inb;
 }

 public Boolean getInb()
 {
  return inb;
 }  

 public void setT(int t)
 {
  this.t = t;
 }

 public int getT()
 {
  return t;
 }

 public void setS(int s)
 {
  this.s = s;
 }

 public int getS()
 {
  return s;
 }



 @Override
 public void readExternal(IDataInput input) 
 {
  sent = input.readInt();
  u = input.readUTF();
  sen = input.readInt();
  ui = input.readInt();
  fn = input.readUTF();
  pic = input.readUTF();
  inb = input.readBoolean();
  t = input.readInt();
  s = input.readInt();  
 }

 @Override
 public void writeExternal(IDataOutput output)
 {
  output.writeInt(sent);
  output.writeUTF(u);
  output.writeInt(sen);
  output.writeInt(ui);
  output.writeUTF(fn);
  output.writeUTF(pic);
  output.writeBoolean(inb);
  output.writeInt(t);
  output.writeInt(s);
 }

    }

AS3 Client:

public function refreshRoom(event:Event)
{
var resp:Responder=new Responder(handleResp,null);
ncLobby.call("getLobbyData", resp, null);
}
public function handleResp(o:Object):void
{
var testObject:LobbyData=new LobbyData;    
testObject = o as LobbyData;  
trace(testObject);
}

Java Client

 public LobbyData getLobbyData(String param)
 {
LobbyData lobbyData1 = new LobbyData();
 lobbyData1.setSent(5);
 lobbyData1.setU("lawlcats");
 lobbyData1.setSen(5);
 lobbyData1.setUi(5);
 lobbyData1.setFn("lulz");
 lobbyData1.setPic("lulzagain");
 lobbyData1.setInb(true);
 lobbyData1.setT(5);
 lobbyData1.setS(5);
 return lobbyData1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轻拂→两袖风尘 2024-09-06 18:05:56

正如您已经想到的,您应该使用 registerClassAlias,因为 RemoteClass 仅适用于 Flex 项目(可绑定等)。

请务必在发生任何序列化/反序列化之前调用 registerClassAlias。

此外,调试器还向您显示“o”参数的实际提示,即对象。这表明播放器没有正确地将 AMF 序列化对象的类映射到您的任何类(因此,默认情况下,它与对象一起使用)。您应该在调试器中看到一个 LobbyData 对象;否则,无论你如何施放/强制它,它都不会起作用。

As you already figured out, you should use registerClassAlias as the RemoteClass works out of the box only for Flex projects (as bindable, etc).

Be sure to call registerClassAlias before any serializing / deserializing occurs.

Also, the debugger is showing you the actual tipe of your "o" parameter, which is object. This shows that the player is not correctly mapping the AMF serialized object's class to any of your classes (so, by default, it goes with Object). You should see a LobbyData object in the debugger; otherwise, no matter how you cast / coerce it, it won't work.

断桥再见 2024-09-06 18:05:56

需要在调用响应程序之前声明该对象。

public function refreshRoom(event:Event)
{
var testObject:LobbyData=new LobbyData;
var resp:Responder=new Responder(handleResp,null);
ncLobby.call("getLobbyData", resp, null);
}
public function handleResp(o:Object):void
{

testObject = o as LobbyData;  
trace(testObject);
}

The objet needs to be declared before the responder is called.

public function refreshRoom(event:Event)
{
var testObject:LobbyData=new LobbyData;
var resp:Responder=new Responder(handleResp,null);
ncLobby.call("getLobbyData", resp, null);
}
public function handleResp(o:Object):void
{

testObject = o as LobbyData;  
trace(testObject);
}
无悔心 2024-09-06 18:05:56

实际上,如果您想解决类型转换问题,只需将其添加到构造函数中即可:

public function dataAwareObject(o:* = null)
{
//TODO: 实现功能
极好的();
如果(o){
for(var a:* in o)
this[a] = o[a];
}
}

就像魅力一样

Actually if you want to workaround the type casting you can simply add this to your constructor:

public function dataAwareObject(o:* = null)
{
//TODO: implement function
super();
if(o){
for(var a:* in o)
this[a] = o[a];
}
}
}

Works like a charm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文