<bdo id='NoFw2'></bdo><ul id='NoFw2'></ul>

  1. <legend id='NoFw2'><style id='NoFw2'><dir id='NoFw2'><q id='NoFw2'></q></dir></style></legend>
  2. <small id='NoFw2'></small><noframes id='NoFw2'>

    1. <i id='NoFw2'><tr id='NoFw2'><dt id='NoFw2'><q id='NoFw2'><span id='NoFw2'><b id='NoFw2'><form id='NoFw2'><ins id='NoFw2'></ins><ul id='NoFw2'></ul><sub id='NoFw2'></sub></form><legend id='NoFw2'></legend><bdo id='NoFw2'><pre id='NoFw2'><center id='NoFw2'></center></pre></bdo></b><th id='NoFw2'></th></span></q></dt></tr></i><div id='NoFw2'><tfoot id='NoFw2'></tfoot><dl id='NoFw2'><fieldset id='NoFw2'></fieldset></dl></div>
      <tfoot id='NoFw2'></tfoot>
    2. 如何让玩家通过相机被摧毁?

      时间:2023-05-20
        <bdo id='6rnTS'></bdo><ul id='6rnTS'></ul>
          <legend id='6rnTS'><style id='6rnTS'><dir id='6rnTS'><q id='6rnTS'></q></dir></style></legend>
        • <i id='6rnTS'><tr id='6rnTS'><dt id='6rnTS'><q id='6rnTS'><span id='6rnTS'><b id='6rnTS'><form id='6rnTS'><ins id='6rnTS'></ins><ul id='6rnTS'></ul><sub id='6rnTS'></sub></form><legend id='6rnTS'></legend><bdo id='6rnTS'><pre id='6rnTS'><center id='6rnTS'></center></pre></bdo></b><th id='6rnTS'></th></span></q></dt></tr></i><div id='6rnTS'><tfoot id='6rnTS'></tfoot><dl id='6rnTS'><fieldset id='6rnTS'></fieldset></dl></div>
            <tbody id='6rnTS'></tbody>
          <tfoot id='6rnTS'></tfoot>

          <small id='6rnTS'></small><noframes id='6rnTS'>

              • 本文介绍了如何让玩家通过相机被摧毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我在让玩家通过相机被摧毁时遇到了一些麻烦.在我的应用程序中,我让相机跟随玩家(球).但摄像机只能跟随球向上.所以我想要完成的是,当玩家(球)到达界面(屏幕)的底部时,它会被破坏.在它被摧毁之后会很好,如果一个新的活动(新屏幕)弹出,那就是游戏结束".非常感谢您的大力支持.应用界面

                I've been having some trouble making the player get destroyed through the camera. In my application, I made the camera follow the player(the ball). But the camera can only follow the ball upward. So what I want to accomplish is, when the player(the ball) reaches the bottom of the interface(the screen) it gets destroyed. After it gets destroyed it would be good, if a new activity(new screen) pops up, that says "Game over". Thanks a lot for the great support. the interface of the application

                包 com.luca.tuninga;

                package com.luca.tuninga;

                import com.badlogic.gdx.ApplicationAdapter;
                import com.badlogic.gdx.Gdx;
                import com.badlogic.gdx.graphics.GL20;
                import com.badlogic.gdx.graphics.Texture;
                import com.badlogic.gdx.graphics.OrthographicCamera;
                import com.badlogic.gdx.Input;
                import com.badlogic.gdx.math.MathUtils;
                import com.badlogic.gdx.math.Vector2;
                import com.badlogic.gdx.physics.box2d.*;
                
                
                
                public class MyGdxGame extends ApplicationAdapter {
                
                
                public static float APP_FPS = 60f;
                public static int V_WIDTH = 480;
                public static int V_HEIGHT = 640;
                
                
                Box2DDebugRenderer b2dr;
                World world;
                Body ballBody;
                
                OrthographicCamera camera;
                
                float cameraMaxY;
                
                @Override
                public void create() {
                    world = new World(new Vector2(0, -9.8f), false);
                    b2dr = new Box2DDebugRenderer();
                
                
                    camera = new OrthographicCamera();
                    camera.setToOrtho(false, V_WIDTH, V_HEIGHT);
                    cameraMaxY = camera.position.y;
                
                
                    ballBody = createBall();
                    createWalls();
                }
                
                private void update() {
                    world.step(1f / APP_FPS, 6, 2);
                
                    if (Gdx.input.isTouched()) {
                        ballBody.setLinearVelocity(0, MathUtils.clamp(ballBody.getLinearVelocity().y, 0, 3));
                        ballBody.applyForceToCenter(new Vector2(0, 650f), false);
                    }
                
                    if (ballBody.getPosition().y * 32 > cameraMaxY) {
                        camera.translate(0, (ballBody.getPosition().y * 32) - cameraMaxY);
                        camera.update();
                
                        cameraMaxY = camera.position.y;
                    }
                }
                
                @Override
                public void render() {
                    Gdx.gl.glClearColor(.25f, .25f, .25f, 1);
                    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
                
                    update();
                
                    b2dr.render(world, camera.combined.cpy().scl(32f));
                }
                
                @Override
                public void dispose() {
                    super.dispose();
                    world.dispose();
                }
                
                private Body createBall() {
                    Body body;
                    BodyDef def = new BodyDef();
                    def.type = BodyDef.BodyType.DynamicBody;
                    def.fixedRotation = true;
                    def.position.set(camera.position.x/ 32 + .5f, camera.position.y/ 32);
                    def.gravityScale = 3;
                    CircleShape shape = new CircleShape();
                    shape.setRadius(.5f);
                
                    body = world.createBody(def);
                    body.createFixture(shape, 1.0f);
                    return body;
                
                }
                
                private void createWalls() {
                    Body body;
                    BodyDef def = new BodyDef();
                    def.type = BodyDef.BodyType.StaticBody;
                    def.fixedRotation = true;
                
                    PolygonShape shape = new PolygonShape();
                    shape.setAsBox(1, 200 / 32);
                    for(int i = 0; i < 20 ; i++) {
                        def.position.set(1.01f, i * (200 / 32));
                        body = world.createBody(def);
                        body.createFixture(shape, 1.0f);
                
                        def.position.set(V_WIDTH / 32 - 1, i * (200 / 32));
                        body = world.createBody(def);
                        body.createFixture(shape, 1.0f);
                    }
                 }
                
                }
                

                推荐答案

                据我了解,相机只会向上跟随.因此,当球向上过渡时,相机会更新并跟随,相机不会再次下降.

                From what I understand, the camera will follow upwards only. Thus when the ball transitions into upwards position, camera will update and follow through, camera won't go down again.

                这样您就可以检查球何时不在摄像机的视线范围内;特别是相机的底部.

                So you can check when the ball is outside of the sight of camera; specifically the bottom of camera.

                你可以把它放在 update() 函数的末尾

                You can do something like this putting it at the end of update() function

                if ((ballBody.getPosition().y + 0.5f) * 32 < ballBody.getPosition().y -
                    camera.getViewportHeight()/2) {
                    // destroy body
                    world.destroyBody(ballBody);
                
                    // TODO: switch to another screen (thus next frame update & draw loop of this screen won't be called anymore)
                }
                

                上面是检查球何时完全不在相机的视线范围内(因此我做了 + 0.5f 这是它的半径,因为你用来创建这样的形状body) 相对于相机的视口高度.

                above is to check if when ball is completely out of camera's sight (thus I do + 0.5f which is its radius as you used to create the shape for such body) against camera's viewport height.

                然后切换到另一个屏幕.这意味着当前屏幕将不再更新或绘制其内容,因此无需检查标志.但是如果下一帧还需要做其他的事情,最好有一个flag检查当前画面,知道游戏已经结束,这样就可以判断是否做某些操作了.

                Then switch to another screen. This means the current screen won't be update or draw its content anymore, thus no need to have a flag to check. But if you need to do something else again in next frame, you better have a flag checking for the current screen to know that the game is now over, and thus you can check whether to do certain operations.

                这篇关于如何让玩家通过相机被摧毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Libgdx 相机/缩放问题 下一篇:如何在启动 Android LibGDX 项目时修复 NoClassDefFoun

                相关文章

                最新文章

                • <bdo id='aLX2p'></bdo><ul id='aLX2p'></ul>
                <legend id='aLX2p'><style id='aLX2p'><dir id='aLX2p'><q id='aLX2p'></q></dir></style></legend>

                  1. <i id='aLX2p'><tr id='aLX2p'><dt id='aLX2p'><q id='aLX2p'><span id='aLX2p'><b id='aLX2p'><form id='aLX2p'><ins id='aLX2p'></ins><ul id='aLX2p'></ul><sub id='aLX2p'></sub></form><legend id='aLX2p'></legend><bdo id='aLX2p'><pre id='aLX2p'><center id='aLX2p'></center></pre></bdo></b><th id='aLX2p'></th></span></q></dt></tr></i><div id='aLX2p'><tfoot id='aLX2p'></tfoot><dl id='aLX2p'><fieldset id='aLX2p'></fieldset></dl></div>

                    <small id='aLX2p'></small><noframes id='aLX2p'>

                  2. <tfoot id='aLX2p'></tfoot>