无法在一个屏幕

时间:2017-08-15 16:12:27

标签: java android toggle screen

您好我已经在我的SettingsMenu上实现了声音的切换按钮,它可以在此屏幕中工作,但是当我返回主菜单时,功能会丢失并重置回其初始状态。任何想法我怎么能这样做,一旦点击它,它在每个屏幕保持这个选择。我将向您展示切换按钮类和实现它的设置类。感谢。

package uk.ac.qub.eeecs.game.cardDemo;


public class ToggleButtonSound extends GameObject {

protected enum ButtonState {
    ON, OFF
}

private ButtonState mButtonState = ButtonState.OFF;

private SoundManager sm;

protected Bitmap mOffBitmap;

protected Bitmap mOnBitmap;

protected Bitmap mOnHoverBitmap;

protected Bitmap mOffHoverBitmap;

private Game mGame;

public boolean mMusicMuted = false;
public boolean mSoundMuted = false;
private MusicManager mMusicManager;

public ToggleButtonSound(float x, float y, float width, float height,
                         String offBitmap,
                         String onBitmap,
                        // String OffHoverBitmap,
                         //String OnHoverBitmap,
                         GameScreen gameScreen,
                         Game game) {

    super(x, y, width, height,
            gameScreen.getGame().getAssetManager().getBitmap(offBitmap), gameScreen);
    mGame = game;
    AssetStore assetStore = gameScreen.getGame().getAssetManager();
    mOffBitmap = assetStore.getBitmap(offBitmap);
   // mOffHoverBitmap = assetStore.getBitmap(offBitmap);
    mOnBitmap = assetStore.getBitmap(onBitmap);
    //mOnHoverBitmap = assetStore.getBitmap(onBitmap);

    sm = new SoundManager(mGame);
    mMusicManager = new MusicManager(mGame);
}

private boolean mPushTriggered;
private boolean mIsPushed;

/**
 * Update the button
 *
 * @param elapsedTime Elapsed time information
 */
public void update(ElapsedTime elapsedTime) {
    // Consider any touch events occurring in this update
    Input input = mGame.getInput();
    BoundingBox bound = getBound();

    // Check for a press release on this button
    for (TouchEvent touchEvent : input.getTouchEvents()) {
        if (touchEvent.type == TouchEvent.TOUCH_UP
                && bound.contains(touchEvent.x, touchEvent.y)) {
            // A touch up has occured in this control
            if (mButtonState == ButtonState.OFF) {
                setToggled(true);
            } else {
                setToggled(false);
            }

            return;
        }
    }

    for (int idx = 0; idx < TouchHandler.MAX_TOUCHPOINTS; idx++) {
        if (input.existsTouch(idx)) {
            if (bound.contains(input.getTouchX(idx), input.getTouchY(idx))) {
                if (!mIsPushed) {
                    mPushTriggered = true;
                    if (mOnHoverBitmap != null && mOffHoverBitmap != null)
                        mBitmap = mButtonState == ButtonState.ON ? mOnHoverBitmap : mOffHoverBitmap;
                    mIsPushed = true;
                }
                return;
            }
        }
    }

    if (mIsPushed) {
        mBitmap = mButtonState == ButtonState.ON ? mOnBitmap : mOffBitmap;
        mIsPushed = false;
        mPushTriggered = false;
    }
}


public boolean isToggledOn() {
    return mButtonState == ButtonState.ON;
}

public void setToggled(boolean on) {
    if (on) {
        mButtonState = ButtonState.ON;
        mBitmap = mOnBitmap;
    } else {
        mButtonState = ButtonState.OFF;
        mBitmap = mOffBitmap;
    }
}

@Override
public void draw(ElapsedTime elapsedTime, IGraphics2D graphics2D,
                 LayerViewport layerViewport, ScreenViewport screenViewport) {

    // Assumed to be in screen space so just draw the whole thing

    drawScreenRect.set((int) (position.x - mBound.halfWidth),
            (int) (position.y - mBound.halfWidth),
            (int) (position.x + mBound.halfWidth),
            (int) (position.y + mBound.halfHeight));

    graphics2D.drawBitmap(mBitmap, null, drawScreenRect, null);

}

}

设置菜单

public class SettingsMenu extends GameScreen {


private ToggleButtonSound mToggleButtonSound;

public SettingsMenu(Game game) {
    super("Settings Menu", game);
    mSoundManager = new SoundManager(game);
    mMenuScreen = new MenuScreen(game);
music = new MusicManager(game);
    AssetStore assetManager = mGame.getAssetManager();
    assetManager.loadAndAddBitmap("offswitch", "img/offswitch.png");
    assetManager.loadAndAddBitmap("onswitch", "img/onswitch.png");
    assetManager.loadAndAddBitmap("SoundOn", "img/Soundon.png");
    assetManager.loadAndAddBitmap("SoundOff", "img/Soundoff.png");
    assetManager.loadAndAddBitmap("SettingsBackground", "img/settingsbackground.png");
    //Load in sound
    assetManager.loadAndAddSound("mouseClick", "Sounds/clicksound.wav");
    assetManager.loadAndAddMusic("BackgroundMusic", "Sounds/MenuScreenBackgroundMusic.mp3");
    //initialise sound

    int spacingX = mGame.getScreenWidth() / 3;
    int spacingY = mGame.getScreenHeight() / 9;
    mPlayButtonBound = new Rect(spacingX, 3 * spacingY, 2 * spacingX, 4 * spacingY);
    mInventoryButtonBound = new Rect(spacingX, 5 * spacingY, 2 * spacingX, 6 * spacingY);
    soundButtonBound = new Rect(game.getScreenWidth() / 100, game.getScreenWidth() / 100, game.getScreenWidth() / 10, game.getScreenWidth() / 10);
    mSettingsBackground = new Rect(1, 1, game.getScreenWidth(), game.getScreenHeight());

    mLayerViewport = new LayerViewport(0, 0, game.getScreenWidth(), game.getScreenHeight());
    mScreenViewport = new ScreenViewport(0, 0, game.getScreenWidth(), game.getScreenHeight());

    int toggleHeight = mGame.getScreenHeight()/3;
    mToggleButtonMusic = new ToggleButtonMusic(game.getScreenWidth() / 100, game.getScreenWidth() / 200, game.getScreenWidth() / 10, game.getScreenWidth() / 20, "onswitch","offswitch","onswitch","offswitch", this, mGame);
    mToggleButtonSound = new ToggleButtonSound(game.getScreenWidth() / 100, game.getScreenWidth() / 200, game.getScreenWidth() / 10, game.getScreenWidth() / 20, "onswitch","offswitch", this, mGame);
    mToggleButtonMusic.setPosition(game.getScreenWidth()/2,game.getScreenHeight()/3);
    mToggleButtonSound.setPosition(game.getScreenWidth()/2, toggleHeight*2);
    //toggleB.getBitmap();
    //toggleB.getBound();
}

private boolean mIsPushed;

@Override
public void update(ElapsedTime elapsedTime) {
    music.isPlaying1();
    //mToggleButtonSound.setToggled(soundMuted);

    mToggleButtonMusic.update(elapsedTime);
    mToggleButtonSound.update(elapsedTime);

    Input input = mGame.getInput();
    List<TouchEvent> touchEvents = input.getTouchEvents();

    if (touchEvents.size() > 0) {
        TouchEvent touchEvent = touchEvents.get(0);

        if (touchEvent.type == TouchEvent.TOUCH_UP) {

            if ((soundButtonBound.contains((int) touchEvent.x, (int) touchEvent.y))) {
                mGame.getScreenManager().removeScreen("Settings Menu");
                mMenuScreen = new MenuScreen(mGame);
                mGame.getScreenManager().addScreen(mMenuScreen);
            }

        }
    }
}

@Override
public void draw(ElapsedTime elapsedTime, IGraphics2D graphics2D) {

    graphics2D.drawBitmap(mGame.getAssetManager().getBitmap("SettingsBackground"), null, mSettingsBackground, null);
    mToggleButtonMusic.draw(elapsedTime,graphics2D,mLayerViewport, mScreenViewport);
    mToggleButtonSound.draw(elapsedTime,graphics2D,mLayerViewport, mScreenViewport);
    graphics2D.drawBitmap(mGame.getAssetManager().getBitmap("SoundOn"), null, soundButtonBound, null);
}

@Override
public boolean onBackPressed() {

    return true;
}

}

0 个答案:

没有答案