Estoy creando un pequeño juego de globos. donde los globos aparecen aleatoriamente y desaparecen después de un rato. Cuando hice clic en ellos, quise desaparecerlos y mostrar +1 en lugar del globo. Cuando hago clic en el globo, quiero desentrañar el globo del sprite. Mi problema es cuando llamo a sprite.detachSelf() en el código, el sprite simplemente desaparece pero en realidad el sprite no se ha eliminado. Solo se vuelve invisible. Cuando hago clic de nuevo en ese lugar, aparece el globo, incluso después de que el globo ha desaparecido, muestra +1. Lo que significa que creo que el globo no se ha desacoplado correctamente.Android AndEngine: sprite.detachSelf() no elimina el sprite
Aquí está mi código:
@Override
protected Scene onCreateScene() {
//this.mEngine.registerUpdateHandler(new FPSLogger());
scene = new Scene();
backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion,
getVertexBufferObjectManager());
backgroundSprite.setSize(CAMERA_WIDTH, CAMERA_HEIGHT);
scene.attachChild(backgroundSprite);
scene.unregisterTouchArea(backgroundSprite);
text = new Text(0, 0, font, "Score : 00",
getVertexBufferObjectManager());
scene.attachChild(text);
textTime = new Text(displayMetrics.widthPixels - 220, 0, font,
"00 : 60", getVertexBufferObjectManager());
scene.attachChild(textTime);
timer = new TimerClock(1, new TimerClock.ITimerCallback() {
TimerClock t = timer;
public void onTick() {
System.out.println("timer inside");
if (time > 0) {
time = time - 1;
System.out.println("timer inside : " + time);
scene.detachChild(textTime);
textTime = new Text(displayMetrics.widthPixels - 220, 0,
font, "00 : " + time,
getVertexBufferObjectManager());
if (time < 10) {
textTime.setColor(1, 0, 0);
}
scene.attachChild(textTime);
deleteSpriteSpawnTimeHandler(sprite);
}
else{
scene.unregisterUpdateHandler(this.t);
}
}
});
this.mEngine.registerUpdateHandler(timer);
createSpriteSpawnTimeHandler();
return scene;
}
private void deleteSpriteSpawnTimeHandler(final IEntity ball) {
TimerHandler spriteTimerHandler1;
final Engine e = mEngine;
this.getEngine().registerUpdateHandler(
spriteTimerHandler1 = new TimerHandler(0.5f, true,
new ITimerCallback() {
@Override
public void onTimePassed(
final TimerHandler spriteTimerHandler1) {
spriteTimerHandler1.reset();
deleteSprite(ball);
}
}));
}
private void gameOverSpawnTimeHandler() {
TimerHandler spriteTimerHandler1;
final Engine e = mEngine;
this.getEngine().registerUpdateHandler(
spriteTimerHandler1 = new TimerHandler(60, true,
new ITimerCallback() {
@Override
public void onTimePassed(
final TimerHandler spriteTimerHandler1) {
spriteTimerHandler1.reset();
timeDue = 0;
scene.detachChild(textComment);
textComment = new Text(CAMERA_WIDTH/2 - 100,
CAMERA_HEIGHT/2, font,
"Game Over...!!!",
getVertexBufferObjectManager());
textComment.setColor(1.0f, 0.0f, 0.0f);
scene.attachChild(textComment);
SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs",
MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
String score1 = myPrefs.getString("SCORE1", "0");
String score2 = myPrefs.getString("SCORE2", "0");
int scoreInt1 = Integer.parseInt(score1);
int scoreInt2 = Integer.parseInt(score2);
System.out.println("session in" + score1 + " "
+ score2);
currScore = totalScore;
if (currScore > scoreInt1 && currScore > scoreInt2) {
prefsEditor.clear();
prefsEditor.commit();
prefsEditor.putString("SCORE1", String.valueOf(currScore));
prefsEditor.putString("SCORE2", String.valueOf(scoreInt1));
prefsEditor.commit();
} else if (currScore < scoreInt1 && currScore > scoreInt2) {
prefsEditor.clear();
prefsEditor.commit();
prefsEditor.putString("SCORE1", String.valueOf(scoreInt1));
prefsEditor.putString("SCORE2", String.valueOf(currScore));
prefsEditor.commit();
} else {
}
}
}));
}
private void createSpriteSpawnTimeHandler() {
TimerHandler spriteTimerHandler;
final Engine e = mEngine;
this.getEngine().registerUpdateHandler(
spriteTimerHandler = new TimerHandler(0.75f, true,
new ITimerCallback() {
@Override
public void onTimePassed(
final TimerHandler spriteTimerHandler) {
spriteTimerHandler.reset();
// scene.detachChild(backgroundSprite);
// scene.attachChild(backgroundSprite);
// Random Position Generator
final float xPos = MathUtils.random(50.0f,
(CAMERA_WIDTH - 50.0f));
final float yPos = MathUtils.random(75.0f,
(CAMERA_HEIGHT - 75.0f));
gameOverSpawnTimeHandler();
if (timeDue > 0) {
createSprite(xPos, yPos);
}else{
//scene.unregisterUpdateHandler(spriteTimerHandler);
}
}
}));
}
private void createSpriteTextSpawnTimeHandler() {
TimerHandler spriteTimerHandler;
final Engine e = mEngine;
this.getEngine().registerUpdateHandler(
spriteTimerHandler = new TimerHandler(mEffectSpawnDelay, true,
new ITimerCallback() {
@Override
public void onTimePassed(
final TimerHandler spriteTimerHandler) {
spriteTimerHandler.reset();
if (totalScore > 50 && totalScore < 60) {
textComment = new Text(150, 100, font,
"Ohhhh...you are doing good.",
getVertexBufferObjectManager());
textComment.setColor(1.0f, 0.0f, 0.0f);
scene.attachChild(textComment);
}
deleteSpriteSpawnTimeHandler(textComment);
// e.getScene().detachChild(backgroundSprite);
// e.getScene().attachChild(backgroundSprite);
}
}));
}
private void createSprite(final float pX, final float pY) {
sprite = new Sprite(pX, pY, this.mrball, getVertexBufferObjectManager()) {
Engine e = mEngine;
TextureRegion gball = mgball;
float x = pX;
float y = pY;
private int score = totalScore;
private Text textComment;;
@Override
public boolean onAreaTouched(
org.andengine.input.touch.TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
this.e.getScene().detachChild(this);
if (timeDue > 0) {
mBrushDrawingSound.play();
totalScore = totalScore + 1;
String score = "Score : " + totalScore;
scene.detachChild(text);
text = new Text(0, 0, font, score,
getVertexBufferObjectManager());
scene.attachChild(text);
//sprite.detachSelf();
createSpriteTextSpawnTimeHandler();
textScorePlus = new Text(x, y, font, "+1",
getVertexBufferObjectManager());
scene.attachChild(textScorePlus);
scene.unregisterTouchArea(textScorePlus);
deleteSpriteSpawnTimeHandler(textScorePlus);
}
} else if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
}
// e.getScene().unregisterTouchArea(sprite);
return true;
}
};
spriteBalloon.add(sprite);
sprite.setSize(100, 100);
sprite.setAlpha(0.8f);
Random randomGenerator = new Random();
red = randomGenerator.nextInt(255);
green = randomGenerator.nextInt(255);
blue = randomGenerator.nextInt(255);
sprite.setColor(red, green, blue);
scene.registerTouchArea(sprite);
scene.attachChild(sprite);
deleteSpriteSpawnTimeHandler(sprite);
}
private void deleteSprite(IEntity pBall) {
IEntity gball = pBall;
scene.detachChild(gball);;
}
¿Mi respuesta te ayudó? – gian1200
Gracias por la atención. En realidad, no era compañero de trabajo. Todavía estoy teniendo el mismo problema. El sprite no se une correctamente – posha
¿Colocación? Pensé que hablabas de separar y evitar los eventos táctiles. – gian1200