我的FitViewport不适用于所有屏幕

时间:2017-09-06 09:01:58

标签: java libgdx resize

通过互联网搜索和搜索如何在LibGDX(这里和其他网站)中正确调整大小后,我的Tic Tac Toe游戏有一个大问题。按预期切换屏幕时,我的FitViewport不会调整大小。我确实用VLC Media Player制作了两个视频来解释我的问题。

这是第一个。这个显示在调整窗口大小时一切都很完美。

https://vid.me/UvlAf

在这里显示我的问题。

https://vid.me/r15cW

我不知道我的问题是,当我使用 ScreenManager 创建一个新屏幕时,我不会在新实例化的屏幕上调用resize。如果是这样,那么我想知道该怎么做。

主要类

public class TicTacToeGame extends ApplicationAdapter {

private TicTacToeScreenManager screenManager;

public static final int WINDOW_WIDTH = 900;
public static final int WINDOW_HEIGHT = 1200;

public SpriteBatch batch;

@Override
public void create () {

    screenManager = new TicTacToeScreenManager();
                                                    //index
    screenManager.AddScreen(HomeScreen.class);      // 0
    screenManager.AddScreen(SettingsScreen.class);  // 1
    screenManager.AddScreen(GameScreen.class);      // 2
    screenManager.AddScreen(GameOverScreen.class);  // 3
    screenManager.AddScreen(CreditsScreen.class);   // 4

    batch = new SpriteBatch();
}

@Override
public void resize (int width, int height) {

    if(screenManager.getCurrentScreen() != null)
    {
        screenManager.getCurrentScreen().resize(width, height);
    }
}

@Override
public void render () {

    screenManager.render(batch);
}

@Override
public void pause () {
}

@Override
public void resume () {
}

@Override
public void dispose () {
    batch.dispose();
}

}

RenderingScreen

public abstract class RenderingScreen implements InputProcessor, Disposable{

 protected OrthographicCamera camera;
 protected Vector3 mouse;
 protected Viewport viewport;
 protected Stage stage;
 protected TicTacToeScreenManager screenManager;

 /**
  * Default Constructor. 
  */
 public RenderingScreen(TicTacToeScreenManager screenManager)
 {
     this.screenManager = screenManager;
     camera = new OrthographicCamera();
     camera.position.set(480, 640, 0);

     viewport = new FitViewport(TicTacToeGame.WINDOW_WIDTH, TicTacToeGame.WINDOW_HEIGHT, camera);
     mouse = new Vector3();

     stage = new Stage(viewport);
     Gdx.input.setInputProcessor(stage);
 }

/**
 *  Initialize images, stage, skin, etc.
 */
public abstract void onInit();

public abstract void drawGraphicRender(SpriteBatch batch);

public void resize(int width, int height)
{
    viewport.update(width, height);
    stage.getViewport().update(width, height, true);
    camera.update();
}

/**
 * Release what we have used
 */
@Override
public void dispose() {
}

/**
 * Clear the screen.
 */
public void clearScreen()
{
    Gdx.gl.glClearColor( 0, 0, 0, 1 );
    Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
}


/** Called when a key was pressed
 * 
 * @param keycode one of the constants in {@link Input.Keys}
 * @return whether the input was processed */
public boolean keyDown (int keycode)
{
    return false;
}

/** Called when a key was released
 * 
 * @param keycode one of the constants in {@link Input.Keys}
 * @return whether the input was processed */
public boolean keyUp (int keycode)
{
    return false;
}

/** Called when a key was typed
 * 
 * @param character The character
 * @return whether the input was processed */
public boolean keyTyped (char character)
{
    return false;
}

/** Called when the screen was touched or a mouse button was pressed. The button parameter will be {@link Buttons#LEFT} on iOS.
 * @param screenX The x coordinate, origin is in the upper left corner
 * @param screenY The y coordinate, origin is in the upper left corner
 * @param pointer the pointer for the event.
 * @param button the button
 * @return whether the input was processed */
public boolean touchDown (int screenX, int screenY, int pointer, int button)
{
    return false;
}

/** Called when a finger was lifted or a mouse button was released. The button parameter will be {@link Buttons#LEFT} on iOS.
 * @param pointer the pointer for the event.
 * @param button the button
 * @return whether the input was processed */
public boolean touchUp (int screenX, int screenY, int pointer, int button)
{
    return false;
}

/** Called when a finger or the mouse was dragged.
 * @param pointer the pointer for the event.
 * @return whether the input was processed */
public boolean touchDragged (int screenX, int screenY, int pointer)
{
    return false;
}

/** Called when the mouse was moved without any buttons being pressed. Will not be called on iOS.
 * @return whether the input was processed */
public boolean mouseMoved (int screenX, int screenY)
{
    return false;
}

/** Called when the mouse wheel was scrolled. Will not be called on iOS.
 * @param amount the scroll amount, -1 or 1 depending on the direction the wheel was scrolled.
 * @return whether the input was processed. */
public boolean scrolled (int amount)
{
    return false;
}

final public void render(SpriteBatch batch) {

    drawGraphicRender(batch);
}

}

屏幕显示器

public class TicTacToeScreenManager {

@SuppressWarnings("rawtypes")
private ArrayList<Class> screens = new ArrayList<Class>();
private int activeScreen;
private RenderingScreen currScreen, nextScreen;

//transition style
public enum Transition_Style
{
    SMOOTH, SLICE, SLIDE;
}

private Transition_Style transition_Style;

//handling transitions
private FrameBuffer currFbo, nextFbo;
private SpriteBatch batch;
private float transitionDuration;
private ScreenTransition screenTransition;
private boolean transitionRunning;
private int nextScreenIdx = -1;


//create an instance..
private  RenderingScreen createInstance(Class<? extends RenderingScreen> screen) {

    try {
        Constructor constr = ClassReflection.getDeclaredConstructor(screen, this.getClass());
        RenderingScreen renderingScreen = (RenderingScreen) constr.newInstance(this);
        renderingScreen.onInit();
        return renderingScreen;
    } catch (ReflectionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}


/**
 * Render the current screen to the GdxGraphics
 * @param g The instance of GdxGraphics to draw on
 */
public void render(SpriteBatch b) {

    // Normal case, no transition
    if (!transitionRunning) {   
        currScreen.render(b);           
    }else {
        // Called at the beginning of the transition
        if (screenTransition == null) {

            int w = Gdx.graphics.getWidth();
            int h = Gdx.graphics.getHeight();
            this.currFbo = new FrameBuffer(Pixmap.Format.RGB888, w, h, false);
            this.nextFbo = new FrameBuffer(Pixmap.Format.RGB888, w, h, false);
            batch = new SpriteBatch();

            switch(transition_Style){

                case SLICE:
                    screenTransition = new SliceTransition(1.15f, Directions.UP_DOWN, 12, Interpolation.pow4Out);
                    break;

                case SMOOTH:
                    screenTransition = new AlphaFadingTransition(1.5f);
                    break;

                case SLIDE:
                    screenTransition = new SlideTransition(0.75f, Direction.LEFT, Interpolation.circleIn, true);
                    break;
            }


            // Render current screen to FBO
            currFbo.begin();
            currScreen.render(b);
            currFbo.end();

            // Releasing resources from the current screen
            currScreen.dispose();
            currScreen = null;

            nextScreen = createInstance(screens.get(getNextScreenIndex()));

            // Render next screen to FBO
            nextFbo.begin();
                nextScreen.render(b);
            nextFbo.end();

        }

        // Do the transition
        transition(Gdx.graphics.getDeltaTime());
    }
}

/**
 * Do the actual transition between the screens
 * @param deltaTime
 */
private void transition(float deltaTime) {

    float duration = screenTransition.getDuration();
    // Update the progress of ongoing transition
    transitionDuration = Math.min(transitionDuration + deltaTime, duration);

    // When transition is over
    if (screenTransition == null || transitionDuration >= duration) {
        screenTransition = null;
        transitionRunning = false;
        activeScreen = getNextScreenIndex();
        currScreen = nextScreen;
        nextScreen = null;
        transitionDuration = 0;
        return;
    }

    // Render the transition effect to screen
    float alpha = transitionDuration / duration;
    screenTransition.render(batch,
            currFbo.getColorBufferTexture(),
            nextFbo.getColorBufferTexture(),
            alpha);

}

// Go to the next screen if no target index has been choose
private int getNextScreenIndex() {

    if (nextScreenIdx < 0)
        return (activeScreen + 1) % screens.size();

    return nextScreenIdx;
}

// Check if the screen index is out of bound or not
private int checkIndexBounds(int screenIndex) {

    if (screenIndex < 0)        
        return 0;

    if (screenIndex > screens.size() - 1)
        return screens.size() - 1;

        return screenIndex;
    }

/**
 * Add a screen to the screen list
 * 
 * @param screen The class of the screen to add (we only want classes whose the super-class is RenderingScreen)
 */
public void AddScreen(Class<? extends RenderingScreen> screen){

    screens.add(screen);

    if (currScreen == null) {

        currScreen = createInstance(screens.get(0));
    }
}


/**
 * Called to transition to the next screen with slicing
 */
public void sliceTransitionToNext(int index) {
    if (activeScreen == index)
        return;

    if (!transitionRunning) {
        nextScreenIdx = checkIndexBounds(index);
        transitionRunning = true;
        transition_Style = Transition_Style.SLICE;
    }
}

/**
 * Called to transition to the next screen with fade-out
 */
public void smoothTransitionToNext(int index) {

    if (activeScreen == index)
        return;

    if (!transitionRunning) {
        nextScreenIdx = checkIndexBounds(index);
        transitionRunning = true;
        transition_Style = Transition_Style.SMOOTH;
    }


}




/**
 * Called to transition to the next screen with sliding
 */
public void SlideTransitionToNext(int index)
{
    if (activeScreen == index)
        return;

    if (!transitionRunning) {
        nextScreenIdx = checkIndexBounds(index);
        transitionRunning = true;
        transition_Style = Transition_Style.SLIDE;
    }
}

/**
 * @return The active screen which is currently rendered
 */
public RenderingScreen getCurrentScreen() {

    return !transitionRunning ? currScreen : null;
}

}

设置屏幕

public class SettingsScreen extends RenderingScreen{

public enum ComputerLevel
{
    EASY, MEDIUM, ADVANCED;
}


public static ComputerLevel level;

private GameComponent compo;

private Skin skin, skin1, holo;

private boolean nameOk;
private boolean opponentOk;
public static boolean MusicIsPlaying = true;

private TextField name;
private TextField opponent;
private TextFieldStyle textStyle;

private Texture textOk;
private Texture textFalse;
private Texture piece1, piece2, piece3;
private Texture ORIGINAL_X, ORIGINAL_O;
private Texture bgWhite;

private Image cross, circle, cross1, circle1;
private Image piecechoice1, piecechoice2, piecechoice3;

private BitmapFont fontmode;
private BitmapFont lovinn, lovinn1, lovinn2;

private TextButton back;
private TextButton start;
private TextButton SoundOn, SoundOff;
private TextButtonStyle buttonStyle;

private TextButton easy, medium, hard;

//static final attributes
private static final int SPACE_TEXTFIELD = 120;
private static final int WIDTH = 100;
private static final int SPACE = 120;
private static final int HEIGHT = 75;

//By default the name of player will be exactly Player1, Player2.
public static String playerName1 = "Player1";
public static String  playerName2 = "Player2";

public static int firstplayer = 0;
public static int PIECES_CHOICES = 0;
public static int compLevel = 0;

private float scale, posY, posSymbol;

private LabelStyle labStyle;

private Dialog error;
private String stringError;
private boolean OK;


public SettingsScreen(TicTacToeScreenManager screenManager) {

    super(screenManager);
}

@Override
public void onInit() {

    compo = new GameComponent();

    //Skin
    skin = new Skin(Gdx.files.internal("skin/comic-ui.json"));
    skin.add("bg", new Texture("images/textfieldbg.png"));
    skin.add("cursor", new Texture("images/cursor.png"));
    skin1 = new Skin(Gdx.files.internal("skin/comic-ui.json"));
    holo = new Skin(Gdx.files.internal("skin/uiskin.json"));


    textOk = new Texture("images/true.png");
    textFalse = new Texture("images/false.png");

    ORIGINAL_X = new Texture("images/cross.png");
    ORIGINAL_O = new Texture("images/circle.png");

    piece1 = new Texture("images/first.png");
    piece2 = new Texture("images/second.png");
    piece3 = new Texture("images/third.png");

    bgWhite = new Texture("images/white.png");



    fontmode = new BitmapFont(Gdx.files.internal("fonts/brushstroke.fnt"), Gdx.files.internal("fonts/brushstroke.png"), false);
    lovinn = new BitmapFont(Gdx.files.internal("fonts/Lovinn.fnt"), Gdx.files.internal("fonts/Lovinn.png"), false);
    lovinn1 = new BitmapFont(Gdx.files.internal("fonts/Lovinn.fnt"), Gdx.files.internal("fonts/Lovinn.png"), false);
    lovinn2 = new BitmapFont(Gdx.files.internal("fonts/Lovinn.fnt"), Gdx.files.internal("fonts/Lovinn.png"), false);

    fontmode.getData().setScale(.93f);
    lovinn.getData().setScale(1.5f);
    lovinn1.getData().setScale(.83f);


    labStyle = new LabelStyle();
    labStyle.font = lovinn2;
    labStyle.fontColor = Color.BLACK;

    //TextField and TextFieldStyle.
    textStyle = new TextFieldStyle();
    textStyle.font = lovinn1;
    textStyle.background = skin.getDrawable("bg");
    textStyle.cursor = skin.getDrawable("cursor");
    textStyle.fontColor = Color.BLACK;

    name = new TextField("Player1", textStyle);
    name.setWidth(320);
    name.setPosition(compo.chalkboard.getWidth() / 1.8f, compo.chalkboard.getHeight() - SPACE_TEXTFIELD);
    name.setTextFieldListener(new TextFieldListener() {
        @Override
        public void keyTyped(TextField textField, char key)
        {
            //When we pressed ENTER, do something!(for desktop or android ENTER)
            if(key == 13 || key == 10)
            {
                if(name.getText().isEmpty())
                {
                    nameOk = false;
                }else {
                    nameOk = true;
                    playerName1 = name.getText();
                }
            }
        }   
    });

    opponent = new TextField("Player2", textStyle);
    opponent.setWidth(320);
    opponent.setPosition(name.getX(), name.getY() - SPACE_TEXTFIELD);
    opponent.setTextFieldListener(new TextFieldListener() {
        @Override
        public void keyTyped(TextField textField, char key)
        {
            //When we pressed ENTER, do something!
            if(key == 13)
            {
                Gdx.input.setOnscreenKeyboardVisible(true);

                if(opponent.getText().isEmpty())
                {
                    opponentOk = false;
                }else {
                    opponentOk = true;
                    playerName2 = opponent.getText();
                }
            }
        }   
    });

    //Handling computer level
    easy = new TextButton("Easy", skin);
    easy.setSize(WIDTH, HEIGHT);
    easy.setPosition(name.getX(), opponent.getY() - 90);
    easy.setColor(Color.WHITE);
    easy.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            level = ComputerLevel.EASY;
            compLevel = 1;
            easy.setColor(Color.GREEN);
            medium.setColor(Color.WHITE);
            hard.setColor(Color.WHITE);
        }
    });


    medium = new TextButton("Med", skin);
    medium.setSize(WIDTH, HEIGHT);
    medium.setPosition(easy.getX() + SPACE, easy.getY());
    medium.setColor(Color.WHITE);
    medium.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            level = ComputerLevel.MEDIUM;
            compLevel = 2;
            medium.setColor(Color.YELLOW);
            hard.setColor(Color.WHITE);
            easy.setColor(Color.WHITE);
        }
    });

    hard = new TextButton("Hard", skin1);
    hard.setSize(WIDTH, HEIGHT);
    hard.setPosition(medium.getX() + SPACE, medium.getY());
    hard.setColor(Color.WHITE);
    hard.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            level = ComputerLevel.ADVANCED;
            compLevel = 3;
            hard.setColor(Color.CORAL);
            easy.setColor(Color.WHITE);
            medium.setColor(Color.WHITE);
        }
    });

    cross = new Image( ORIGINAL_X);
    cross.setPosition(compo.chalkboard.getWidth() / 6f, name.getY() + 16);
    cross.setScale(.4f, .4f);

    circle = new Image(ORIGINAL_O);
    circle.setPosition(cross.getX(), opponent.getY() + 16);
    circle.setScale(.4f, .4f);


    if(HomeScreen.MODE == 1)
    {
        scale = .5f;

    }else {
        scale = .4f;

    }

    if(HomeScreen.MODE == 1)
    {
        posSymbol = SPACE + HEIGHT;

    }else {

        posSymbol = SPACE + HEIGHT + 50;
    }
    cross1 = new Image( ORIGINAL_X);
    cross1.setScale(.45f, .45f);
    cross1.setPosition(name.getX() + 100, compo.chalkboard.getY() + posSymbol);
    cross1.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            firstplayer = 1;
            cross1.setColor(Color.RED);
            circle1.setColor(Color.WHITE);
        }
    });


    circle1 = new Image(ORIGINAL_O);
    circle1.setScale(.45f, .45f);
    circle1.setPosition(name.getX() + 250, compo.chalkboard.getY() + posSymbol);
    circle1.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            firstplayer = 2;
            circle1.setColor(Color.YELLOW);
            cross1.setColor(Color.WHITE);
        }
    });

    if(HomeScreen.MODE == 1)
    {
        posY = 110;
    }else {
        posY = 150;
    }
    //Piece Choices
    piecechoice1 = new Image(piece1);
    piecechoice1.setPosition(cross1.getX() - 160, cross1.getY() - posY);
    piecechoice1.setSize(120, 60);
    piecechoice1.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            PIECES_CHOICES = 1;
        }   
    });

    piecechoice2 = new Image(piece2);
    piecechoice2.setPosition(piecechoice1.getX() + 150, cross1.getY() - posY);
    piecechoice2.setSize(120, 60);
    piecechoice2.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            PIECES_CHOICES = 2;
        }   
    });

    piecechoice3 = new Image(piece3);
    piecechoice3.setPosition(piecechoice2.getX() + 150, cross1.getY() - posY);
    piecechoice3.setSize(120, 60);
    piecechoice3.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            PIECES_CHOICES = 3;
        }   
    });


    //button back and start
    buttonStyle = new TextButtonStyle();
    buttonStyle.font = lovinn;
    buttonStyle.fontColor = Color.valueOf("65390C");

    back = new TextButton("back", buttonStyle);
    back.setPosition(compo.smallboard.getImageX(), compo.smallboard.getY() - 15);
    back.addCaptureListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            screenManager.SlideTransitionToNext(0);
        }
    });


    start = new TextButton("start", buttonStyle);
    start.setPosition(compo.smallboard.getImageX() + Gdx.graphics.getWidth() + 200, compo.smallboard.getY() - 10);
    start.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
             if(NoErrors()){
                 screenManager.sliceTransitionToNext(2);
            }
        }
    });

    //Button on/off for options
    SoundOn = new TextButton("On", skin);
    SoundOn.setPosition(name.getX(), name.getY());
    SoundOn.setSize(WIDTH, HEIGHT);
    SoundOn.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            MusicGame.MenuLoop.play();
            MusicIsPlaying = true;//A static boolean for the HomeScreen
        }   
    });

    SoundOff = new TextButton("Off", skin);
    SoundOff.setPosition(SoundOn.getX() + 200, SoundOn.getY());
    SoundOff.setSize(WIDTH, HEIGHT);
    SoundOff.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);

            if(MusicGame.MenuLoop.isPlaying())
            {
                MusicGame.MenuLoop.stop();
                MusicIsPlaying = false;//A static boolean for the HomeScreen
            }
        }   
    });

    compo.getActor(stage);

    switch(HomeScreen.MODE)
    {
        case 1:
            stage.addActor(easy);
            stage.addActor(medium);
            stage.addActor(hard);
            stage.addActor(name);
            stage.addActor(opponent);
            stage.addActor(start);
            stage.addActor(cross);
            stage.addActor(circle);
            stage.addActor(cross1);
            stage.addActor(circle1);
            stage.addActor(piecechoice1);
            stage.addActor(piecechoice2);
            stage.addActor(piecechoice3);
            break;

        case 2:
            stage.addActor(name);
            stage.addActor(opponent);
            stage.addActor(start);
            stage.addActor(cross);
            stage.addActor(circle);
            stage.addActor(cross1);
            stage.addActor(circle1);
            stage.addActor(piecechoice1);
            stage.addActor(piecechoice2);
            stage.addActor(piecechoice3);
            break;

        case 3:
            stage.addActor(SoundOn);
            stage.addActor(SoundOff);
            break;

        default:
            break;  
    }

    stage.addActor(back);
}



private boolean NoErrors()
{
    switch(HomeScreen.MODE)
    {
        case 1:

            if(firstplayer <= 0){

                stringError = "Who's the first player?";
                error = new Dialog("WARNING !", holo);
                error.text(stringError, labStyle);
                error.button("OK", true);
                error.setResizable(false);
                error.show(stage);

            }else if(PIECES_CHOICES == 0){

                stringError = "What is your piece ?";
                error = new Dialog("WARNING !", holo);
                error.text(stringError, labStyle);
                error.button("OK", true);
                error.setResizable(false);
                error.show(stage);

            }else if(compLevel == 0)
            {
                stringError = "What is Computer's Level?";
                error = new Dialog("WARNING !", holo);
                error.text(stringError, labStyle);
                error.button("OK", true);
                error.setResizable(false);
                error.show(stage);

            }else {

                OK = true;
            }

            break;

        case 2:

            if(firstplayer <= 0)
            {
                stringError = "Who's the first player?";
                error = new Dialog("WARNING !", holo);
                error.text(stringError, labStyle);
                error.button("OK");
                error.setResizable(false);

                error.show(stage);

            }else {

                OK = true;
            }

            break;
    }

    return OK;
}


public static ComputerLevel getLevel()
{
    return level;
}

private final void drawModes(SpriteBatch batch)
{
    if(HomeScreen.MODE == 1 || HomeScreen.MODE == 2)
    {
        if(nameOk)
        {
            batch.draw(textOk, compo.chalkboard.getWidth() + 17, name.getY() + 10, 50, 50);
        }else {
            batch.draw(textFalse, compo.chalkboard.getWidth() + 17, name.getY() + 10, 50, 50);
        }

        if(opponentOk)
        {
            batch.draw(textOk, compo.chalkboard.getWidth() + 17, opponent.getY() + 10, 50, 50);
        }else {
            batch.draw(textFalse, compo.chalkboard.getWidth() + 17, opponent.getY() + 10, 50, 50);
        }


        switch(PIECES_CHOICES)
        {
            case 1:
                batch.draw(bgWhite, piecechoice1.getX() - 15, piecechoice1.getY() - piece1.getHeight() / 9, 150, piece1.getHeight() / 2);
                break;

            case 2:
                batch.draw(bgWhite, piecechoice2.getX() - 15, piecechoice3.getY() - piece2.getHeight() / 9, 150, piece2.getHeight() / 2);
                break;

            case 3:
                batch.draw(bgWhite, piecechoice3.getX() - 15, piecechoice3.getY() - piece3.getHeight() / 9, 150, piece3.getHeight() / 2);
                break;

            default:
                break;
        }
    }

    switch(HomeScreen.MODE)
    {
        case 1:
            fontmode.draw(batch, "Player vs Computer", compo.chalkboard.getWidth() / 5.5f, compo.chalkboard.getHeight() * 1.12f);

            lovinn1.setColor(Color.LIGHT_GRAY);
            lovinn1.draw(batch, "Player", compo.chalkboard.getWidth() / 3.8f, name.getY() + 68);
            lovinn1.draw(batch, "Computer", compo.chalkboard.getWidth() / 3.8f, opponent.getY() + 68);

            lovinn1.setColor(Color.WHITE);
            lovinn1.draw(batch, "First Player", compo.chalkboard.getWidth() / 5, cross1.getY() + 60);
            lovinn1.draw(batch, "Pieces", compo.chalkboard.getWidth() / 5, piecechoice1.getY() + 60);

            break;
        case 2:
            fontmode.draw(batch, "Player vs Player", compo.chalkboard.getWidth() / 4.3f, compo.chalkboard.getHeight() * 1.12f);
            lovinn1.setColor(Color.LIGHT_GRAY);
            lovinn1.draw(batch, "Player 1", compo.chalkboard.getWidth() / 3.8f, name.getY() + 68);
            lovinn1.draw(batch, "Player 2", compo.chalkboard.getWidth() / 3.8f, opponent.getY() + 68);

            lovinn1.setColor(Color.WHITE);
            lovinn1.draw(batch, "First Player", compo.chalkboard.getWidth() / 5, cross1.getY() + 60);
            lovinn1.draw(batch, "Pieces", compo.chalkboard.getWidth() / 5, piecechoice1.getY() + 60);
            break;
        case 3:
            fontmode.draw(batch, "Options", compo.chalkboard.getWidth() / 2 - 50, compo.chalkboard.getHeight() * 1.12f);
            lovinn1.setColor(Color.LIGHT_GRAY);
            lovinn1.draw(batch, "Sound", cross.getX() + 30, SoundOn.getY() + 60);
            break;
    }

}

@Override
public void drawGraphicRender(SpriteBatch batch) {
    batch.setProjectionMatrix(camera.combined);
    camera.update();
    clearScreen();
    stage.act(Gdx.graphics.getDeltaTime());
    viewport.apply();
    stage.draw();
    batch.begin();
    drawModes(batch);
    batch.end();

}

/**
 * Release what we have used
 */
@Override
public void dispose() {
    skin.dispose();
    skin1.dispose();
    holo.dispose();
    stage.dispose();
    textOk.dispose();
    textFalse.dispose();
    ORIGINAL_O.dispose();
    ORIGINAL_X.dispose();
    lovinn.dispose();
    lovinn1.dispose();
    lovinn2.dispose();
    bgWhite.dispose();
    piece1.dispose();
    piece2.dispose();
    piece3.dispose();
    fontmode.dispose();
}

}

2 个答案:

答案 0 :(得分:1)

所以,在仔细查看我的代码之后,我终于想出了一个适合我游戏的计划。所以真正的问题出在我的 TicTacToeScreenManager类中。

这是我的ScreenManager,现在进行了适当的修改:

public class TicTacToeScreenManager {

private ArrayList<Class<? extends RenderingScreen>> screens = new ArrayList<Class<? extends RenderingScreen>>();
private int activeScreen;
private RenderingScreen currScreen, nextScreen, renderingScreen;

//transition style
public enum Transition_Style
{
    SMOOTH, SLICE, SLIDE;
}

private Transition_Style transition_Style;

//handling transitions
private FrameBuffer currFbo, nextFbo;
private SpriteBatch batch = new SpriteBatch();;
private float transitionDuration;
private ScreenTransition screenTransition;
private boolean transitionRunning;
private int nextScreenIdx = -1;


/**
 * Create an instance of RenderingScreen's class.
 * 
 * @param screen    the RenderingScreen's child class.
 * @return
 */
private  RenderingScreen createInstance(Class<? extends RenderingScreen> screen) {

    try {
        Constructor constr = ClassReflection.getDeclaredConstructor(screen, this.getClass());
        renderingScreen = (RenderingScreen) constr.newInstance(this);
        renderingScreen.Init();
    } catch (ReflectionException e) {
        e.printStackTrace();
    }

    return renderingScreen;
}


/**
 * Render the current screen to the GdxGraphics
 */
public void render() {

    float deltaTime = Gdx.graphics.getDeltaTime();

    // Normal case, no transition
    if(!transitionRunning) {

        currScreen.render(batch);
    }else {

        // Called at the beginning of the transition
        if (screenTransition == null) {

            int w = TicTacToeGame.WINDOW_WIDTH;
            int h = TicTacToeGame.WINDOW_HEIGHT;

            this.currFbo = new FrameBuffer(Pixmap.Format.RGB888, w, h, false);
            this.nextFbo = new FrameBuffer(Pixmap.Format.RGB888, w, h, false);

            switch(transition_Style){

                case SLICE:
                    screenTransition = new SliceTransition(1.15f, Directions.UP_DOWN, 12, Interpolation.pow4Out);
                    break;

                case SMOOTH:
                    screenTransition = new AlphaFadingTransition(1.5f);
                    break;

                case SLIDE:
                    screenTransition = new SlideTransition(0.75f, Direction.LEFT, Interpolation.circleIn, true);
                    break;
            }

            currFbo.begin();
            currScreen.resize(w, h);//before drawing the current screen we must resize!!!!
            currScreen.render(batch);
            currFbo.end();

            // Releasing resources from the current screen
            currScreen.dispose();
            currScreen = null;

            //create the instance of the next screen
            nextScreen = createInstance(screens.get(getNextScreenIndex()));


            // Render next screen to FBO
            nextFbo.begin();
            nextScreen.resize(w, h);//before drawing the next screen we also must resize!!!!
            nextScreen.render(batch);
            nextFbo.end();
        }

        //Now, before we do the transition we have to resize the whole screen again!!!
        resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        //do the transition.
        setTransition(deltaTime);
    }
}

/**
 * This function will resize the whole screen before drawing the transition between two screens.
 * 
 * @param width     the screen width.
 * @param height    the screen height.
 */
public void resize(int width, int height) {

     if (currScreen != null) {
         currScreen.resize(width, height);
     }

     if(nextScreen != null) {
        nextScreen.resize(width, height);
     }

}

/**
 * Do the actual transition between the screens
 * @param deltaTime
 */
private void setTransition(float deltaTime) {

    float duration = screenTransition.getDuration();
    // Update the progress of ongoing transition
    transitionDuration = Math.min(transitionDuration + deltaTime, duration);

    // When transition is over
    if (transitionDuration >= duration) {
        screenTransition = null;
        transitionRunning = false;
        currScreen = nextScreen;
        nextScreen = null;
        transitionDuration = 0;
        return;
    }

    // Render the transition effect to screen
    float alpha = transitionDuration / duration;
    screenTransition.render(batch,
            currFbo.getColorBufferTexture(),
            nextFbo.getColorBufferTexture(),
            alpha);

}

/**
 *  Go to the next screen if no target index has been choose.
 * @return
 */
private int getNextScreenIndex() {

    if (nextScreenIdx < 0) {
        return (activeScreen + 1) % screens.size();
    }

    return nextScreenIdx;
}

/**
 * Check if the screen index is out of bound or not
 * @param screenIndex
 * @return
 */
private int checkIndexBounds(int screenIndex) {

    if (screenIndex < 0) {  
        return 0;

    }else if (screenIndex > screens.size() - 1) {

        return screens.size() - 1;

    }else {

        return screenIndex;
    }
}

/**
 * Add a screen to the screen list
 * 
 * @param screen The class of the screen to add (we only want classes whose the super-class is RenderingScreen)
 */
public void AddScreen(Class<? extends RenderingScreen> screen){

    screens.add(screen);

    if (currScreen == null) {

        currScreen = createInstance(screens.get(0));
    }
}

/**
 *  Called to activate the next screen without any transition
 * @param nextScreen        the index of the nextScreen. (See in the class in TicTacToeGame.)
 */
public void NoTransitionToNext(int nextScreen){

    if(!transitionRunning){
        currScreen.dispose();
        nextScreenIdx = -1;
        activeScreen = checkIndexBounds(nextScreen);
        currScreen = createInstance(screens.get(activeScreen));
    }
}


/**
 * Called to transition to the next screen with slicing
 */
public void sliceTransitionToNext(int nextScreen) {

    if (!transitionRunning) {
        nextScreenIdx = checkIndexBounds(nextScreen);
        transitionRunning = true;
        transition_Style = Transition_Style.SLICE;
    }
}

/**
 * Called to transition to the next screen with fade-out
 */
public void smoothTransitionToNext(int nextScreen) {

    if (!transitionRunning) {
        nextScreenIdx = checkIndexBounds(nextScreen);
        transitionRunning = true;
        transition_Style = Transition_Style.SMOOTH;
    }   

}

/**
 * Called to transition to the next screen with sliding
 */
public void SlideTransitionToNext(int nextScreen)
{
    if (!transitionRunning) {
        nextScreenIdx = checkIndexBounds(nextScreen);
        transitionRunning = true;
        transition_Style = Transition_Style.SLIDE;
    }
}

/**
 * Get the current screen which is not transitioning.
 * 
 * @return The active screen which is currently rendered
 */
public RenderingScreen getCurrentScreen() {

    return !transitionRunning ? currScreen : null;
}

}

所以问题是,在渲染当前屏幕,下一个屏幕和转换之前,我没有调用resize函数。为了解决这个问题,我只需要在&#34;绘制&#34;之前调整大小。当前和下一个屏幕以及过渡效果。

现在这是我的想法的结果:https://youtu.be/46Pd4cWkroU

我没有意识到的另一个问题是与虚拟(宽度,高度)和Gdx图形(宽度,高度)混淆。

现在一切都像魅力一样!!

答案 1 :(得分:0)

也许尝试camera.viewport.width = width; 并在resize方法中为高度做同样的事情。