libGDX:创建用于棋盘游戏的网格

6
我正在使用libGDX创建一个简单的棋盘游戏。为了让您大致了解我的想法,可以想象一下Bejeweled(虽然我的游戏当然不会那么复杂)。
游戏包含一个由方块组成的棋盘。根据不同的级别,这个网格有不同数量的方块,例如6x6或8x8。我还想在交换两个相邻方块的位置时包含一些漂亮的动画效果(就像Bejeweled中的效果)。当然,屏幕上也需要一些按钮。
我的问题是:最佳方法是什么?我应该使用舞台和表格来构建网格吗?我还能轻松地使用通用缓动引擎进行动画吗?还是最好逐个绘制Sprites?还是有其他完全不同的方法来处理这个问题?
谢谢您的回答,
Tony
2个回答

7
Sprite square = new Sprite(new Texture("texture"));

渲染

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();

这应该输出一个纹理网格,但未经测试。

如果您想创建平滑的动画效果,您一定要研究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); 
    }
 }

渲染器
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);
     }
 }

接下来绘制舞台,注意停止当前正在运行的任何批处理,因为舞台有自己的batch.begin()和batch.end()。在stage.draw()之后,您可以重新开始批处理。

 stage.act(delta);
 stage.draw();

好的,所以您不使用“舞台(stage)”。但是,如果没有“舞台”,我如何添加按钮呢?或者说,是否可以只覆盖屏幕的某些部分而拥有“舞台”呢? - tomet
谢谢你的努力。我想要一个Sprite网格和一些按钮在下面。所以我需要以某种方式渲染它们两个。 - tomet
@fritzi2000 我认为你问题的一部分已经在你的另一个问题中得到了回答,因为你现在(希望如此)知道如何使用相机、舞台和精灵批处理。如果不是,请在你的其他问题中写下评论。 @thetheodor 你的答案似乎是正确的,但我不会使用Gdx.graphics.getWidth(),Gdx.graphics.getHeight()作为视口。如果你这样做,你必须用像素来做所有的事情,并且你依赖于窗口大小。如果你使用默认的固定值,你就有了自己的世界坐标,它们会被放大以适应游戏窗口。 - Robert P
没问题。拥有a)分辨率无关的代码和b)您可以拥有自己的单位(例如米),并且可以用米而不是像素计算事物。在我看来,这样做会使事情变得更加容易 :) - Robert P
@thetheodor,我看到你使用了camera.viewportHeight/Width。camera.viewPortWidth是当前视口的宽度。你应该将其设置为你想要的值,例如80,以便在游戏窗口中从0到80获得x坐标,其中0是左边界,80是右边界。希望你明白了(: - Robert P
显示剩余2条评论

0

如果你想要一个网格,你可以并且应该使用相机:

OrthographicCamera cam = new OrthographicCamera(8,8);

你要告诉相机拥有8 x和8 y的视口。 相机(0,0)点位于屏幕中央。
为了使其位于左下角,您需要将其位置设置为。
cam.translate(camWidth / 2, camHeight / 2); 

现在,您可以使用sqare.setX(0)在底部行上添加您的正方形,或者使用sqare.setY(3)将其添加到从左到右的第四行。对于动画,您还可以使用Actions,它允许您将不同的移动添加到演员中并使其随着时间执行它们。例如:
actor.addAction(Actions.parallel(Actions.moveTo(float x, float y, float duration), Actions.rotateTo(float rotation, float duration)));

使用这个代码示例,您可以告诉您的角色在duration秒内从他的位置移动到(x,y),并且当他移动到该位置时,他会在duration秒内从当前旋转角度旋转到rotation。 希望这可以帮助到您。

没有一个名为setPosition的方法,它被命名为translate并且执行相同的操作。此外,您可以调用setToOrtho()而不是手动将相机设置为0, 0。 - thetheodor
摄像机的位置默认为0/0,据我所知。这实际上只是伪代码,让人们知道它的工作原理。我不记得确切的方法名称,Eclipse帮了我很多:P感谢您的评论,我会编辑我的答案! - Robert P
Eclipse确实帮了很多忙,必须在那里测试代码才能发布!:D - thetheodor
哈哈,不错 :P 我给你的回答点了赞,但请看一下我在那里的评论。 - Robert P

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接