简体   繁体   中英

In AndEngine, where to call entity update methods?

I'm using AndEngine and I adopted a Component-Entity Model to developing my game. My question is in an AndEngine setup, where and how should I invoke the update methods of my entities (and their components)?

I've only started using AndEngine two days ago, so forgive me for any noobity.

In AndEngine , for your Entity to have an update method that gets invoked at every frame, its class must implement the IUpdateHandler interface.

This will force you to override the public void onUpdate(float pSecondsElapsed) and public void reset() methods.

You then place your "update code" in the body of onUpdate(float pSecondsElapsed) .

The instantiated object implementing the IUpdateHandler interface then must be registered in your Scene object via the registerUpdateHandler(IUpdateHandler updateHandler) .

Here's an example code:

Entity class with the update method.

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

Scene mainScene;

You register the Entity like this:

Entity entity = new Entity();

this.mainScene.registerUpdateHandler(entity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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