3

我使用 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);

                        }


                    }
4

2 回答 2

0

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

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

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

于 2013-08-19T05:16:18.863 回答
0

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

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

于 2015-03-21T18:43:12.067 回答