【问题标题】:Render Image actor on top of TiledMap in Libgdx在 Libgdx 中的 TiledMap 上渲染图像 actor
【发布时间】:2013-08-20 06:48:36
【问题描述】:

我已经使用 Tile 软件创建了 10 个对象图层,并使用 OrthogonalTiledMapRenderer 对象渲染了平铺地图。

                    renderer.setView(camera);
                    renderer.render();

我已经创建了将 Actor 类扩展到它的 backgroundTiledMap 类。 在 mainGame 类的阶段添加 backgroundTiledMap 类的对象。 它的渲染完美。

但是当我尝试将下一个演员添加到同一舞台时。它没有被渲染。

是否有任何不同的方式使用平铺地图和演员来检测它们之间的碰撞。

实际代码下方

backgroundTiledMap.java 类,扩展为actor,用于从平铺地图中绘制背景(在平铺软件中创建)

                public class BackgroundTiledMap extends Actor {

                        public MapProperties properties;
                        public MapLayer layer;
                        public MapObject mapObject;
                        public TiledMapTileLayer tiledLayer;
                        private TiledMap map;
                        private OrthogonalTiledMapRenderer renderer;
                        private OrthographicCamera camera;
                        public Array<Rectangle> tiles = new Array<Rectangle>();

                        private Pool<Rectangle> rectPool = new Pool<Rectangle>() {
                            @Override
                            protected Rectangle newObject () {
                                return new Rectangle();
                            }
                        };

                        GameStartPoint game;
                        Stage  stage;
                        Reptile reptile;

                        public BackgroundTiledMap(GameStartPoint game,Stage stage)
                        {
                               this.game = game;
                               this.stage = stage;
                               init();

                        }


                        public void init()
                        {
                            map = new TmxMapLoader().load("Images/GuideCrocodile.tmx");
                            renderer = new OrthogonalTiledMapRenderer(map, 1 / 16f);

                            camera = new OrthographicCamera();
                            camera.setToOrtho(false, 70, 50);
                            camera.update();
                            stage.setCamera(camera);

                        }

                        @Override
                        public void draw(SpriteBatch batch, float parentAlpha) {
                            super.draw(batch, parentAlpha);
                            render();

                        }

                        public void render()
                        {
                            camera.update();
                            renderer.setView(camera);
                            renderer.render();

                        }




                    }

这是用于创建舞台的mainGame 屏幕,所有其他演员和对象都添加在这里

                    public class GuideCrocodileScreen implements Screen {

                        public GameStartPoint game;
                        private Stage stage;

                        public BackgroundTiledMap backgroundTiledMap;
                        public Reptile reptile;
                        MoveToAction moveAction;

                        public GuideCrocodileScreen(GameStartPoint game)
                        {
                            this.game = game;
                            stage = new Stage();

                        }


                        @Override
                        public void render(float delta) {
                            // clear the screen
                            Gdx.gl.glClearColor(0.7f, 0.7f, 1.0f, 1);
                            Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

                            Gdx.input.setInputProcessor(stage);
                            stage.draw();
                        }


                        private void addReptile(){

                            reptile = new Reptile(stage,100,100,50,50);
                    //        backgroundTiledMap.getTiles(100,100,50,50);

                            reptile.addListener(new InputListener() {
                                public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                                    System.out.println("down");
                                    return true;
                                }

                                public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
                                    System.out.println("up");
                                }
                                public void touchDragged (float x, float y, int pointer) {
                                    reptile.updateReptile(x,y);
                                }

                                public boolean touchMoved (float x, float y) {
                                    //reptile.updateReptile(x,y);
                                    return false;
                                }

                            });
                            stage.addActor(reptile);

                        }



                        @Override
                        public void resize(int width, int height) {

                        }


                        @Override
                        public void show() {

                            addBackgroundArea();
                            addReptile();
                        }




                        private void addBackgroundArea(){
                            backgroundTiledMap = new BackgroundTiledMap(game,stage);
                            stage.addActor(backgroundTiledMap);
                           }


                    }

新的演员类,这是演员在屏幕上移动,检测与在 backgroundTiledMap reptile.java 上绘制的对象的碰撞(这不会被渲染)

                    public class Reptile extends Actor  {

                        public GameStartPoint game;
                        private OrthogonalTiledMapRenderer renderer;
                        public SpriteBatch batch;
                        public int screenWidth;
                        public int screenHeight;
                        private Texture reptiletexture;
                        private TextureRegion reptileRegion;
                        private Stage stage;

                        public Reptile( Stage stage,int x, int y,int width, int height){



                                setX((float)x);
                                setY((float)y);
                                setWidth((float)width);
                                setHeight((float)height);
                                this.setTouchable(Touchable.enabled);
                                 this.stage = stage;
                                initialize();


                        }

                        private void initialize(){

                            this.screenWidth = GameStartPoint.WIDTH;
                            this.screenHeight = GameStartPoint.HEIGHT;
                            reptiletexture = new Texture(Gdx.files.internal("Images/basketball.png"));
                            reptileRegion = new TextureRegion(reptiletexture);
                            this.setVisible(true);

                        }

                        @Override
                        public void draw(SpriteBatch batch,float parentAlpha)
                        {
                            super.draw(batch,parentAlpha);


                           batch.draw(reptileRegion, getX(), getY(), getOriginX(),
                                    getOriginY(), getWidth(), getHeight(), getScaleX(),
                                    getScaleY(), getRotation());



                        }

                        public void updateReptile(float x,float y)
                        {
                            int duration = 5;
                             if(Gdx.input.isTouched())
                             {
                                 MoveToAction action = Actions.action(MoveToAction.class);
                                 action.setPosition(x, y);
                                 action.setDuration(duration);
                                 this.addAction(action);
                             }

                        }



                        @Override
                        public Actor hit(float x, float y, boolean touchable) {
                            //Gdx.app.log("","reptile is touched.. ") ;
                            updateReptile(x,y);
                            return super.hit(x, y, touchable);

                        }


                    }

【问题讨论】:

    标签: libgdx collision-detection actor tile


    【解决方案1】:

    一个老问题,但是当我在谷歌上搜索同样的问题时,这是第一个命中......

    如果演员使用其他方法而不是批处理进行绘制,批处理应该结束,然后在方法结束时再次重新启动。所以在BackgroundTiledMap中:

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        batch.end();
        render();  // Renders the TiledMap using OrthogonalTiledMapRenderer
        batch.begin();
    }
    

    请参阅https://github.com/libgdx/libgdx/wiki/Scene2d#drawing 了解更多信息。

    【讨论】:

      【解决方案2】:

      要在 tilemap 上方绘制一个 actor,您必须在渲染 tilemap 后调用 stage.draw,如下所示:

      @Override
      public void draw(SpriteBatch batch, float parentAlpha) {
          render();
          super.draw(batch, parentAlpha);
      }
      

      对于碰撞部分,您可以在不同的类中获取您在TiledMap 中创建的对象,例如 EllipseMapObject、PolygonMapObject、RectangleMapObject 等,您可以从中提取折线、多边形等,并使用 Intersector 类检查它们的边界。

      【讨论】:

        猜你喜欢
        • 2013-01-23
        • 1970-01-01
        • 2016-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多