【发布时间】:2014-07-31 05:29:55
【问题描述】:
我正在尝试使用 libgdx 制作一个单词搜索应用程序。 创建了一个实现 Screen 的 AbstractScreen 类 我扩展了这个 AbstractScreen 类来为游戏创建不同的屏幕。
AbstractScreen.java
public abstract class AbstractScreen implements Screen
{
// the fixed viewport dimensions (ratio: 1.6)
public static final int GAME_VIEWPORT_WIDTH = 1080, GAME_VIEWPORT_HEIGHT = 1920;
public static final int MENU_VIEWPORT_WIDTH = 1080, MENU_VIEWPORT_HEIGHT = 1920;
protected final WordSearch1 game;
protected final Stage stage;
private BitmapFont font;
private SpriteBatch batch;
private Skin skin;
private TextureAtlas atlas;
private Table table;
public AbstractScreen(WordSearch1 game )
{
this.game = game;
int width = ( isGameScreen() ? GAME_VIEWPORT_WIDTH : MENU_VIEWPORT_WIDTH );
int height = ( isGameScreen() ? GAME_VIEWPORT_HEIGHT : MENU_VIEWPORT_HEIGHT );
this.stage = new Stage();
}
//**** Removed some method for this post ****
protected Table getTable()
{
if( table == null ) {
table = new Table( getSkin() );
table.setFillParent( true );
if( WordSearch1.DEV_MODE ) {
table.debug();
}
stage.addActor( table );
}
return table;
}
// Screen implementation
@Override
public void show()
{
Gdx.app.log( WordSearch1.LOG, "Showing screen: " + getName() );
// set the stage as the input processor
Gdx.input.setInputProcessor( stage );
}
@Override
public void resize(
int width,
int height )
{
Gdx.app.log( WordSearch1.LOG, "Resizing screen: " + getName() + " to: " + width + " x " + height );
}
@Override
public void render(
float delta )
{
stage.act( delta );
// clear the screen with the given RGB color (black)
Gdx.gl.glClearColor( 1f, 1f, 1f, 1f );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
// draw the actors
stage.draw();
// draw the table debug lines
Table.drawDebug( stage );
}
}
GameScreen.java
public class GameScreen extends AbstractScreen
{
public GameScreen( WordSearch1 game )
{
super( game );
}
@Override
public void show()
{
super.show();
TextButton startGameButton = new TextButton( "Start game", getSkin() );
String[][] data = gets a 2D array of letters for grid
// retrieve the default table actor
Table tableR = super.getTable();
Table table = new Table( super.getSkin() );
table.row();
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
TextButton letter = new TextButton( data[i][j], getSkin());
letter.getLabel().setTouchable(Touchable.disabled);
Gdx.app.log( WordSearch1.LOG, " -- Grid Letter: " + letter.getText() );
letter.addListener( new DefaultActorListener() {
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button )
{
//Gdx.app.log( WordSearch1.LOG, " -- Touch X: " + x + " Y: " + y );
//super.touchUp( event, x, y, pointer, button );
//TextButton b = (TextButton) event.getListenerActor();
//Gdx.app.log( WordSearch1.LOG, " -- Touch Name: " + b.getText() );
//game.getSoundManager().play( TyrianSound.CLICK );
//game.setScreen( new StartGameScreen( game ) );
}
} );
table.add( letter ).size( 30, 30 ).spaceBottom( 0 );
}
table.row();
}
table.row();
table.setColor(0, 0, 0, 0);
table.addAction(sequence( delay(1.0f), fadeIn( 1.50f )));
//table.debug();
tableR.row();
tableR.add(startGameButton);
tableR.row();
tableR.row();
tableR.add(table); //****Display nicely but input does not work.
tableR.addActor(table); //**** Displays only 1/4th of table
tableR.row();
table.addListener(new InputListener() {
ArrayList<Integer> hashList= new ArrayList<Integer>();
float scX , scY ; //Start CenterX, CenterY
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("----------------------------------------");
System.out.println("2- DOWN - Pressed - X : " + x + " Y: " + y);
System.out.println("2- DOWN - HIT : " + stage.hit(x, y, true) );
hashList.clear();
float sX = x; //Start X
float sY = y; //Start Y
if (stage.hit(sX, sY, true) != null) {
scX = stage.hit(sX, sY, true).getCenterX();
scY = stage.hit(sX, sY, true).getCenterY();
System.out.println("2- DOWN---- DRAG START Center - X : " + scX + " Y: " + scY);
System.out.println("2- DOWN---- DRAG START Center - HIT : " + stage.hit(scX, scY, true) );
}
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("2- UP - Released - X : " + x + " Y: " + y);
float upX = x;
float upY = y;
float tX = scX; //temp
float tY = scY; //temp
String test = "";
// *** implemented only for Left to Right drag ****
while (tX <= upX) {
//System.out.println("2- While : " + test);
if (stage.hit(tX, tY, true) != null) {
System.out.println("2- IF : " + test);
TextButton t = (TextButton) stage.hit(tX, tY, true);
test = test + t.getText();
}
tX = tX + 30;
}
System.out.println("2- UP - WORD : " + test);
}
// *** implemented only for Left to Right drag ****
public void touchDragged (InputEvent event, float x, float y, int pointer) {
//System.out.println("2- Dragged - X : " + x + " Y: " + y);
//System.out.println("------- Dragged ------");
if (stage.hit(x, y, true) != null) {
int hash = stage.hit(x, y, true).hashCode();
if (hashList.contains(hash) == false) {
hashList.add(hash);
float cX = stage.hit(x, y, true).getCenterX();
float cY = stage.hit(x, y, true).getCenterY();
TextButton t = (TextButton) stage.hit(cX, cY, true);
System.out.println("2- Dragged - hit : " + stage.hit(x, y, true));
System.out.println("2- Dragged - hit C : " + stage.hit(cX, cY, true));
System.out.println("2- Dragged - Text : " + t.getText());
}
else {
//System.out.println("2- Dragged - Hash MATCHED : ");
}
}
else {
System.out.println("2- Dragged - Cancelled : ");
event.cancel();
}
}
});
}
}
我已经包含了上面的大部分代码。现在的问题是 如果我只使用一张桌子 - tableR 并摆脱桌子,那么 touchdragged 会按预期工作。通过在从左到右的方向选择起点和终点之间的字母,给我正确的行为。 不,我想使用 tableR 作为根表并完成 UI 的其余部分。所以我创建了一个不同的表——“表”(上面的代码是我同时使用这两个表的时候)
现在问题来了
如果我使用 tableR.add(table);带有字母的表正确显示,但输入函数的行为不符合预期。尽管对文本按钮的标签禁用了可触摸性,但它会命中标签。
如果我使用 tableR.addActor(table) 则无法正确显示带有字母的表格。只有表格的 1/4 右上角部分可见,并且与左下角对齐。桌子的其余部分是不可见的。输入对于可见的 1/4 似乎非常有效。
如果有帮助,我可以分享完整的代码。
请帮忙!! 在旁注中,请告诉我 inputListener (table.addListener(new InputListener()) 是否是正确的方法。 我的目标是显示一个字母网格,并像市场上的单词搜索游戏一样具有触摸行为。
【问题讨论】: