“设定”后瓷砖在先前位置消失

时间:2011-06-28 17:45:05

标签: map 2d

我正在为我正在做的小游戏项目创建一个地图编辑器。考虑到地图编辑器不会很激烈,我只使用java 2d,我讨厌。无论如何,这是我的map.java,tile.java和我的TileList.java代码。

FIXED,我将我的TileList.java代码(设置函数)修改为:好吧,我修复了它:我只是更改了set(Tile tile)函数!

public void set(Tile tile) {
    for(int i = 0; i < this.tileList.length; i++) {
        int x = this.tileList[i].getX();
        int y = this.tileList[i].getY();
        if((x == tile.getX()) && (y == tile.getY())) {
            System.out.println("Changing tile: (" + x + "," + y + ")" + " with (" + tile.getX() + "," + tile.getY() + ")");
            this.tileList[i].setImage(tile.getImage());
        }
    }
}

图片显示错误:http://i.imgur.com/eosPt.png

map.java:

package org.naive.gui.impl;

import org.naive.util.TileList;

import javax.swing.JPanel;
import java.util.HashMap;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * Copyright 2011 Fellixombc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class Map extends JPanel implements MouseListener {

    private final int tileSize;
    private final int mapSize;
    private final int size;
    private TileList tileSet;
    private Tile currentTile = null;

    /* Creates the Map, e.g, Panel
     * @param int Desired size (in tiles) of the map
     */
    public Map(int size, int tileSize) {
        this.tileSize = tileSize / 2;
        this.size = size;
        this.mapSize = (this.tileSize)*(size/2);
        this.tileSet = new TileList(size/2 * size/2);
        properties();
    }

    /* Initlize the properties for the JPanel
     */
    public void properties() {
        this.setPreferredSize(new Dimension(mapSize, mapSize));
        this.addMouseListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D gfx = (Graphics2D) g;
        for(int i = 0; i < this.tileSet.size; i++) {
            Tile tile = this.tileSet.get(i);
            gfx.drawImage(tile.getImage(), tile.getX(), tile.getY(), null);
        }
        for(int i = 0; i <= size/2; i++) {
            gfx.setColor(Color.BLACK);
            gfx.drawLine(i * this.tileSize, 0, i * this.tileSize, this.tileSize * this.size/2);
            gfx.drawLine(0, i * this.tileSize, this.tileSize * this.size/2, i * this.tileSize);
        }
    }

    public void populate() {
        int i = 0;
        for(int x = 0; x < size/2; x++) {
            for(int y = 0; y < size/2; y++) {
                Tile tile = new Tile("grass.png");
                tile.setPos(x * this.tileSize, y * this.tileSize);
                this.tileSet.setAtIndex(i, tile);
                i++;
            }
        }
    }

    /* Sets a new tile
     * @param tile The *new* tile to be set
     */
    public void setTile(Tile tile) {
        if(this.currentTile != null) {
            tile.setPos(this.currentTile.getX(), this.currentTile.getY());
            this.tileSet.set(tile);
            this.currentTile = tile;
        }
        this.repaint();
    }

    /* Gets the tile closest* to the mouse click
     * @param int The x-axis location of the mouse click
     * @param2 int The y-axis location of the mouse click
     */
    public void getTile(int x, int y) {
        for(int i = 0; i < this.tileSet.size; i++) {
            Tile tile = this.tileSet.get(i);
            int minX = tile.getX();
            int minY = tile.getY();
            int maxX = minX + this.tileSize;
            int maxY = minY + this.tileSize;

            if((x >= minX) && (x < maxX) && (y >= minY) && (y < maxY)) {
                this.currentTile = tile;
                System.out.println("Tile at: " + "(" + this.currentTile.getX() + "," + this.currentTile.getY() + ")");
            }
        }
    }


    public void setTileSet(TileList tileSet) {
        this.tileSet = tileSet;
    }

    /* Gets the TileList, e.g, the tiles of the 'map'
     * @return hashmap Returns the list of tiles
     */
    public TileList getTileSet() {
        return this.tileSet;
    }

    public int getMapSize() {
        return this.size;
    }

    public int getTileSize() {
        return this.tileSize * 2;
    }


    /* Gets where the mouse clicked on the canvas
     * @param mouseevent Where the mouse event occurred
     */
    public void mouseClicked(MouseEvent e) {
        this.getTile(e.getX(), e.getY());
    }

    /* Useless..
     */
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}

Tile.java

package org.naive.gui.impl;

import javax.swing.ImageIcon;

/**
 * Copyright 2011 Fellixombc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class Tile extends ImageIcon {

    private int x = 0;
    private int y = 0;
    private final String sprite;

    public Tile(String sprite) {
        super("data/sprite/" + sprite);
        this.sprite = sprite;
    }

    public String getSprite() {
        return this.sprite;
    }

    public void setPos(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

}

TileList.java     包org.naive.util;

import org.naive.gui.impl.Tile;

import java.util.Iterator;

/**
 * Copyright 2011 Fellixombc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class TileList {

    public int size;
    private Tile[] tileList;

    public TileList(int size) {
        this.size = size;
        this.tileList = new Tile[size];
    }

    public void setAtIndex(int index, Tile tile) {
        this.tileList[index] = tile;
    }

    public void set(Tile tile) {
        for(int i = 0; i < this.tileList.length; i++) {
            int x = this.tileList[i].getX();
            int y = this.tileList[i].getY();
            if((x == tile.getX()) && (y == tile.getY())) {
                System.out.println("Changing tile: (" + x + "," + y + ")" + " with (" + tile.getX() + "," + tile.getY() + ")");
                this.tileList[i] = tile;
            }
        }
    }

    public Tile get(int index) {
        return this.tileList[index];
    }
}

2 个答案:

答案 0 :(得分:0)

有一件事可能有助于让它更准确地发生什么,以及在代码中更高效的是消除此函数中的循环:

public void set(Tile tile) {
    for(int i = 0; i < this.tileList.length; i++) {
        int x = this.tileList[i].getX();
        int y = this.tileList[i].getY();
        if((x == tile.getX()) && (y == tile.getY())) {
            System.out.println("Changing tile: (" + x + "," + y + ")" + " with (" + tile.getX() + "," + tile.getY() + ")");
            this.tileList[i] = tile;
        }
    }
}

现在我建议你做一些事情:

public void set(Tile tile)
{
     int tileIndex = tile.getX() + (tile.getY() * 16); // 16 is the grid Y segments, this assumes your grid is square
     if(tileIndex >= 0 && tileIndex < (16*16)) // some validation 0 to max size
     {
         System.out.println("Changing tile: (" + tile.getX() + "," + tile.getY() + ")");
         this.tileList[i] = tile;
     }
}

它现在应该更高效,也许你可以发现错误:)当你知道你不需要循环找到它的x,y位置时,我也可能会替换getTile函数来做同样的事情。 / p>

答案 1 :(得分:0)

我认为这里的问题是你在调用setTile时传入一个新的(可能是临时的)Tile对象。因此,当您编辑/更改另一个图块时,该临时对象将被新图块对象覆盖,旧图块将被删除。确保这一点的一种方法是,如果您编辑第三个图块,则您更改的前两个图块将消失。

通常,平铺编辑器(实际上是基于平铺的游戏)不会更改平铺对象,它们只是在事物发生变化时将该对象的属性更改为新值。这有许多好处,主要与创建新对象的费用有关。所以,你应该在这个地图编辑器中做的是传递新的精灵值(以及可能改变的任何其他东西),而不是为每次编辑创建一个全新的tile对象。

您还可以创建一个包含每种类型磁贴属性的数组(或映射,列表或其他内容),并简单地传入一个指向该数组索引的值。如果需要的话,我可能会为这些解决方案编写一些伪代码。