所以我尝试了几种不同的方法将精灵放到我的硬币身上,但无济于事。我最初只是尝试在上面放一个纹理,但那不起作用。然后我尝试使用纹理包装器和图册来查看是否有帮助。这是我的代码:
public class Coin extends Item {
public Coin(PlayScreen screen, float x, float y) {
super(screen, x, y);
setRegion(screen.getAtlas().findRegion("Coin"), 0, 0, 15, 16);
}
@Override
public void defineItem() {
BodyDef bdef = new BodyDef();
bdef.position.set(getX(), getY());
bdef.type = BodyDef.BodyType.DynamicBody;
body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(10/Racing.PPM);
fdef.shape = shape;
Filter filter = new Filter();
filter.categoryBits = Racing.COIN_BIT;
Fixture fixture = body.createFixture(fdef);
fixture.setFilterData(filter);
fixture.setUserData(this);
}
public void update(float dt){
super.update(dt);
setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2);
}
}
它扩展了这个类:
public abstract class Item extends Sprite{
protected PlayScreen screen;
protected World world;
protected Vector2 velocity;
protected boolean toDestroy;
protected boolean destroyed;
protected Body body;
public Item(PlayScreen screen, float x, float y){
this.screen = screen;
this.world = screen.getWorld();
toDestroy = false;
destroyed = false;
setPosition(x, y);
setBounds(getX(), getY(), 16 / Racing.PPM, 16 / Racing.PPM);
defineItem();
}
public abstract void defineItem();
public abstract void collect(Kart kart);
public void update(float dt){
if(toDestroy && !destroyed){
world.destroyBody(body);
destroyed = true;
}
}
public void draw(Batch batch){
if(!destroyed){
super.draw(batch);
}
}
public void destroy(){
toDestroy = true;
}
}
任何帮助将不胜感激!谢谢!