0

我有一个游戏引擎,它试图通过将图像存储在 HashMap 中来有效地加载资源。当我想为 Sprite 对象加载图像时,我只需说 Sprite.image = LOAD_IMAGE(imageName);

现在我正在为这款游戏制作地图编辑器。我希望能够旋转某些瓷砖,例如房子的瓷砖,通过 Java 图像操作面向不同的方向。我有那个工作;但是,错误出现在我将图像设置为新的旋转图像的方式中。我说 Sprite.image = rotateImage(Sprite.image); 这导致相同类型的所有 TILES 具有相同的新旋转图像。

我可以在我的游戏中实现什么,以便每个 Sprite 都可以旋转而不影响其他 Sprite 的图像?如果可能的话,我仍然想保留我的 HashMap,因为我认为它确实会提高我的游戏效率。

这就是我实现轮换的方式:

terrain.terrainImage = ImageManipulator.mirror(currentTile.terrain.terrainImage, false);

这是我的“ImageBank”: public HashMap images = new HashMap(30);

public synchronized Image getImage(String imgName) {
    // Check if the HashMap images already has the specified image
            // by checking for the image name.
    if (imgExists(imgName)) {
        // Sprite already loaded.
        return hm.get(imgName);
    } else {
        // Sprite hasn't been loaded yet.
        Image img;
        img = loadImage(imgName);
        if (img == null) {
            // An error occured, couldn't load sprite.
            return null;
        } // else move on

        // save the loaded sprite (and now transparent) into the hashmap
        hm.put(imgName, img);
        // and then return the sprite
        return img;
    }
}
4

1 回答 1

0

您可以为每个图块存储它是否旋转以及旋转的角度(而不是每次绘制它时旋转它),或者将旋转的副本存储在图像上。

我建议存储旋转版本。

于 2012-12-28T22:54:16.527 回答