[英]Andengine UpdateHandler
I'm using 2 UpdateHandlers one to add sprites with a TimerHandler set to 2 seconds and another one to delete sprites(the ones that collided) that updates on every frame. 我正在使用2个UpdateHandlers,一个用于添加带有设置为2秒的TimerHandler的精灵,另一个用于删除在每一帧上更新的精灵(发生冲突的精灵)。 The problem is that the handler that should delete the sprites updates depended on the "TimerSeconds" parameter(in this case 2 seconds).
问题在于应该删除Sprite更新的处理程序取决于“ TimerSeconds”参数(在这种情况下为2秒)。 So if I set the TimerSeconds to something like 10s than the collided sprites dont get deleted.
因此,如果将TimerSeconds设置为10s左右,则碰撞后的精灵不会被删除。
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));
}
}));
delete collided sprites: 删除碰撞的精灵:
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);
}
}
}
Thank you in advance, and sorry for the convoluted code. 在此先感谢您,对不起代码。
For checking sprite collision, use collidesWith() method like sprite1.collidesWith(sprite2) 要检查精灵冲突,请使用collidesWith()方法,例如sprite1.collidesWith(sprite2)
Use above method in TimeHandler. 在TimeHandler中使用以上方法。 Depending upon time pass (2f or 10f as per your requirement) collidesWith method run.. So you can delete sprite after some seconds..
根据时间的流逝(根据您的要求为2f或10f)collidesWith方法运行。因此,您可以在几秒钟后删除精灵。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.