engine.registerUpdateHandler(new TimerHandler(0.2f,
new ITimerCallback() {
public void onTimePassed(final TimerHandler pTimerHandler) {
pTimerHandler.reset();
Rectangle xpRect = new Rectangle(30, 200,
(float) (((player.getXp()) / (player
.getNextLevelxp())) * 800), 40, vbom);
HUD.attachChild(xpRect);
}
}));
到目前为止,我在 createHUD 方法中已经有了这个。它非常简单,它创建一个矩形,显示玩家的 xp 与下一关所需的 xp 的关系,并将其附加到 HUD。唯一的问题是旧矩形永远不会被删除。我怎样才能拥有一个像这样的矩形,可以 self 更新并删除旧的矩形?
最佳答案
如果您过于频繁地使用 detachChild() 或任何其他分离方法,您迟早可能会遇到问题。特别是因为分离只能在更新线程
上进行。你永远不会知道你的矩形何时会再次分离。因此,为了节省大量的附加和分离操作,请重复使用矩形:
i) 将 Rectangle 的引用保存在某处(作为全局变量,例如在您的 Player
类中)。
ii) 在开始加载内容时也会初始化矩形:
Rectangle xpRect = new Rectangle(30, 200, 0, 40, vbom); // initialize it
HUD.attachChild(xpRect); // attach it where it belongs
xpRect.setVisible(false); // hide it from the player
xpRect.setIgnoreUpdate(true); // hide it from the update thread, because you don't use it.
此时,将矩形放在哪里或它有多大并不重要。重要的是它在那里。
iii) 现在,当您想向玩家显示他的 XP 时,只需将其设置为可见
public void showXP(int playerXP, int nextXP){
float width= (float) ((playerXP / nextXP) * 800); // calculate your new width
xpRect.setIgnoreUpdate(false); // make the update thread aware of your rectangle
xpRect.setWidth(width); // now change the width of your rectangle
xpRect.setVisible(true); // make the rectangle visible again
}
iv) 当您不再需要它时:为了使其再次不可见,只需调用
xpRect.setVisible(false); // hide it from the player
xpRect.setIgnoreUpdate(true); // hide it from the update thread, because you don't
当然,您现在可以以任何您喜欢的方式使用 showXP()
方法,并在 TimerHandler
中使用它。如果你想要更完整的外观,你可以这样做:
public void showXP(int playerXP, int nextXP){
float width= (float) ((playerXP / nextXP) * 800); // calculate your new width
xpRect.setIgnoreUpdate(false); // make the update thread aware of your rectangle
xpRect.setWidth(width); // now change the width of your rectangle
xpRect.setVisible(true);
xpRect.registerEntityModifier(new FadeInModifier(1f)); // only this line is new
}
其实和上面的方法是一样的,只是最后一行稍微改变一下,让矩形看起来更平滑一点...
关于android - 分 ionic 项、更新并附加?安卓引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15751808/