我们是一对正在做游戏的家伙。游戏实体的数据存储在嵌套数组中。顶层数组包含数组,每个数组对应一个实体。我所说的实体是指存储在数组中的游戏对象的参数和数组中引用的starling对象。
这些子数组引用了一个八哥电影剪辑或一个八哥精灵,以及该实体的大约15个其他变量。因此,由于各种原因,这些变量没有存储在电影剪辑/子画面本身中。Blitting的可能性就是其中之一。
然后,问题是如何以最好的方式组织这一过程,并最终实现starling对象的回收,以减少垃圾收集的问题。我有三个建议,但我知道,其他的东西可能会更好。
1)为每类对象设置一个“墓地”容器:一个用于小行星,一个用于播放快照,一个用于....当一个实体被销毁时,相应的对象将被放入正确的墓地。每当应该创建同一类的实体时,如果容器中包含任何实体,则重用相应容器中的实体。
2)一个大容器,用来存放所有被破坏的电影片段和精灵。每当精灵或电影剪辑产生时,重用该容器中的对象(如果有)。还应设置正确的动画。我不知道这是不是占用了很多cpu,或者根本不可能。
3)让垃圾回收来处理被破坏的电影片段和精灵。不要回收它们。
发布于 2014-02-17 14:07:17
就我个人而言,当我想做这样的事情时,我会覆盖我想要使用的基类,并添加一些代码来完成您想要做的事情:
例如,扩展MovieClip的Asteroid类:
package
{
import starling.display.MovieClip;
import starling.textures.Texture;
public class Asteroid extends MovieClip
{
/** a object pool for the Asteroids **/
protected static var _pool :Vector.<Asteroid>;
/** the number of objects in the pool **/
protected static var _nbItems :int;
/** a static variable containing the textures **/
protected static var _textures :Vector.<Texture>;
/** a static variable containing the textures **/
protected static var _fps :int;
public function Asteroid()
{
super( _textures, 60 );
}
/** a static function to initialize the asteroids textures and fps and to create the pool **/
public static function init(textures:Vector.<Texture>, fps:int):void
{
_textures = textures;
_fps = fps;
_pool = new Vector.<Asteroid>();
_nbItems = 0;
}
/** the function to call to release the object and return it to the pool **/
public function release():void
{
// make sure it don't have listeners
this.removeEventListeners();
// put it in the pool
_pool[_nbItems++] = this;
}
/** a static function to get an asteroid from the pool **/
public static function get():Asteroid
{
var a :Asteroid;
if( _nbItems > 0 )
{
a = _pool.pop();
_nbItems--;
}
else
{
a = new Asteroid();
}
return a;
}
/** a static function to destroy asteroids and the pool **/
public static function dispose():void
{
for( var i:int = 0; i<_nbItems; ++i ) _pool[i].removeFromParent(true);
_pool.length = 0;
_pool = null;
}
}
}因此,您可以使用Asteroid.get();方法创建一个小行星,这将在池中获取一个小行星,如果池为空,则创建一个小行星。
在必须使用静态函数Asteroid.init(textures, fps)初始化类之前
当你不再需要小行星时,你可以调用myAsteroid.release();,这将把小行星返回到池中供下一次使用。
当您不再需要小行星时,您可以调用静态Asteroid.dispose();来清空池。
希望这能对你有所帮助。
https://stackoverflow.com/questions/21813522
复制相似问题