[英]LibGdx Why does sprite draw() method change my X and Y
在過去的幾個小時中,我一直在調試我的LibGdx游戲,試圖弄清楚為什么有些精靈會四處移動而有些卻沒有。 我基本上有一個LittleMan類,它是player類,當我移動播放器時,sprite會順暢地移動。 然后我還有另一個子彈類,代表子彈,但我拍攝的子彈無法正確渲染。 我嘗試從玩家的位置開始繪制它們,但它總是從地圖上的另一個固定點繪制它們。 (播放器的起始位置)
現在來了奇怪的部分; 我調試了我的應用程序,以嘗試了解播放器的運動來自何處,奇怪的是,每次我調用super.draw(batch)時,X和Y坐標都會發生變化。 我不知道這種情況如何發生或為什么發生。 很好,因為它可以工作,但是我想知道它是如何工作的,所以我可以將其用於子彈。
這是一些代碼來澄清:
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:
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);
}
使用以下代碼創建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);
LittleMan課程是由一位同學制作的,但是他也無法向我解釋它是如何工作的,因此我嘗試了盡我所能來復制它,但是我真的不知道該怎么做:
使子彈從正確的位置渲染
為什么在地球上draw()方法會更改x和y坐標
誰能幫助我並解釋發生了什么或我做錯了什么?
非常感謝所有幫助!
- 使子彈從正確的位置渲染
什么是“正確的地方”? 您將其明確設置為小矮人的位置:
float x = littleMan.b2body.getPosition().x;
float y = littleMan.b2body.getPosition().y;
我嘗試從玩家的位置開始繪制它們,但它總是從地圖上的另一個固定點繪制它們。 (播放器的起始位置)
您希望它們位於播放器位置上,但它們在播放器位置上呈現嗎?
- 為什么在地球上draw()方法會更改x和y坐標
在更新方法中,將位置設置為世界位置。 要回答這個問題,您必須檢查對象的世界坐標是否改變。 我猜發生了以下情況:您在同一位置創建了兩個對象(子彈頭和小矮人),它們在世界中碰撞並被推開。 因此,它們緩慢地彼此漂移。
也許解決方案是使用沖撞位或沖撞組過濾掉小矮人和他的子彈之間的沖撞。
我認為轉換時有問題(像素到米)
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);
子彈的update()
應該是
public void update(float dt){
setPosition(b2body.getPosition().x*FacultyWars.PPM - getWidth()/2, b2body.getPosition().y*FacultyWars.PPM - getHeight()/2);
}
同樣,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.