L
L
lidiya112018-02-17 21:56:53
Java
lidiya11, 2018-02-17 21:56:53

How to mirror a texture in libgdx?

I can't get past the sb.draw(Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY) method. I need to mirror the texture, keeping its size and not cropping anything. In boolean flipX I write true and everything is mirrored, but in int srcX, int srcY, int srcWidth, int srcHeight I can’t figure out what to enter so that the picture is not distorted. Here is the class of the game screen where rendering takes place and the class of one of the enemies, in fact, some of them need to be reflected. I attach the "enemy" texture, maybe something is wrong with it, because it is not rectangular like a photo, but with transparency. In general, what to write in a method? 0,0, texture width and height - not suitable. Or maybe it's better to implement it in some other way?
public class GameState extends State {
private Texture background;
public player player;
public Enemy enemy;
long lastDropTime;
EnemyManager enemyManager;
boolean q = false;
public GameState(GameStateManager gsm) {
super(gsm);
enemyManager = new EnemyManager();
camera.setToOrtho(false,MiniFish.WIGHT,MiniFish.HEIGHT);
background = new Texture("bg.jpg");
player = new Player(100,100);
}
@Override
protected void handleInput() {
if (Gdx.input.justTouched()){
player.move();
}
}
@Override
public void update(float dt) {
handleInput();
player update(dt);
camera update();
}
@Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(camera.combined);
sb.begin();
sb.draw(background,0,0, MiniFish.WIGHT,MiniFish.HEIGHT);
sb.draw(player.getImg(), player.getPos().x- player.X_IMG/2, player.getPos().y- player.Y_IMG/2, player.X_IMG, player.Y_IMG);
if (TimeUtils.nanoTime()-lastDropTime > 1000000000){
lastDropTime = TimeUtils.nanoTime();
int t = MathUtils.random(1, 2);
enemyManager.bornEnemy();
}
for (Enemy e: enemyManager.enemiesToRight){
sb.draw(e.getTexture(),e.position.xe.getSizeX(),e.position.y, e.getSizeX(),e.getSizeY());
}
enemyManager.moveEnemyRight();
for (Enemy e: enemyManager.enemiesToLeft){
//THIS IS A PROBLEM
sb.draw(e.getTexture(),e.position.xe.getSizeX(),e.position.y, e.getSizeX(),e.getSizeY( ),0,0,e.getSizeX(),e.getSizeY(),true,false);
}
enemyManager.moveEnemyLeft();
sb end();
}
@Override
public void dispose() {
background.dispose();
}
}
public class Enemy1 extends Enemy {
public static final int X_IMG1 = 80;
public static final int Y_IMG1 = 30;
public static final int X_IMG2 = 160;
public static final int Y_IMG2 = 60;
public static final int X_IMG3 = 270;
public static final int Y_IMG3 = 90;
private int sizeX;
private int sizeY;
Texture enemyImg;
public Vector3 position;
int Screen_height;
public TextureRegion region;
public Enemy1(float x, float y) {
super(x, y);
enemyImg = new Texture("enemyone.png");
position = new Vector3(x,y,0);
super.setTexture(enemyImg);
Screen_height=Gdx.graphics.getHeight();
int s = MathUtils.random(1, 3);
switch (s){
case 1: sizeX = X_IMG1; sizeY = Y_IMG1; break;
case 2: sizeX = X_IMG2; sizeY = Y_IMG2; break;
case 3: sizeX = X_IMG3; sizeY = Y_IMG3; break;
}
region = new TextureRegion(enemyImg,x,y,sizeX,sizeY);
super.region = region;
}
public Texture getImg() {
return enemyImg;
}
public int getSizeX(){
return sizeX;
}
public int getSizeY(){
return sizeY;
}
public Vector3 getPos(){
return position;
}
public void setPos(Vector3 position){
this.position = position;
}
}
5a887a5862ce7253597753.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
exenza, 2018-02-18
@lidiya11

Here is the code I have reflects your texture from left to right:

sb.begin();
Texture unnamed = new Texture("unnamed.png");
int width = unnamed.getWidth();
int height = unnamed.getHeight();
sb.draw(unnamed, 350, 350, width, height, 0, 0, width, height, true, false);
sb.end();

But I would prefer a sprite:
sb.begin();
Texture unnamed = new Texture("unnamed.png");
Sprite sprite = new Sprite(unnamed);
sprite.flip(true, false);
sprite.setCenter(700, 600);
sprite.draw(sb);
sb.end();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question