简体   繁体   English

LibGdx为什么Sprite draw()方法会更改我的X和Y

[英]LibGdx Why does sprite draw() method change my X and Y

I have been debugging my LibGdx game for the last couple of hours trying to figure out why some sprites were moving around and some not. 在过去的几个小时中,我一直在调试我的LibGdx游戏,试图弄清楚为什么有些精灵会四处移动而有些却没有。 I basically have a LittleMan class that is the player class and the sprite moves along nice when i move my player. 我基本上有一个LittleMan类,它是player类,当我移动播放器时,sprite会顺畅地移动。 Then i have another class Bullet that represents bullets but the bullets i shoot don't render correctly. 然后我还有另一个子弹类,代表子弹,但我拍摄的子弹无法正确渲染。 I try to draw them starting from the position of the player but it always draws them from another fixed point on the map. 我尝试从玩家的位置开始绘制它们,但它总是从地图上的另一个固定点绘制它们。 (starting position of the player) (播放器的起始位置)

Now here comes the weird part; 现在来了奇怪的部分; i debugged my app to try to understand where the movement of the player was coming from and strangely the X and Y coords change every time i call super.draw(batch). 我调试了我的应用程序,以尝试了解播放器的运动来自何处,奇怪的是,每次我调用super.draw(batch)时,X和Y坐标都会发生变化。 I don't understand how or why this happens. 我不知道这种情况如何发生或为什么发生。 Its fine because it works but i want to know how it works so i can use it for my bullets. 很好,因为它可以工作,但是我想知道它是如何工作的,所以我可以将其用于子弹。

Here is some code to clarify: 这是一些代码来澄清:

LittleMan.java : LittleMan.java:

private Texture littleMan;
private TextureRegion stand;
public LittleMan(Texture littleMan, World world) {
    super(new Sprite(littleMan));
    speed = 1;
    pv=50;
    stamina = 100;
    power = 1;
    defense = 1;
    this.world = world;
    defineMario();

    this.littleMan = littleMan;
    stand = new TextureRegion(getTexture(), 39, 21);
    setBounds(0,0,39,21);
    setRegion(stand);
}

public void update(float dt){

    setPosition(b2body.getPosition().x - getWidth()/2, b2body.getPosition().y - getHeight()/2);
}

public void defineMario() {
    BodyDef bdef = new BodyDef();
    bdef.position.set(32/ FacultyWars.PPM, 32/ FacultyWars.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(30/ FacultyWars.PPM);

    fdef.shape = shape;
    b2body.createFixture(fdef);
}


@Override
public void draw(Batch batch){
    update(Gdx.graphics.getDeltaTime());
    super.draw(batch); //this is the line the coords change, tested with debugger and getY() and getX()

}

Bullet.java: Bullet.java:

 private int bulletSpd;//speed
private int bulletRng;//range
private int dmg;//damage
//texture est déja dans sprite
private float rotation;
public World world;
public Body b2body;


public Bullet(Sprite sprite, int speed,int range, int dmg,float x, float y ,float rotation, World world){
    super(sprite);
    bulletRng=range;
    bulletSpd=speed;
    this.dmg=dmg;
    this.rotation=rotation;
    this.world = world;
    defineBullet(x,y);

}

public void defineBullet(float x, float y) {
    BodyDef bdef = new BodyDef();
    bdef.position.set(x/ FacultyWars.PPM, y/ FacultyWars.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(30/ FacultyWars.PPM);

    fdef.shape = shape;
    b2body.createFixture(fdef);
}

@Override
public void draw(Batch batch){
    update(Gdx.graphics.getDeltaTime());
    super.draw(batch);
}

public void update(float dt){
    setPosition(b2body.getPosition().x - getWidth()/2, b2body.getPosition().y - getHeight()/2);
}

A Bullet object is created using this code: 使用以下代码创建Bullet对象:

    float rotation = littleMan.getRotation();
    float x =littleMan.b2body.getPosition().x;
    float y =littleMan.b2body.getPosition().y;
    Bullet bullet = new Bullet(new Sprite(new Texture("bullet.png")), 30, 500, 1, x, y, rotation,world);

The LittleMan class was made by a fellow student but he can't explain to me how it works either so i tried copying it the best i could but i really cannot figure out how: LittleMan课程是由一位同学制作的,但是他也无法向我解释它是如何工作的,因此我尝试了尽我所能来复制它,但是我真的不知道该怎么做:

  1. To make the bullets render from the correct place 使子弹从正确的位置渲染

  2. Why on earth the draw() method changes the x and y coords 为什么在地球上draw()方法会更改x和y坐标

Can anyone help me and explain what is going on or what i am doing wrong? 谁能帮助我并解释发生了什么或我做错了什么?

All help is greatly appreciated! 非常感谢所有帮助!

  1. To make the bullets render from the correct place 使子弹从正确的位置渲染

What is the "correct place"? 什么是“正确的地方”? You explicitly set it to littleman's position: 您将其明确设置为小矮人的位置:

float x = littleMan.b2body.getPosition().x;
float y = littleMan.b2body.getPosition().y;

I try to draw them starting from the position of the player but it always draws them from another fixed point on the map. 我尝试从玩家的位置开始绘制它们,但它总是从地图上的另一个固定点绘制它们。 (starting position of the player) (播放器的起始位置)

You want them on the players position but they are rendered on the players position? 您希望它们位于播放器位置上,但它们在播放器位置上呈现吗?

  1. Why on earth the draw() method changes the x and y coords 为什么在地球上draw()方法会更改x和y坐标

In your update method, you set the position to the world position. 在更新方法中,将位置设置为世界位置。 To answer that question you have to check if the world coordinates of your object alters. 要回答这个问题,您必须检查对象的世界坐标是否改变。 I guess that the following happens: You create two objects at the same place (bullet and littleman), they collide in the world and are pushed apart. 我猜发生了以下情况:您在同一位置创建了两个对象(子弹头和小矮人),它们在世界中碰撞并被推开。 Because of this they slowly drift away from each other. 因此,它们缓慢地彼此漂移。

Maybe the solution would be to filter out collisions between your littleman and his bullets using collisionBits or collisionGroups. 也许解决方案是使用冲撞位或冲撞组过滤掉小矮人和他的子弹之间的冲撞。

I think there is problem in conversion (Pixel to meter) 我认为转换时有问题(像素到米)

float x = littleMan.b2body.getPosition().x*FacultyWars.PPM;
float y = littleMan.b2body.getPosition().y*FacultyWars.PPM;

Bullet bullet = new Bullet(new Sprite(new Texture("bullet.png")), 30, 500, 1, x, y, rotation,world);

And update() of Bullet should be 子弹的update()应该是

public void update(float dt){
    setPosition(b2body.getPosition().x*FacultyWars.PPM - getWidth()/2, b2body.getPosition().y*FacultyWars.PPM - getHeight()/2);
}

And similarly update() of LittleMan should be 同样,LittleMan的update()应该是

public void update(float dt){

    setPosition(b2body.getPosition().x*FacultyWars.PPM - getWidth()/2, b2body.getPosition().y*FacultyWars.PPM - getHeight()/2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM