Android SetTexture和Sprite未按预期工作!(LibGDX)
我在互联网上到处搜索,还没有找到答案。我有一个叫做GameIcon的对象,它扩展了Sprite。除了质地之外,一切都很好。这是我的GameIcon类代码Android SetTexture和Sprite未按预期工作!(LibGDX),android,libgdx,textures,sprite,Android,Libgdx,Textures,Sprite,我在互联网上到处搜索,还没有找到答案。我有一个叫做GameIcon的对象,它扩展了Sprite。除了质地之外,一切都很好。这是我的GameIcon类代码 package com.xx4everPixelatedxx.gaterunner.sprites; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; impor
package com.xx4everPixelatedxx.gaterunner.sprites;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.xx4everPixelatedxx.gaterunner.GateRunner;
import javax.xml.soap.Text;
/**
* Created by Michael Jan on 8/17/2015.
*/
public class GameIcon extends Sprite {
private int vX = 3;
private int vY = 3;
private int r = 9;
private int rotation;
private Vector3 position;
private Texture texture;
public GameIcon(int x, int y) {
position = new Vector3(x, y, 0);
texture = new Texture(Gdx.files.internal("icon_players/icon1.png"));
setTexture(texture);
}
public void update() {
position.add(vX, vY, 0);
rotation = rotation + r;
rotation = rotation % 360;
setRotation(rotation);
setOriginCenter();
}
public void addPosition(int x, int y) {
position.add(x, y, 0);
setOriginCenter();
}
public void negateVelocityX() {
vX = -vX;
}
public void negateRotation() {
r = -r;
}
public Vector3 getPosition() {
return position;
}
public int getvY() {
return vY;
}
public void setvY(int vY) {
this.vY = vY;
}
public int getvX() {
return vX;
}
public void setvX(int vX) {
this.vX = vX;
}
}
以下是icon1.png:
以下是我得到的:
请注意,图像和粒子的旋转是有意的。问题是大正方形应该是纹理(icon1.png),我不知道如何解决这个问题
(声誉不足,无法发布图片)我不熟悉LibGDX,但这可能与您可能正在覆盖
texturereregion.texture
有关。您是否可以尝试像这样使用父类构造函数:
...
public class GameIcon extends Sprite {
private int vX = 3;
private int vY = 3;
private int r = 9;
private int rotation;
private Vector3 position;
//private Texture texture;
public GameIcon(int x, int y) {
super(new Texture(Gdx.files.internal("icon_players/icon1.png")))
position = new Vector3(x, y, 0);
}
...
正如Tenfour04在注释中所述,此方法之所以有效,是因为父级构造函数应用纹理的宽度和高度,而
setTexture()
则不适用。如何渲染它?您可能不应该隐藏超类对位置
、纹理
和旋转的引用。这是一个等待发生的错误。如果您隐藏了Sprite的所有功能,则没有理由将其子类化。@m.antkowicz name.render(batch)@Tenfour04我还在扩展Sprite的过程中。以前,sprite只是一个字段,我不得不一直调用getSprite。你可能不熟悉它,但你的答案很准确。之所以需要这样做,是因为setTexture
方法不应用纹理的宽度和高度,但超级构造函数会应用。谢谢@Tenfour04。你介意我用你的解释更新答案吗?