java中的单个磁贴(libgdx&x2B;tileMap)

java中的单个磁贴(libgdx&x2B;tileMap),java,libgdx,tile,Java,Libgdx,Tile,我正在制作一个老超级马里奥的复制品,当马里奥以他的小体型从下面撞到砖瓦时,我正试图摇晃砖瓦。这个概念取自Brent Aureli编写的一个很棒的libgdx教程,以及他编写/展示的所有作品,但是当我做这个(看起来很小的)修改时,修改本身的行为很奇怪 也就是说,当小马里奥碰到瓷砖(任何砖块瓷砖)时,所有这类瓷砖都会得到这个偏移量。根据我的理解,访问是getCell().getTile().setOffsetY(value),这意味着 获取单个单元格->获取该单元格中的平铺->将偏移量设置为给定单元

我正在制作一个老超级马里奥的复制品,当马里奥以他的小体型从下面撞到砖瓦时,我正试图摇晃砖瓦。这个概念取自Brent Aureli编写的一个很棒的libgdx教程,以及他编写/展示的所有作品,但是当我做这个(看起来很小的)修改时,修改本身的行为很奇怪

也就是说,当小马里奥碰到瓷砖(任何砖块瓷砖)时,所有这类瓷砖都会得到这个偏移量。根据我的理解,访问是
getCell().getTile().setOffsetY(value)
,这意味着

获取单个单元格->获取该单元格中的平铺->将偏移量设置为给定单元格中的该平铺。 文件还建议它应该这样工作:

当big mario点击互动程序时,将互动程序替换为null可以正常工作,所以我不理解为什么使用getCell.getTile的方法不能正常工作

最后,我意识到细胞是不动的,我只想在半秒钟左右的时间里对实际纹理进行一次纯粹的修饰性变换——这是可能的,但在当前状态下,它会偏移所有类似的瓷砖

为了好玩,我

    public class Brick extends InteractiveTile {

    private static TiledMapTileSet tileSet;

    public Brick(PlayScreen screen, MapObject object) {
        super(screen, object);
        tileSet = screen.getMap().getTileSets().getTileSet("tileset_gutter");
        fixture.setUserData(this);
        setCategoryFilter(SuperMario.BRICK_BIT);
    }

    @Override
    public void onHeadHit(Mario mario) {
        if(mario.isBig()){
            SuperMario.manager.get("audio/breakblock.wav", Sound.class).play();
            setCategoryFilter(SuperMario.DESTROYED_BIT);

            getCell().setTile(null);
        }
        else {
            SuperMario.manager.get("audio/bump.wav", Sound.class).play();
// DOES THE EXACT SAME AS LATER 3 LINES
//            getCell().getTile().setOffsetY(2);

            getCell().setTile(tileSet.getTile(28)); //this can change texture of individual tile.

            TiledMapTile tile = getCell().getTile();
            tile.setOffsetY(2);
            getCell().setTile(tile);
        }

    }
}
互动:

public abstract class InteractiveTile {

    private World world;
    private TiledMap map;
    private TiledMapTile tile;
    private Rectangle bounds;

    protected Body body;
    protected final Fixture fixture;
    protected PlayScreen screen;
    protected MapObject object;


    public InteractiveTile(PlayScreen screen, MapObject object){
        this.screen = screen;
        this.world = screen.getWorld();
        this.map = screen.getMap();
        this.object = object;
        this.bounds = ((RectangleMapObject)object).getRectangle();

        BodyDef bdef = new BodyDef();
        FixtureDef fdef = new FixtureDef();
        PolygonShape shape = new PolygonShape();
        bdef.type = BodyDef.BodyType.StaticBody;
        bdef.position.set(
                (bounds.getX()+ bounds.getWidth()/2)/ SuperMario.PPM,
                (bounds.getY() + bounds.getHeight()/2)/SuperMario.PPM);
        body = world.createBody(bdef);
        shape.setAsBox(
                (bounds.getWidth()/2)/SuperMario.PPM,
                (bounds.getHeight()/2)/SuperMario.PPM);            fdef.shape = shape;
        fixture = body.createFixture(fdef);
    }

    public abstract void onHeadHit(Mario mario);

    public TiledMapTileLayer.Cell getCell(){
        TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1);
        return layer.getCell(
                //REVERT SCALING (PPM) AND by tile size
                (int)(body.getPosition().x * SuperMario.PPM / 16),
                (int)(body.getPosition().y * SuperMario.PPM / 16));
    }

    public void setCategoryFilter(short filterBit){
        Filter filter = new Filter();
        filter.categoryBits = filterBit;
        fixture.setFilterData(filter);
    }
}
此代码中是否有错误,或者是发动机的限制

PS-原始(未修改)文件可在Brent Aurelis github网站上找到:


平铺实例在所有单元中共享(出于内存保护目的)。文档并没有建议每个单元格都包含或返回原始磁贴的副本。因此,换句话说,不可能访问单个磁贴/纹理。我想我得找个解决办法了——谢谢。我希望当瓷砖共享纹理本身时,单个瓷砖可以被操纵,就像我可以改变单个瓷砖的纹理一样。如果你愿意,你可以对加载程序进行子类化,让它创建瓷砖的副本。您还可以迭代单元格并执行相同的操作。或者,您可以使用具有偏移量的单元子类和可以从单元中获取偏移量的自定义呈现程序。我想我将尝试使用自定义加载程序-似乎这比遍历每个单元需要更少的周期-谢谢您的建议!