2013-08-18 39 views
3

我已經使用Tile軟件創建了10個對象圖層,並使用OrthogonalTiledMapRenderer對象渲染了平鋪的地圖。在Libgdx的TiledMap頂部渲染圖像actor

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

我已經創建了backgroundTiledMap類,它將Actor類擴展到它。 在mainGame類的stage中添加backgroundTiledMap類的對象。 它的渲染完美。

但是當我試圖添加下一個演員到同一個舞臺。它沒有得到渲染。

是否有任何不同的方式使用平鋪地圖和演員來檢測之間的碰撞。

低於實際代碼

backgroundTiledMap.java類擴展爲演員和用於繪製從瓷磚地圖(瓦軟件創建的)背景

   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繪製的對象進行渲染tilemap的這樣

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

後和碰撞圖紙上面tilemap的,你必須調用stage.draw演員檢測碰撞(這是沒有得到渲染)

    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); 

         } 


        } 

回答

0

你可以在不同的類中獲得你創建的對象,例如 EllipseMapObject,PolygonMapObject,RectangleMapObject等。從哪裏可以提取多義線,多邊形等並使用個部門間類來檢查邊界

0

一個老問題,但是這是在我google搜索同樣的問題,先打...

如果演員使用一些其他的方法繪製的,而不是批,批次應該結束,然後在方法結束時重新啓動。所以在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