You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中角色无法在地图指定颜色像素位置生成的问题

角色生成位置错误问题排查与修复

我在Java开发的游戏地图中,尝试让角色在指定颜色像素的位置生成时遇到问题。完全按照教学视频编写代码,但角色并未出现在正确位置,反而生成在屏幕上方中间区域。我使用以下World类代码控制角色、Tile及Entity的生成位置,其中通过Game.player.setX(xx*16);和Game.player.setY(yy*16);设置角色坐标,但没有效果。

World类代码

public class World {
    
    private Tile[] tiles;
    public static int WIDTH,HEIGHT;

    public World(String path) {
        try {
            BufferedImage map = ImageIO.read(getClass().getResource(path));
            int[] pixels = new int[map.getWidth() * map.getHeight()];
            WIDTH = map.getWidth();
            HEIGHT = map.getHeight();
            tiles = new Tile[map.getWidth() * map.getHeight()];
            map.getRGB(0, 0, map.getWidth(), map.getHeight(), pixels, 0, map.getWidth());
            for(int xx = 0; xx < map.getWidth(); xx++) {
                for(int yy = 0; yy < map.getHeight(); yy++) {
                    int pixelAtual = pixels[xx + (yy*map.getWidth())];
                    tiles[xx + (yy * WIDTH)] = new FloorTile(xx*16,yy*16, Tile.TILE_FLOOR);
                    if(pixelAtual == 0xFF000000) {
                        //FLOOR
                        tiles[xx + (yy * WIDTH)] = new FloorTile(xx*16,yy*16, Tile.TILE_FLOOR);
                    }else if(pixelAtual == 0xFFFFFFFF){
                        //PAREDE
                        tiles[xx + (yy * WIDTH)] = new FloorTile(xx*16,yy*16, Tile.TILE_WALL);

                    }else if(pixelAtual == 0xFF0026FF) {
                        //Player    
                        Game.player.setX(xx*16);
                        Game.player.setY(yy*16);
                    }else if(pixelAtual == 0xFFFF0000){
                        //Enemy
                        
                    }else if(pixelAtual == 0xFFFF00DC) {
                        //WEAPON
                        
                    }else if(pixelAtual == 0xFFFF7F7F) {
                        //LIFEPACK
                        
                    }else if(pixelAtual == 0xFFFFD800) {
                        //BULLET
                        
                    }
                    
                    
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void render(Graphics g) {
        for(int xx = 0; xx < WIDTH; xx++) {
            for(int yy = 0; yy < HEIGHT; yy++) {
                Tile tile = tiles[xx + (yy*WIDTH)];
                tile.render(g);
            }
        }
    }
    
}

Entity类代码

public class Entity {

    public static BufferedImage LIFEPACK_EN = Game.spritesheet.getSprite(78, 0, 16, 16);
    public static BufferedImage WEAPON_EN = Game.spritesheet.getSprite(96, 0, 16, 16);
    public static BufferedImage BULLET_EN = Game.spritesheet.getSprite(78, 16, 16, 16);
    public static BufferedImage ENEMY_EN = Game.spritesheet.getSprite(96, 16, 16, 16);
    
    protected double x;
    protected double y;
    protected int width;
    protected int height;
    
    private BufferedImage sprite;
    
    public Entity(int x, int y, int width, int height, BufferedImage sprite) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.sprite = sprite;
    }
    
    public void setX(int newX) {
        this.x = newX;
    }
    
    public void setY(int newY) {
        this.x = newY;
    }
    
    public int getX() {
        return (int)this.x;
    }
    
    public int getY() {
        return (int)this.y;
    }
    
    public int getWidth() {
        return this.width;
    }
    
    public int getHeight() {
        return this.height;
    }
    
    public void update() {
        
    }
    
    public void render(Graphics g) {
        g.drawImage(sprite, this.getX(), this.getY(), null);
    }
}

问题根源与修复

你的Entity类中存在明显笔误:setY方法错误地将传入的newY赋值给了this.x,而非this.y。这导致调用Game.player.setY(yy*16)时,实际修改的是角色的X坐标,Y坐标始终保持初始值(通常为0或默认中间值),所以角色会出现在屏幕上方中间区域。

修复方法很简单,修正Entity类的setY方法:

public void setY(int newY) {
    this.y = newY;
}

此外还可额外检查两点确保问题彻底解决:

  • 确认地图图片中确实存在颜色值为0xFF0026FF的像素(即你指定的角色生成点)
  • 检查Game.player的初始坐标是否未在其他代码中被覆盖

内容的提问来源于stack exchange,提问作者Caio Satorio Bisi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 09:30:57