【问题标题】:LibGDX nested table display and inputLibGDX 嵌套表格显示和输入
【发布时间】: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 的其余部分。所以我创建了一个不同的表——“表”(上面的代码是我同时使用这两个表的时候)

现在问题来了

  1. 如果我使用 tableR.add(table);带有字母的表正确显示,但输入函数的行为不符合预期。尽管对文本按钮的标签禁用了可触摸性,但它会命中标签。

  2. 如果我使用 tableR.addActor(table) 则无法正确显示带有字母的表格。只有表格的 1/4 右上角部分可见,并且与左下角对齐。桌子的其余部分是不可见的。输入对于可见的 1/4 似乎非常有效。

如果有帮助,我可以分享完整的代码。

请帮忙!! 在旁注中,请告诉我 inputListener (table.addListener(new InputListener()) 是否是正确的方法。 我的目标是显示一个字母网格,并像市场上的单词搜索游戏一样具有触摸行为。

【问题讨论】:

    标签: android libgdx


    【解决方案1】:

    你还没有给contextproblem statement

    您的示例代码太复杂了。简化问题并发布executable 的示例代码,可以复制、粘贴和运行。

    Table 的可触摸设置为childrenOnly。您可能想使用setTouchable(enabled)。如果您只关心按钮的触摸,那么您不需要这样做(它们也是表格,但已经完成了)。

    您很可能不需要致电stage.hit()。给出了触摸坐标,事件提供了很多信息,包括target。这是事件发起的actor,它可能是按钮或按钮内的actor(例如TextButton 内的Label)。如果您在表格上启用触摸,它可能是表格。

    【讨论】:

    • 我将在今天晚些时候上传项目。谢谢
    • 没有项目,应该是可以复制粘贴运行的单个文件(即有main方法)。
    • 我正在清理我的代码。过几天上传。
    • 以下内容有意义吗?还是只是错了? stageT.addActor(rootTable); rootTable.addActor(gridTable); rootTable.setFillParent(true); gridTable.setFillParent(true); 为两个表设置 setFillParent(true) 似乎可以解决问题。将进行更多测试并确认。但以下帖子建议:只有根表应该填充其父级(阶段)。 stackoverflow.com/questions/23483546/…谢谢
    • 只有根表应该使用setFillParent
    猜你喜欢
    • 2022-11-15
    • 2021-07-31
    • 2012-05-14
    • 2014-02-27
    • 2014-06-22
    • 1970-01-01
    • 2013-03-07
    • 2015-07-12
    • 2021-11-26
    相关资源
    最近更新 更多