我正在使用2个UpdateHandlers,一个用于添加设置为2秒的TimerHandler的精灵,另一个用于删除每个帧上更新的精灵(碰撞的精灵)。 问题是应该删除sprite更新的处理程序依赖于“TimerSeconds”参数(在这种情况下为2秒)。因此,如果我将TimerSeconds设置为10s,而不是碰撞的sprite,则不会被删除。
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);
}
}
}
提前感谢您,对于错综复杂的代码感到抱歉。
答案 0 :(得分:1)
要检查精灵碰撞,请使用像sprite1.collidesWith(sprite2)这样的collidesWith()方法
在TimeHandler中使用上述方法。取决于时间的推移(根据您的要求,2f或10f) collidesWith方法运行..所以你可以在几秒后删除精灵..