我正在使用 AndEngine,并采用组件实体模型来开发我的游戏。我的问题是在 AndEngine 设置中,我应该在哪里以及如何调用我的实体(及其组件)的更新方法?
我两天前才开始使用 AndEngine,所以请原谅我的任何新手。
最佳答案
在 AndEngine 中,为了让您的实体拥有在每一帧调用的更新方法,其类必须实现 IUpdateHandler
接口(interface)。
这将强制您重写 public void onUpdate(float pSecondsElapsed)
和 public void reset()
方法。
然后将“更新代码”放入 onUpdate(float pSecondsElapsed)
的主体中。
实现 IUpdateHandler
接口(interface)的实例化对象必须通过 registerUpdateHandler(IUpdateHandler updateHandler)
在您的 Scene
对象中注册。
这是一个示例代码:
具有更新方法的实体类。
public class Entity implements IUpdateHandler
{
// Lorem ipsum dolor sit amet...
@Override
public void onUpdate(float pSecondsElapsed)
{
// Update code here
}
@Override
public void reset() {
// Reset code here
}
}
然后在 SimpleBaseGameActivity
的初始化 block 中,将主场景声明为
Scene mainScene;
您可以像这样注册实体:
Entity entity = new Entity();
this.mainScene.registerUpdateHandler(entity);
关于java - 在AndEngine中,哪里调用实体更新方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14228423/