在Libgdx中的TiledMap顶部渲染Image actor

时间:2013-08-18 08:56:41

标签: libgdx collision-detection actor tile

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

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

我创建了backgroundTiledMap类,将Actor类扩展到它。 在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);
                           }


                    }

新的actor类,这是演员在屏幕上移动,检测与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);

                        }


                    }

2 个答案:

答案 0 :(得分:0)

要在tilemap上方绘制actor,您必须在渲染tilemap后调用stage.draw

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

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

答案 1 :(得分:0)

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

如果actor使用其他一些方法进行绘制而不是批处理,则应该结束批处理,然后在方法结束时再次重新启动。所以在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

相关问题