【问题标题】:AS3 - Problems with IExternalizableAS3 - IExternalizable 的问题
【发布时间】:2015-10-13 20:34:38
【问题描述】:

所以,我最近一直在使用 ByteArrays,但遇到了一些烦人的问题,让我想把头发扯掉。

基本上,我正在尝试为我正在制作的应用程序保存项目数据,以便将游戏角色编译到一个文件中。该项目包含自定义对象、向量、向量的向量,甚至是自定义对象的向量的向量!我认为正确写入所有这些数据的最佳方法是将 IExternalizable 接口与 readExternal 和 writeExternal 命令一起使用。所以这就是我正在做的事情。

我将所有项目数据写入一个对象,然后将该对象写入一个 ByteArray,并将其保存到一个文件中:

// probject means project object !
mProbject= { };

// Register vector as class, just to prevent any errors about that just in case
registerClassAlias("vec_core.timeline.KeyFrame", Vector.<KeyFrame> as Class);

// single KeyFrame Object
mProbject.currentFrame = Main.getInstance().animationList.selectedItem.currentKeyFrame; 

// a Vector.<Vector.<KeyFrame>>
mProbject.frames = Main.getInstance().animationList.keyFrameVectorVector; 

// an unsigned int
mProbject.selectedItemIndex = Main.getInstance().animationList.entries.indexOf(Main.getInstance().animationList.selectedItem);  

// Vector.<String>
mProbject.animationNames = Main.getInstance().animationList.animationNames; 

// String
mProbject.projectPath = nativePath;

//String
mProbject.projectName = name; 

mByteArray = new ByteArray();
mByteArray.writeObject(mProbject);
mByteArray.compress();

return mByteArray;

在 KeyFrame 类中,还有两个自定义对象的向量:

private var mHitboxes:Vector.<Hitbox>;
private var mHitboxSprites:Vector.<HitboxSprite>;

所以我设置了这两个类和我的 KeyFrame 类以使用 IExternalizable:

public class HitboxSprite extends Sprite implements IExternalizable
{
    public function readExternal(input:IDataInput):void
    {
        trueBounds.x = input.readFloat();
        trueBounds.y = input.readFloat();
        trueBounds.width = input.readFloat();
        trueBounds.height = input.readFloat();
        mHitbox = input.readObject();
    }

    public function writeExternal(output:IDataOutput):void
    {
        output.writeFloat(trueBounds.x);
        output.writeFloat(trueBounds.y);
        output.writeFloat(trueBounds.width);
        output.writeFloat(trueBounds.height);
        output.writeObject(mHitbox);            
    }
}


public class Hitbox implements IExternalizable
{
    public function readExternal(input:IDataInput):void
    {
        mName = input.readUTF();
        mType = input.readUnsignedInt();
        mEnabled = input.readBoolean();
        mKnockback = input.readBoolean();
        x = input.readFloat();
        y = input.readFloat();
        width = input.readFloat();
        height = input.readFloat();
       addMultipleTags(input.readUTF());
    }

    public function writeExternal(output:IDataOutput):void
    {
        output.writeUTF(mName);
        output.writeUnsignedInt(mType);
        output.writeBoolean(mEnabled);
        output.writeBoolean(mKnockback);
        output.writeFloat(mX);
        output.writeFloat(mY);
        output.writeFloat(mWidth);
        output.writeFloat(mHeight);
        output.writeUTF(getAllTags());
    }
}

public class KeyFrame implements IExternalizable
{
    public function readExternal(input:IDataInput):void
    {
        mID = input.readUnsignedInt();
        mLabel = input.readUTF();
    }

    public function writeExternal(output:IDataOutput):void
    {
        output.writeUnsignedInt(mID);
        output.writeUTF(mLabel);
    }
}

但是当它到达“根”字节数组的 writeObject() 方法时,我得到了错误:

[Fault] exception, information=ArgumentError: Error #2004: One of the parameters is invalid.

这可能是我遇到过的最烦人的问题。好像我已经尝试了一切,但没有任何效果。 其他人有这方面的经验吗?难道我做错了什么?? 我会很感激我能得到的任何帮助。我只想继续制作我的游戏:

【问题讨论】:

  • 也许您正在寻找this
  • @MarcoAurélioDeleu 我的朋友,你救了我的命。我几乎失去了理智,所以,我谢谢你。当我搜索这个问题时,这个问题没有出现,所以我很感谢你指点我那个答案! adobe 文档只是告诉您注册该类,但并不是说它仅在您注册对象时才适用于自定义对象!如果你愿意,你可以让你的评论成为答案,我很乐意接受:>

标签: actionscript-3 iexternalizable


【解决方案1】:

就像评论部分提到的那样,您正在寻找this。对我敲钟的是你扩展的“Sprite”课程。这让我怀疑你试图将可视化内容外化。

【讨论】:

  • 哦,是的!我什至没有考虑过!我完全忘记了外化视觉对象(例如精灵)与普通对象不同。不过,我不知道以这种方式将精灵外部化是可能的。我猜你每天都会学到一些新东西,哈哈。再次感谢!
猜你喜欢
  • 2011-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
相关资源
最近更新 更多