Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AndEngine : sprite.detachSelf() doesnt remove the sprite

I'm creating small balloon game. where balloons pop up randomly and disappear after a little while. When i clicked on them those i want to disappear them and show +1 instead of the balloon. When i click on the balloon, i want to deattach the balloon sprite. My problem is when i call sprite.detachSelf() in the code, the sprite just disapears but actually the sprite hasn't being removed. It only becomes invisible. When i click again on that place the balloon appears, even after the balloon has disappear it shows +1. Which means i think the balloon hasn't deattached correctly.

Here is my code:

@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);;
}
like image 923
posha Avatar asked Oct 04 '12 10:10

posha


1 Answers

When you attach, you need to detach; when you scene.registerTouchArea(sprite), you need to scene.unregisterTouchArea(sprite).

like image 172
gian1200 Avatar answered Sep 27 '22 16:09

gian1200