开发者

Tile disappearing in previous position after being "set"

开发者 https://www.devze.com 2023-03-15 20:05 出处:网络
I\'m creating a map editor for a little game project that I\'m doing. Considering that the map editor isn\'t going to be intense, I simply used java 2d, which I hate. Anyways, here is my map.java, til

I'm creating a map editor for a little game project that I'm doing. Considering that the map editor isn't going to be intense, I simply used java 2d, which I hate. Anyways, here is my map.java, tile.java, and my TileList.java code.

FIXED, I modified my TileList.java code (set function) to this: Alright, I fixed it: I simply changed the set(Tile tile) function!

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());
        }
    }
}

Image showing error: 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 w开发者_开发问答here 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 package 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];
    }
}


One thing that might help to make it a bit clearer what exactly is going on and also more efficient in your code is to eradicate the loop within this function:

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;
        }
    }
}

Now I would recommend doing something along the lines of this:

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;
     }
}

It now should be more efficient and maybe you can spot the error :) I would also probably replace the getTile function to do the same, when you know the x,y position you do not need to loop to find it.


I think the problem here is that you're passing in a new (and likely temporary) Tile object when you call setTile. Thus, when you edit/change another tile, that temporary object gets overwritten with the new tile object, and the old one gets deleted. One way to be sure of this is if you edit a third tile and the first two you changed disappear.

Generally, tile editors (and in fact, tile based games) don't change the tile object, they simply change the properties of that object to new values when things change. This has many benefits, mostly related to the expense of creating a new object. So, what you should be doing in this map editor is passing in the new sprite value (along with any other things that might change), instead of creating a whole new tile object for each edit.

You could also create an array (or map, or list, or something) that contains the properties of each type of tile, and simply pass in a value that points to an index of that array. I could probably write some pseudo-code for either of these solutions if you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号