libGDX:为棋盘游戏创建网格

tom*_*met 6 java android game-engine libgdx

我正在尝试使用libGDX创建一个简单的棋盘游戏.只是你对我想要做的事情有一个粗略的想法,想象一下Bejeweled(虽然我当然不那么复杂).

游戏涉及一个单元格为正方形的棋盘.根据级别,此网格具有不同数量的单元格,如6x6或8x8.我还想要包含一些漂亮的动画来切换两个相邻单元格的位置(比如Bejeweled).当然还需要在屏幕上显示一些按钮.

我的问题是:最好的方法是什么?我应该使用一个舞台,然后使用表格作为网格吗?那么我还可以轻松制作动画(使用通用补间引擎)吗?或者单独绘制精灵更好?还是有另一种完全不同的方法来接近这个?

谢谢您的回答,

干杯,

托尼

the*_*dor 5

Sprite square = new Sprite(new Texture("texture"));
Run Code Online (Sandbox Code Playgroud)

给予

float squareWidth = camera.viewportWidth / squaresOnWidth;
float squareHeight = camera.viewportHeight / squaresOnHeight;
square.setWidth(squareWidth);
square.setHeight(squareHeight);
batch.begin(); `
for(int y = 0; y < squaresOnHeight; y++){
    for(int x = 0; x < squaresOnWidth; x++){
        square.setX(x * squareWidth);
        square.setY(y * squareHeight);
        square.draw(batch);
     }
 }
 batch.end();
Run Code Online (Sandbox Code Playgroud)

这应该输出纹理网格,而不是测试.

如果你想创建流畅的动画,你一定要看看UniveralTweenEngine,这里有一个它可以做的演示:http://www.aurelienribon.com/universal-tween-engine/gwt/demo.html

如果你想要按钮中的网格.

OrthoGraphicCamera camera = new OrthoGraphicCamera();
camera.setToOrtho(false, yourViewportWidth, yourViewportHeight);
camera.translate(xPos, yPos);
Stage stage = new Stage(your wanted stage width, your wanted stage height, false, batch);
stage.setCamera(camera); 
for(int y = 0; y < buttonsOnHeight; y++){
    for(int x = 0; x < buttonsOnWidth; x++){
       stage.addActor(new TextButton("" + x + y * buttonsOnWidth, textButtonStyle); 
    }
 }
Run Code Online (Sandbox Code Playgroud)

渲染

float buttonWidth = camera.viewportWidth / buttonsOnWidth;
float buttonHeight = camera.viewportHeight / buttonsOnHeight;
for(int y = 0; y < buttonsOnHeight; y++){
    for(int x = 0; x < buttonsOnWidth; x++){
        TextButton button = stage.getActors().get(x + y * buttonsOnWidth);
        button.setX(x * buttonWidth);
        button.setY(y * buttonHeight);
        button.setWidth(buttonWidth);
        button.setHeight(buttonHeight);
     }
 }
Run Code Online (Sandbox Code Playgroud)

然后绘制舞台,注意你应该停止当前正在运行的任何批处理因为stage拥有它自己的batch.begin()和batch.end().您可以在stage.draw()之后再次启动批处理;

 stage.act(delta);
 stage.draw();
Run Code Online (Sandbox Code Playgroud)