使用'as'转换对象将返回null:myObject = newObject as MyObject; // 空值

时间:2010-05-03 06:46:27

标签: actionscript-3 red5 iexternalizable

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

此屏幕截图说明我已成功从red5接收到我的对象'o',我正准备将其转换为LobbyData的(假设)相同的数据类型testObject:

alt text Enlarge

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

alt text Enlarge

下面您将在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;
}

3 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

在调用响应者之前,需要声明对象。

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);
}

答案 2 :(得分:0)

实际上,如果你想解决类型转换的问题,你可以简单地将它添加到你的构造函数中:

public function dataAwareObject(o:* = null) {     // TODO:实现功能     超();     如果(O){         for(var a:* in o)             这[a] = o [a];         }     } }

像魅力一样。