我正在尝试在LibGDX中制作Tick Tack Toe游戏,我想从用户播放的屏幕(PlayGround)移动到显示结果的屏幕(End),但PlayGround屏幕中的渲染方法仍然是虽然我已将屏幕更改为End,但已调用。在调试之后,我重新启动了只调用End屏幕中的show方法,但仍然从PlayGround调用render方法而不是从End调用。
PlayGround代码:
public class PlayGround implements Screen {
MainMenu game;
Stage stage;
Texture gridTex;
Image gridImage ;
TextButton restart ;
TextButtonStyle style ;
String turn ;
SpriteBatch batch;
Zone[] zones = new Zone[9];
End end;
XO game1;
public PlayGround(MainMenu game){
this.game = game;
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
game1 = new XO();
end = new End(this);
//Draw Grid
gridTex = new Texture("images/playground/grid.png");
gridImage = new Image(gridTex);
batch = new SpriteBatch();
gridImage.setPosition(40, 100);
//Draw Restart
style = new TextButtonStyle();
style.font = MainMenu.defaultFont;
TextureRegionDrawable drawable =new TextureRegionDrawable(Zone.atlas.findRegion("restart"));
style.up = drawable ;
style.down = drawable ;
restart = new TextButton("Restart",style);
//Create the Zones
zones[0] = new Zone(50, 378);
zones[1] = new Zone(151, 378);
zones[2] = new Zone(251, 378);
zones[3] = new Zone(50, 242);
zones[4] = new Zone(151, 242);
zones[5] = new Zone(251, 242);
zones[6] = new Zone(50, 108);
zones[7] = new Zone(151, 108);
zones[8] = new Zone(251, 108);
restart.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
for(int i = 0 ; i < zones.length ; i++)
{
zones[i].setPlayer(0) ;
zones[i].setClicked(false);
zones[i].getImage().setDrawable(new TextureRegionDrawable(Zone.atlas.findRegion("empty")));
}
Zone.counter = 1 ;
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
}
});
//Creating the stage and adding actors
stage = new Stage(380, 630, true);
stage.addActor(gridImage);
for(int i = 0 ; i< zones.length ; i++)
stage.addActor(zones[i].getImage());
stage.addActor(restart);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
//It clears the older screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
System.out.println(Zone.counter);
if(Zone.counter%2==0)
{turn = "O's TURN";}
else
{turn = "X's TURN";}
check();
//Drawing the stage and it's children's
batch.begin();
stage.draw();
batch.end();
batch.begin();
MainMenu.title.draw(batch,turn, 114 , 570);
batch.end();
//Fires the listeners
stage.act(delta);
}
public void check()
{
//Horizontal
for(int i = 0 ; i<7;i += 3)
{
if(zones[i].getPlayer()==1 && zones[i+1].getPlayer()==1 && zones[i+2].getPlayer()==1)
{
System.out.println("Player 1 wins");
End.setWinner(1);
stage.clear();
game1.setScreen(end);
}
else if(zones[i].getPlayer()==2 && zones[i+1].getPlayer()==2 && zones[i+2].getPlayer()==2)
{
System.out.println("Player 2 wins");
End.setWinner(2);
stage.clear();
game1.setScreen(end);
}
else if (Zone.counter == 10)
{
stage.clear();
game1.setScreen(end);
}
}
//Vertical :
for(int i = 0 ; i<3;i++)
{
if(zones[i].getPlayer()==1 && zones[i+3].getPlayer()==1 && zones[i+6].getPlayer()==1)
{
System.out.println("Player 1 wins");
End.setWinner(1);
stage.clear();
game1.setScreen(end);
}
else if(zones[i].getPlayer()==2 && zones[i+3].getPlayer()==2 && zones[i+6].getPlayer()==2)
{
System.out.println("Player 2 wins");
End.setWinner(2);
stage.clear();
game1.setScreen(end);
}
else if (Zone.counter == 10)
{System.out.println("Tie");
stage.clear();
game1.setScreen(end);
}
}
//Diagonal LeftToRight
if(zones[0].getPlayer()==1 && zones[4].getPlayer()==1 && zones[8].getPlayer()==1)
{System.out.println("Player 1 wins");
End.setWinner(1);
stage.clear();
game1.setScreen(end);
}
if(zones[0].getPlayer()==2 && zones[4].getPlayer()==2 && zones[8].getPlayer()==2)
{System.out.println("Player 2 wins");
End.setWinner(2);
stage.clear();
game1.setScreen(end);
}
//Diagonal RightToLeft
if(zones[2].getPlayer()==1 && zones[4].getPlayer()==1 && zones[6].getPlayer()==1)
{System.out.println("Player 1 wins");
End.setWinner(1);
stage.clear();
game1.setScreen(end);
}
if(zones[2].getPlayer()==2 && zones[4].getPlayer()==2 && zones[6].getPlayer()==2)
{System.out.println("Player 2 wins");
End.setWinner(2);
stage.clear();
game1.setScreen(end);
}
}
结束屏幕中的代码:
package com.teamthawra.www.Screens;
public class End implements Screen {
static int winner = 0;
SpriteBatch batch;
static Stage stage;
XO game;
MainMenu main;
PlayGround playground;
public End (PlayGround playground) {
this.playground = playground;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
System.out.println("Rendering in end");
if (winner == 1){
batch.begin();
MainMenu.title.draw(batch, "X WON!", 114, 570);
batch.end();
}
else if (winner == 2){
batch.begin();
MainMenu.title.draw(batch, "O WON!", 114, 570);
batch.end();
}
else {
batch.begin();
MainMenu.title.draw(batch, "It's a tie", 114, 570);
batch.end(); }
batch.begin();
MainMenu.title.draw(batch, "Press anywhere to continue", 114, 300);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
System.out.println("I am at end");
game = new XO();
main = new MainMenu(this);
stage = new Stage(380, 630 , true);
Gdx.input.setInputProcessor(stage);
stage.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
stage.clear();
game.setScreen(main);
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
}
});
}
public static void setWinner(int winner) {
End.winner = winner;
}
public static int getWinner() {
return winner;
}
}
很抱歉添加了所有代码,因为我是初学者,这是我在LibGDX中的第一个程序,我不知道问题究竟在哪里。
答案 0 :(得分:1)
我不完全确定,但我最好的猜测是在 game1 (类型 XO 的新对象)上调用 setScreen()是问题。
如果我是正确的,你应该在同一时间游戏(或从LibGdx游戏继承的那个)上调用 setScreen() setScreen()为你的屏幕PlayGround。
例如,如果您有一个菜单屏幕,那么您可以使用此命令前往游乐场:
game.setScreen(playGroundScreen);
你应该在同一个游戏对象上切换屏幕,如下所示:
game.setScreen(endScreen);
而不是创建一个新变量(就像你对game1所做的那样)。
让我知道它是否有效。
答案 1 :(得分:0)
也许如果你正在学习libgdx,看看这个项目可能会有用:https://github.com/edesdan/libgdx-playground。这是libgdx游戏的基本实现结构。您可以将它作为游戏的起点。