SetTexture 和 Sprites 没有按预期工作! (LibGDX)
SetTexture and Sprites not working as intended! (LibGDX)
我在互联网上搜索和广泛搜索,但我还没有找到答案。我有一个名为 GameIcon 的对象,它扩展了 Sprite。关于它的一切都很好,除了质地。这是我的 GameIcon class.
代码
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:
https://i.gyazo.com/1d52f5e58b227f08809f6c14ae4c94a4.png
这是我得到的:
https://i.gyazo.com/1881a9392955af34de5c55b9b8fac391.png
请注意,图像和粒子的旋转是有意的。问题是大方块应该是纹理(icon1.png),我不知道如何解决这个问题。
(没有足够的声誉 post 图片)
我不熟悉LibGDX,但这可能与您可能覆盖TextureRegion.texture
有关。您可以尝试像这样使用您的父级 class Sprite(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()
没有。
我在互联网上搜索和广泛搜索,但我还没有找到答案。我有一个名为 GameIcon 的对象,它扩展了 Sprite。关于它的一切都很好,除了质地。这是我的 GameIcon class.
代码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: https://i.gyazo.com/1d52f5e58b227f08809f6c14ae4c94a4.png
这是我得到的: https://i.gyazo.com/1881a9392955af34de5c55b9b8fac391.png
请注意,图像和粒子的旋转是有意的。问题是大方块应该是纹理(icon1.png),我不知道如何解决这个问题。
(没有足够的声誉 post 图片)
我不熟悉LibGDX,但这可能与您可能覆盖TextureRegion.texture
有关。您可以尝试像这样使用您的父级 class Sprite(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()
没有。