简体   繁体   English

在AndEngine中,在何处调用实体更新方法?

[英]In AndEngine, where to call entity update methods?

I'm using AndEngine and I adopted a Component-Entity Model to developing my game. 我正在使用AndEngine,并且采用了组件实体模型来开发游戏。 My question is in an AndEngine setup, where and how should I invoke the update methods of my entities (and their components)? 我的问题是在AndEngine设置中,我应该在哪里以及如何调用实体(及其组件)的更新方法?

I've only started using AndEngine two days ago, so forgive me for any noobity. 两天前我才开始使用AndEngine,所以请原谅我。

In AndEngine , for your Entity to have an update method that gets invoked at every frame, its class must implement the IUpdateHandler interface. AndEngine中 ,为使您的实体具有一个在每一帧都被调用的update方法,其类必须实现IUpdateHandler接口。

This will force you to override the public void onUpdate(float pSecondsElapsed) and public void reset() methods. 这将迫使您覆盖public void onUpdate(float pSecondsElapsed)public void reset()方法。

You then place your "update code" in the body of onUpdate(float pSecondsElapsed) . 然后,将“更新代码”放在onUpdate(float pSecondsElapsed)的正文中。

The instantiated object implementing the IUpdateHandler interface then must be registered in your Scene object via the registerUpdateHandler(IUpdateHandler updateHandler) . 然后,必须通过registerUpdateHandler(IUpdateHandler updateHandler)将实现IUpdateHandler接口的实例化对象注册到Scene对象中。

Here's an example code: 这是一个示例代码:

Entity class with the update method. 具有update方法的实体类。

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
    }     
}

And then in the initialization block of your SimpleBaseGameActivity where you have the main scene declared as 然后在您的SimpleBaseGameActivity的初始化块中,将主场景声明为

Scene mainScene;

You register the Entity like this: 您可以这样注册实体:

Entity entity = new Entity();

this.mainScene.registerUpdateHandler(entity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM