繁体   English   中英

如何在多个 box2d 对象层中绘制相同的精灵。?

[英]How to draw same sprite in multiple box2d object layers.?

我正在做一个游戏,这是我第一次......我遇到了一个我无法帮助自己解决的问题。 让我们切入正题,我有一个硬币类,它将硬币吸引到唯一的一个 box2D 主体,但是在该层(我用平铺制作的)中,我有更多的 1 个主体,但硬币只出现在其中的 1 个中,最后一个一。 我希望硬币纹理出现在我为硬币定义的所有对象层中,我还将张贴图片以帮助更好地理解我的问题。硬币的代码和图像如下;

图片: https : //ibb.co/nAoYFq

public class Coin extends Sprite{


protected PlayScreen screen;
private Body body;
private BodyDef bodyDef;
private FixtureDef fixtureDef;
private PolygonShape polygonShape;


public Coin(PlayScreen screen, World world,TiledMap map) {
super(screen.getAtlas().findRegion("Gold"));
this.screen = screen;
this.bodyDef = new BodyDef();
this.fixtureDef = new FixtureDef();
this.polygonShape = new PolygonShape();

TextureRegion coinTexture = new TextureRegion(getTexture(),0,0,64,64);
setBounds(0, 0, 84 / trollVersusZombies.PPM, 84 / trollVersusZombies.PPM);
setRegion(coinTexture);

for(MapObject mapObject: map.getLayers().get(6).getObjects().getByType(RectangleMapObject.class))
{
Rectangle rectangle = ((RectangleMapObject)mapObject).getRectangle();

bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set((rectangle.getX() + rectangle.getWidth() / 2)/ trollVersusZombies.PPM, (rectangle.getY() + rectangle.getHeight() / 2) / trollVersusZombies.PPM);

body = world.createBody(bodyDef);

polygonShape.setAsBox(rectangle.getWidth() / 2 / trollVersusZombies.PPM, rectangle.getHeight() / 2 / trollVersusZombies.PPM);
fixtureDef.shape = polygonShape;

fixtureDef.isSensor = true;
body.createFixture(fixtureDef);




}


}

public void update()
{
setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2);

}
}

供参考 :

在我的主 playscreen 类中,我已经声明并实例化了我的 coin 类以传递相关参数,并且在主 playscreen 类的 update 方法中调用了 coin 类的 update 方法,也在 playscreen 类的 render 方法中调用了 coin.draw( playscreen.batch),即;

public void update(float dt) {
//Other code...
coin.update();
}


public void render(float delta) {

//Other Code
gameScreen.batch.begin();
coin.draw(gameScreen.batch);
gameScreen.batch.end();
}

问题是您将创建的所有主体都放在 1 个变量中。 结果只有最后创建的主体存储在该变量中。 相反,您必须创建一个数组来存储每个创建的身体并为每个身体位置绘制一个精灵。 如果您想使用精灵扩展类,您最好为每个主体创建单独的“硬币”对象,但使用相同的纹理区域对象。 更好的方法是在单独的类中而不是在构造函数中创建主体和区域。

示例(这是伪代码):

public class Main {
    private Array<Coin> coins = new Array<>();

    public void create(){
        TextureRegion region = assets.getAtlas().findRegion("Gold");
        for(MapObject mapObject : mapObjects){
            Body body = createBody(mapObject);
            Coin coin = new Coin(region, body)
            coins.add(coin);
        }
    }

    public void render(SpriteBatch batch){
        for(Coin coin : coins){
            coin.draw(batch);
        }
    }

    public Body createBody(MapObject mapObject){
        // here create body using map object
    }
}

public class Coin extends Sprite {

    private Body body;

    public Coin(TextureRegion region, Body body){
        super(region);
        this.body = body;
    }

    public void update(){
    // here update sprite position using body coordinates
    }
}

希望能帮助到你! )

暂无
暂无

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

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