简体   繁体   English

你如何移动 LibGDX 中的形状?

[英]How do you move shapes in LibGDX?

I am currently trying to move a shape using an arrow key press in LibGDX. This is what I have so far:我目前正在尝试使用 LibGDX 中的箭头键移动形状。这就是我目前所拥有的:

    shapeRenderer.setColor(Color.BLACK);
    shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
    shapeRenderer.rect(250,250,20,20);
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
        shapeRenderer.rect(200,250,20,20);

    }
    shapeRenderer.end()

However it seems to only create a new shape, as opposed to move my old one.然而,它似乎只是创造了一个新的形状,而不是移动我的旧形状。 Is there a way to move the old one with shapes, or should I be using another class?有没有办法用形状移动旧的,或者我应该使用另一个 class?

ShapeRenderer only renders points, lines, "shapes" that you tell it to draw. ShapeRenderer仅渲染您告诉它绘制的点,线,“形状”。 It doesn't actually store/create shapes itself. 它实际上并不存储/创建形状。 Your code renders a rectangle every time, and if the Left key is pressed will simply draw a 2nd rectangle at slightly different coords. 你的代码每次渲染一个矩形,如果按下左键,只需在略微不同的坐标上绘制第二个矩形。

If you simply want to use shapes, look at the libGDX Rectangle class itself (there are also Circle, Ellipse, Polygon classes). 如果您只想使用形状,请查看libGDX Rectangle类本身(还有Circle,Ellipse,Polygon类)。 You can create a rectangle (assign x/y coords, width/height, etc) and then use the shape renderer to draw that rectangle based on the rectangle's x/y coords (or width/height/etc). 您可以创建一个矩形(指定x / y坐标,宽度/高度等),然后使用形状渲染器根据矩形的x / y坐标(或宽度/高度/等)绘制该矩形。

That's a simplistic solution, but may be all you need 这是一个简单的解决方案,但可能就是您所需要的

You'll need to clear the screen between the two render calls. 您需要在两个渲染调用之间清除屏幕。 This can be done with the command: 这可以使用以下命令完成:

Gdx.gl.glClearColor( 1, 0, 0, 1 );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );

(from the libgdx wiki). (来自libgdx wiki)。

In an actual game, this is generally done in the render method, before anything is drawn. 在实际游戏中,这通常在绘制任何内容之前在渲染方法中完成。

It is important to note that the 'shape' you have created cannot be modified at all, and most programs will shape objects (such as Rectangle) to track attributes, and then use this data in a render method. 重要的是要注意,您创建的“形状”根本无法修改,并且大多数程序将对对象(如Rectangle)进行整形以跟踪属性,然后在渲染方法中使用此数据。 I would recommend working your way through the 'a simple game' tutorial on the libgdx wiki, it shows how a number of common game-related tasks are accomplished using libGDX. 我建议你通过libgdx wiki上的'一个简单的游戏'教程,它展示了如何使用libGDX完成一些常见的游戏相关任务。 Link to tutorial. 链接到教程。

It's a very bad option, but it does what you want这是一个非常糟糕的选择,但它可以满足您的需求

    ScreenUtils.clear(0, 0, 0.2f, 1);
    //camera.update();
    shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
    shapeRenderer.setColor(Color.GOLD);
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
        x = x + -1;
        shapeRenderer.rect(x, y, 20, 20);
    }
    if (Gdx.input.isKeyPressed(Keys.RIGHT)){
        x = x + 1;
        shapeRenderer.rect(x, y, 20, 20);
    }
    a = x;
    b = y;
    shapeRenderer.rect(a, y, 20, 20);
    shapeRenderer.end();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM