此问题涉及andengine GL-ES1。循环使用detachChild()和addChild()时表现不如预期
我在制作壁纸活动时遇到麻烦,当您返回时或者用户在游戏中执行操作时会刷新。
我的游戏有两个活动。其中之一是,您可以编辑和排列房间中的背景元素。在另一方面你玩游戏,它使用与你在第一个活动中安排的元素相同的背景。
还有一个动态壁纸,其中你的房间是背景和角色在它前面移动。
我在壁纸中的onResume()中创建updtaes。
首先,我分离出之前使用过的所有背景精灵。 然后我在新的位置添加新的精灵。
会发生什么情况:有些精灵不显示。
以下是方法:你能看到我做错了什么吗?
private void loadBackgroundDecorations() {
//Add new decorations
Log.d(TAG, "loadBackgoundDecorations");
mEngine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
// Remove Old Decorations
Log.d(TAG, "loadBackgoundDecorations: decorationList.size() =" + decorationList.size());
while(decorationList.size() > 0){
Sprite d = decorationList.remove(0);
scene.detachChild(d);
Log.d(TAG, "loadBackgoundDecorations: detachChild");
}
decorationList.clear();
//Add new decorations
ArrayList<Integer> decorations = app.getBackgroundManager().getDecorations();
Log.d(TAG, "loadBackgoundDecorations: decorations.size()" +decorations.size());
for (int i = 0; i < decorations.size(); i+=3) {
Log.d(TAG, "Decoration Values: texture-"+decorations.get(i)+", x-"+decorations.get(1+i)+", y-"+decorations.get(2+i));
Sprite draggable = new Sprite(decorations.get(1+i),decorations.get(2+i),mGameTextureRegionLibrary.get(decorations.get(i)));
draggable.setIgnoreUpdate(true);
scene.attachChild(draggable,0);
decorationList.add(draggable);
Log.d(TAG, "loadBackgoundDecorations: attachChild"+ i);
}
}
});
}
@Override protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
BitmapTextureAtlasTextureRegionFactory.createFromAsset(mTexture, this, app.getAquariumBackground(), 0, 0);
addBubbles();
loadBackgroundDecorations();
addCharacters()
}
============ UPDATE ================= 正如一些人以下建议,我试图将所有的场景设置功能放入可运行。这没有效果。我的工作是将“错误的”装饰可见属性设置为“false”。但我担心这会最终导致内存泄漏,因为越来越多的精灵副本被隐藏在壁纸上。 这个问题只在我称之为“detachChild”时才存在。出于某种原因,似乎阻止“attachChild”正确触发。任何人都有什么可能导致这种想法?
其他人可以创建一个活动,添加和删除onResume函数中的精灵?
+1。强烈的怀疑你是正确的。 – 2013-02-25 22:33:49
我想那不是。该函数不需要在其他两种方法之间运行(这就是为什么他们以这种方式编写的原因)。 runOnUI线程用于使detachChild()线程安全。但泡沫和角色都不足以遮掩装饰。问题是当循环执行时,一个项目不会被添加。但是,如果我省略detachChild()调用,则会添加所有这些 - 不幸的是,它也会保留所有旧的。任何其他想法? – 2013-02-25 22:47:38
尝试了上面的建议,将所有的功能都移到了可运行的程序中。不用找了。其他的东西。仍在搜索... – 2013-02-26 05:26:07