5

我正在制作小气球游戏。气球随机弹出并在一段时间后消失。当我点击它们时,我想让它们消失并显示 +1 而不是气球。当我点击气球时,我想拆下气球精灵。我的问题是当我在代码中调用 sprite.detachSelf() 时,精灵只是消失了,但实际上精灵并没有被删除。它只会变得不可见。当我再次单击该位置时,气球会出现,即使在气球消失后它也会显示+1。这意味着我认为气球没有正确分离。

这是我的代码:

@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);;
}
4

1 回答 1

17

附着时,需要分离;当你scene.registerTouchArea(sprite),你需要scene.unregisterTouchArea(sprite)

于 2012-10-05T04:41:08.613 回答