AS3]利用ByteArray压缩游戏关卡数据
ByteArray 类提供用于优化读取、写入以及处理二进制数据的方法和属性。其中可以利用compress方法来压缩程序中过长的字符串,非常有效,特别是用在可以设计关卡生成代码的小游戏中,这里要用到subhero写的一个Base64类,方法如下:
压缩:
public static function Compress(value:String):String
{
var textBytes:ByteArray = new ByteArray();
textBytes.writeUTFBytes(value);
textBytes.compress();
return Base64.Encode(textBytes);
}
解压缩:
public static function UnCompress(value:String):String
{
var textBytes:ByteArray = Base64.Decode(value);
try
textBytes.uncompress();
catch(e:IOError)
trace("The code to uncompress is not valid.");
return textBytes.toString();
}
ByteArray 类提供用于优化读取、写入以及处理二进制数据的方法和属性。其中可以利用compress方法来压缩程序中过长的字符串,非常有效,特别是用在可以设计关卡生成代码的小游戏中,这里要用到subhero写的一个Base64类,方法如下:
压缩:
public static function Compress(value:String):String
{
var textBytes:ByteArray = new ByteArray();
textBytes.writeUTFBytes(value);
textBytes.compress();
return Base64.Encode(textBytes);
}
解压缩:
public static function UnCompress(value:String):String
{
var textBytes:ByteArray = Base64.Decode(value);
try
textBytes.uncompress();
catch(e:IOError)
trace("The code to uncompress is not valid.");
return textBytes.toString();
}