BaseGameActivity.runOnUpdateThread()与Entity.registerUpdateHandler() [英] BaseGameActivity.runOnUpdateThread() vs. Entity.registerUpdateHandler()
问题描述
有什么区别,如果我执行的Runnable
与 runOnUpdateThread()
或我注册一个更新处理成实体与执行code?
Is there any difference if I execute a Runnable
with runOnUpdateThread()
or I register an update handler into an Entity and execute the code with that?
我想从场景中删除雪碧
与 Sprite.detachSelf()
。在这种情况下,教程说,这种方法必须在更新线程与 BaseGameActivity.runOnUpdateThread()
调用。但是,这种解决方案我有活动对象传递给希望使用 runOnUpdateThread每一个对象()
。嗯......我不喜欢它。
I would like to remove a Sprite
from the Scene with Sprite.detachSelf()
. In this case the tutorial says that this method must be invoked in the Update Thread with BaseGameActivity.runOnUpdateThread()
. But with this solution I have to pass the activity object to every object that want to use the runOnUpdateThread()
. Well... I do not like it.
我的问题是,如果我在实体创建一个 RunnableHandler
对象,并使用 registerUpdateHandler注册它()
和新Runnable接口被添加到 RunnableHandler
,是该解决方案的 runOnUpdateThread()
的功能相同。在更新线程是这个的Runnable
执行?
My question is if I create a RunnableHandler
object in an Entity and register it with registerUpdateHandler()
and new Runnable is added to RunnableHandler
, is this solution identical with the runOnUpdateThread()
functionality. Is this Runnable
executed in Update Thread?
/* MySprite is attached to a Scene object */
public class MySprite extends Sprite {
private final RunnableHandler UPDATE_HANDLER = new RunnableHandler();
public MySprite() {
registerUpdateHandler(UPDATE_HANDLER);
}
/* called when the sprite has to be removed from scene */
public void removeMyself() {
Runnable r = new Runnable() {
public void run() {
detachSelf();
}
};
UPDATE_HANDLER.postRunnable(r);
}
}
我问这是因为与标准溶液一切工作正常。但随着更新处理的解决方案,我得到这个异常:
I am asking this because with the standard solution everything is working fine. But with the update handler solution I got this exception:
致命异常:UpdateThread
java.lang.IndexOutOfBoundsException:无效指数19,大小为19
在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
在java.util.ArrayList.get(ArrayList.java:308)
在org.andengine.entity.Entity.onManagedUpdate(Entity.java:1402)
在org.andengine.entity.scene.Scene.onManagedUpdate(Scene.java:284)
在org.andengine.entity.Entity.onUpdate(Entity.java:1167)
在org.andengine.engine.Engine.onUpdateScene(Engine.java:591)
在org.andengine.engine.Engine.onUpdate(Engine.java:586)
在org.andengine.engine.Engine.onTickUpdate(Engine.java:548)
在org.andengine.engine.Engine $ UpdateThread.run(Engine.java:820)
FATAL EXCEPTION: UpdateThread java.lang.IndexOutOfBoundsException: Invalid index 19, size is 19 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at org.andengine.entity.Entity.onManagedUpdate(Entity.java:1402) at org.andengine.entity.scene.Scene.onManagedUpdate(Scene.java:284) at org.andengine.entity.Entity.onUpdate(Entity.java:1167) at org.andengine.engine.Engine.onUpdateScene(Engine.java:591) at org.andengine.engine.Engine.onUpdate(Engine.java:586) at org.andengine.engine.Engine.onTickUpdate(Engine.java:548) at org.andengine.engine.Engine$UpdateThread.run(Engine.java:820)
这通常是安装/拆卸函数调用的不可以在更新线程时。我对吗?
预先感谢您的帮助。
It usually comes when attach/detach functions called not in Update Thread. Am I right? Thank you in advance for your help.
推荐答案
在查看源$ C $ C <一个href=\"https://github.com/nicolasgramlich/AndEngine/blob/GLES2/src/org/andengine/entity/Entity.java#L1400\"相对=nofollow>实体的理论发生的事情是:
Looking at the source code in Entity a theory what happens is:
- 显示线,拿起一个实体数= 19(从日志)
- 表示entity.get(I)中的一个.onUpdate()调用removeMyself()
- 的可运行为detachSelf排队。
- 在下一entity.get(ⅰ).onUpdate()将导致UpdateHandler经由<一个运行href=\"https://github.com/nicolasgramlich/AndEngine/blob/GLES2/src/org/andengine/entity/Entity.java#L1165\"相对=nofollow>的onUpdate (updatehandlers在onManagedUpdate运行)
- 的<一个href=\"https://github.com/nicolasgramlich/AndEngine/blob/GLES2/src/org/andengine/entity/Entity.java#L705\"相对=nofollow> detachSelf 删除从父列表中的孩子(主叫)(其中,入境次数还是19,但mChildren的长度/实体现在是18)
- The line shown, picks up a entity count = 19 (from log)
- one of the the entity.get(i).onUpdate() calls removeMyself()
- The runnable for detachSelf is queued.
- On the next entity.get(i).onUpdate() will result in the UpdateHandler to be run via onUpdate (updatehandlers are run in onManagedUpdate)
- the detachSelf removes the child from the parent list (the caller) (where entry count still 19 but the length of mChildren/entities is now 18)
从看code只是一个理论......我觉得好点入手,得到的答复是看在<一环href=\"https://github.com/nicolasgramlich/AndEngine/blob/GLES2/src/org/andengine/entity/Entity.java#L1400\"相对=nofollow>实体,看看'实体'在循环过程中修改。 (注:最终实体的只是prevents重新分配变量,而不是修改列表)
Just a theory from looking at code... I think a good point to start to get the answer is to look at the loop in Entity and see if 'entities' is modified during the loop. (Note: the 'final entities' just prevents reassigning variable, not modifying the list)
这篇关于BaseGameActivity.runOnUpdateThread()与Entity.registerUpdateHandler()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!