如何在多个 box2d 对象图层中绘制相同的精灵。?
How to draw same sprite in multiple box2d object layers.?
我正在做游戏,这是我第一次...我遇到了一个我无法帮助自己解决的问题。让我们切入正题,我有一个硬币 class,它将硬币吸引到唯一的一个 box2D 物体,但是在那个层(我在平铺中制作),我有更多的 1 个物体,但硬币只出现在其中的 1 个中他们,最后一个。我希望硬币纹理出现在我为硬币定义的所有对象层中,我还将 post 图片帮助理解我的问题 better.Code 硬币图像如下所示;
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);
}
}
仅供参考:
在我的主屏幕 class 中,我已经声明并实例化了我的硬币 class 以传递相关参数,并且在主屏幕的更新方法中调用了硬币 class 的更新方法class,同样在播放屏幕 class 的渲染方法中,我调用了 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 个变量中。结果只有最后创建的 body 存储在该变量中。相反,您必须创建一个数组来存储每个创建的 body 并为每个 body 位置绘制一个精灵。如果您想使用 sprite-extended class,您最好为每个 body 创建单独的 'Coin' object,但使用相同的纹理区域 object。更好的方法是在单独的 class 而不是在构造函数中创建主体和区域。
示例(这是伪代码):
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
}
}
希望对您有所帮助! )
我正在做游戏,这是我第一次...我遇到了一个我无法帮助自己解决的问题。让我们切入正题,我有一个硬币 class,它将硬币吸引到唯一的一个 box2D 物体,但是在那个层(我在平铺中制作),我有更多的 1 个物体,但硬币只出现在其中的 1 个中他们,最后一个。我希望硬币纹理出现在我为硬币定义的所有对象层中,我还将 post 图片帮助理解我的问题 better.Code 硬币图像如下所示;
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);
}
}
仅供参考:
在我的主屏幕 class 中,我已经声明并实例化了我的硬币 class 以传递相关参数,并且在主屏幕的更新方法中调用了硬币 class 的更新方法class,同样在播放屏幕 class 的渲染方法中,我调用了 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 个变量中。结果只有最后创建的 body 存储在该变量中。相反,您必须创建一个数组来存储每个创建的 body 并为每个 body 位置绘制一个精灵。如果您想使用 sprite-extended class,您最好为每个 body 创建单独的 'Coin' object,但使用相同的纹理区域 object。更好的方法是在单独的 class 而不是在构造函数中创建主体和区域。
示例(这是伪代码):
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
}
}
希望对您有所帮助! )