简体   繁体   English

我试图返回x和y值来设置游戏中的实体生成。 我在这里想念什么?

[英]I am trying to return an x and y value to set an entities spawn in my game. What am I missing here?

I am trying to set the spawn point for the player to be at a specific tile, but I am not sure how to accomplish that. 我正在尝试将播放器的生成点设置为特定的图块,但是我不确定如何实现。 Here is the relevant code: 以下是相关代码:

From Player class: 从播放器类:

public Player(Level level, int x, int y, InputHandler input) {
    super(level, "Player", x, y, 1);
    this.input = input;
}

From Game class: 从游戏类:

public int spawnX = Level.getSpawnTileX();
public int spawnY = Level.getSpawnTileY(getY());

This is in my init(): 这是在我的init()中:

player = new Player(level, spawnX, spawnY, input);

This is from my Level class: 这是从我的水平班上得到的:

public static int getSpawnTileX(int x){
    for(int y = 0; y < height; y++){
        for(int x1 = 0; x1 < width; x1++){
            for(Tile t : Tile.tiles){
            if(t.getLevelColor() == 0xff00ff00){
                return x1;                  
            }else{
                return 0;
            }
            }
        }
    }
    return x;
}
public static int getSpawnTileY(int y){
    for(int x = 0; x < width; x++){
        for(int y1 = 0; y1 < height; y1++){
            for(Tile t : Tile.tiles){
            if(t != null && t.getLevelColor() == 0xff00ff00){
                return y1;
            }else{
                return 0;
            }
            }
        }
    }
    return y;
}

And this is from my Tile class: 这是我的Tile类中的内容:

public static final Tile SPAWN = new BasicTile(3, 3, 0, Colors.get(-1, 141, 131, -1), 0xffff0000);
public int getLevelColor(){
    return levelColor;
}

Tile class: 瓷砖类:

public abstract class Tile {

public static final Tile[] tiles = new Tile[256];
public static final Tile VOID = new BasicSolidTile(0, 0, 0, Colors.get(000, -1, -1, -1), 0xff000000);
public static final Tile STONE = new BasicSolidTile(1, 1, 0, Colors.get(-1, 333, -1, -1), 0xff555555);
public static final Tile GRASS = new BasicTile(2, 2, 0, Colors.get(-1, 131, 141, -1), 0xff00ff00);
public static final Tile SPAWN = new BasicTile(3, 3, 0, Colors.get(-1, 141, 131, -1), 0xffff0000);

If more code is needed for clarification, I would be happy to try to provide it. 如果需要更多代码进行澄清,我很乐意尝试提供它。 Any help on this is much appreciated. 任何帮助对此表示感谢。

Your issue is here: 您的问题在这里:

        for(Tile t : Tile.tiles){
        if(t.getLevelColor() == 0xff00ff00){
            return x1;                  
        }else{
            return 0;
        }
        }

It should be this: 应该是这样的:

        for(Tile t : Tile.tiles){
        if(t.getLevelColor() == 0xff00ff00){
            return x1;                  
        }
        }

The issue is that it only checks the first value in Tile.tiles . 问题是它仅检查Tile.tiles的第一个值。 If it isn't the correct value, it immediately returns 0. The change I show above makes it actually continue, and check the rest of the tiles. 如果它不是正确的值,它将立即返回0。我在上面显示的更改实际上使它继续,并检查了其余图块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM