LibGDX - Textures rendering at wrong position

Posted by ACluelessGuy on Game Development See other posts from Game Development or by ACluelessGuy
Published on 2014-07-20T10:42:49Z Indexed on 2014/08/19 22:35 UTC
Read the original article Hit count: 184

Filed under:
|
|

Update 2:

Let me further explain my problem since I think that i didn't make it clear enough: The Y-coordinates on the bottom of my screen should be 0. Instead it is the height of my screen. That means the "higher" i touch/click the screen the less my y-coordinate gets. Above that the origin is not inside my screen, atleast not the 0 y-coordinate.

Original post:

I'm currently developing a tower defence game for fun by using LibGDX. There are places on my map where the player is or is not allowed to put towers on. So I created different ArrayLists holding rectangles representing a tile on my map. (towerPositions)

for(int i = 0; i < map.getLayers().getCount(); i++) {
            curLay = (TiledMapTileLayer) map.getLayers().get(i);

            //For all Cells of current Layer
            for(int k = 0; k < curLay.getWidth(); k++) {
                for(int j = 0; j < curLay.getHeight(); j++) {
                    curCell = curLay.getCell(k, j);

                 //If there is a actual cell
                if(curCell != null) {
                    tileWidth = curLay.getTileWidth();
                    tileHeight = curLay.getTileHeight();
                    xTileKoord = tileWidth*k;
                    yTileKoord = tileHeight*j;

                    switch(curLay.getName()) {

                     //If layer named "TowersAllowed" picked
                    case "TowersAllowed":
                        towerPositions.add(new Rectangle(xTileKoord, yTileKoord,  tileWidth, tileHeight));

// ... AND SO ON

If the player clicks on a "allowed" field later on he has the opportunity to build a tower of his coice via a menu. Now here is the problem: The towers render, but they render at wrong position. (They appear really random on the map, no certain pattern for me)

for(Rectangle curRect : towerPositions) {
            if(curRect.contains(xCoord, yCoord)) {

                //Using a certain tower in this example (left the menu out
                if(gameControl.createTower("towerXY")) {

                    //RenderObject is just a class holding the Texture and x/y coordinates
                    renderList.add(new RenderObject(new Texture(Gdx.files.internal("TowerXY.png")), curRect.x, curRect.y));
                }
            }
        }

Later on i render it:

game.batch.begin();
    for(int i = 0; i < renderList.size() ; i++) {
        game.batch.draw(renderList.get(i).myTexture, renderList.get(i).x, renderList.get(i).y);
    }
game.batch.end();

regards

© Game Development or respective owner

Related posts about java

Related posts about textures