我正在使用 2 个 UpdateHandlers 一个来添加将 TimerHandler 设置为 2 秒的精灵,另一个用于删除在每一帧上更新的精灵(碰撞的精灵)。问题是应该删除精灵更新的处理程序取决于“TimerSeconds”参数(在本例中为 2 秒)。因此,如果我将 TimerSeconds 设置为 10 秒,则碰撞的精灵不会被删除。
this.registerUpdateHandler(new TimerHandler(2f,true, new ITimerCallback(){
@Override
public void onTimePassed(final TimerHandler pTimerHandler) {
final FixtureDef PLAYER_FIX = PhysicsFactory.createFixtureDef(25.0f, 1.0f, 0.25f);
Sprite redBombSprite = new Sprite(SceneManager.CAMERA_WIDTH/2,SceneManager.CAMERA_HEIGHT/8,bombTextureRegion,engine.getVertexBufferObjectManager());
redBombSprite.setX(redBombSprite.getX()-redBombSprite.getWidth()/2);
redBombSprite.setY(redBombSprite.getY()-redBombSprite.getHeight()/2);
redBombSprite.setRotation(35.0f);
attachChild(redBombSprite);
float x = MathUtils.random(-600f,600f);
float y = MathUtils.random(-600f,600f);
Vector2 impulse = new Vector2(x,y);
Body redBomb = PhysicsFactory.createBoxBody(physicsWorld, redBombSprite, BodyType.DynamicBody, PLAYER_FIX);
redBomb.setAttachedSprite(redBombSprite);
redBomb.applyLinearImpulse(impulse, redBomb.getPosition());
redBomb.setUserData("bomb");
redBombs.add(redBomb);
physicsWorld.registerPhysicsConnector(new PhysicsConnector(redBombSprite, redBomb, true, true));
}
}));
删除碰撞的精灵:
public IUpdateHandler getCollisionUpdateHandler(){
return new IUpdateHandler(){
@Override
public void onUpdate(float pSecondsElapsed) {
for(int i=0;i<redBombs.size()-1;i++) {
if(redBombs.get(i).getAttachedSprite().isDelete()){
final Sprite Object = redBombs.get(i).getAttachedSprite();
redBombs.remove(i);
final Body body = physicsWorld.getPhysicsConnectorManager().findBodyByShape(Object);
physicsWorld.destroyBody(body);
detachChild(Object);
}
}
}
在此先感谢您,并对复杂的代码感到抱歉。