敌人产卵的问题

我一直试图让我的角色在随机点出现在地图上。 我有一个相机跟随我的主角(2D RPG风格)在屏幕周围,但是我想让这些“敌人”在随机的地方产生。

我似乎无法得到这个工作,没有敌人产生。 用我的方法summonMonsters(showTime)语句在'渲染',似乎我的角色相对于我在屏幕上(所以根本没有)产卵。

我真正想要的是这些实体在地图上产卵,而没有相机在他们身上或相对于我。

我打印出的这些敌人应该在哪里产卵的坐标显示了应该出现在地图上的范围,这导致我认为它们是相对于相机产生的(我在哪里),这是我不想要的。

这是我迄今为止:

 public class PlatformerGame extends ApplicationAdapter implements ApplicationListener, InputProcessor { public static final String TITLE = "ADVENTURE RPG"; private float time, speed = 160; public SpriteBatch batch, ui, showTime; private BitmapFont font; private float posX; private float posY; Maps maps; Random r; Player player; GameManager game; Monster monster; public float width, height; ArrayList<Monster> enemyMonster= new ArrayList<Monster>(); //holds the list of enemies as Monster boolean summoned = false; @Override public void create () { width = Gdx.graphics.getWidth(); height = Gdx.graphics.getHeight(); r = new Random(); game = new GameManager(); player = new Player(200); maps = new Maps(); //sets up the initial map monster = new Monster(20); setUpMonsters(); //sets up monsters batch = new SpriteBatch(); ui = new SpriteBatch(); showTime = new SpriteBatch(); Gdx.input.setInputProcessor(this); font = new BitmapFont(); font.setColor(Color.RED); } public void dispose(){ batch.dispose(); } @Override public void render () { //clear the screens after every refresh Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); player.camera.position.set(player.sprite.getX(), player.sprite.getY(), 0); //camera follows the char around player.camera.update(); //update the camera, pass it to tilemaprenderer, then render the map maps.tiledMapRenderer.setView(player.camera); maps.tiledMapRenderer.render(); batch.setProjectionMatrix(player.camera.combined); handleInput(); checkCollisions(); //these update the coordinates every render refresh posX = player.sprite.getX(); posY = player.sprite.getY(); batch.begin(); time += Gdx.graphics.getDeltaTime(); player.sprite.draw(batch); //draw the sprite to the batch batch.end(); ui.begin(); font.draw(ui, "Time: "+time, 50, 600); font.draw(ui,"posX: "+posX, 50, 550); font.draw(ui,"posY: "+posY, 50, 525); ui.end(); showTime.begin(); font.draw(showTime, "Time until next wave: " +game.roundTime, 1000, 650); summonMonsters(showTime);//if gametime is equal to zero, summon monsters showTime.draw(enemyMonster.get(0).img,posX,posY); //this just tests to draw the image System.out.println("GG: " + (20-posX) + ", " + (20-posY)); showTime.end(); } //create 5 new monster objects and add them to arraylist public void setUpMonsters(){ //int distance = 0; for (int i = 0; i < monster.NUMBER_OF_MONSTERS; i++){ //distance +=5; Monster monster = new Monster(20); monster.setPositionXY((r.nextInt((int) width)), (r.nextInt((int) height))); //set enemies up in random places on map enemyMonster.add(monster); //add monster object to arraylist } } //draws the monster to screen public void summonMonsters(SpriteBatch batch){ if (game.roundTime == 0){ //if roundtime is zero and it's time for another wave if (summoned == false){ //if minions havent been summoned already, summon them and draw to screen for (int i = 0; i < monster.NUMBER_OF_MONSTERS; i++){ batch.draw(enemyMonster.get(i).img, enemyMonster.get(i).getX(), enemyMonster.get(i).getY()); System.out.println(enemyMonster.get(i).getX() + ", " + enemyMonster.get(i).getY()); //print out positions } } summoned = true; //when all minions are dead, set it back to false. } } 

怪兽类:

 public class Monster extends Entities{ public Texture img; public Sprite sprite; private int health; public int NUMBER_OF_MONSTERS = 5; private int x, y; //x,y coordinate for set position Random r = new Random(); GameManager game; PlatformerGame pg; public Monster(int health){ game = new GameManager(); this.health = 20; //import img character and then set as a sprite img = new Texture(Gdx.files.internal("char/lydiachar.gif")); sprite = new Sprite(img); } //sets the position of the sprites public void setPositionXY(int x, int y){ this.x = x; this.y = y; sprite.setPosition((float) x, (float) y); } @Override public void moveTo(float playerX, float playerY, float enemyX, float enemyY) { // TODO Auto-generated method stub } @Override public float getX() { return x; } @Override float getY() { return y; } 

你是怪物的位置是基于一个随机数字:

 monster.setPositionXY((r.nextInt((int) width)), (r.nextInt((int) height))); 

你用下面的方法初始化宽度和高度:

 width = Gdx.graphics.getWidth(); height = Gdx.graphics.getHeight(); 

所以你的怪物将被放置在屏幕坐标的随机位置。

现在我不确定你的绘图代码,但是在我看来,你总是只画出第一个怪物,并在玩家的位置绘制:

 posX = player.sprite.getX(); posY = player.sprite.getY(); showTime.draw(enemyMonster.get(0).img,posX,posY); 

在我看来,这里有三个错误。

  1. 怪物应该放在地图上的一个位置,所以使用地图尺寸
  2. 在绘制怪物时,怪物的坐标应该从地图坐标转换到地图上该位置在屏幕上的位置。
  3. 目前使用玩家坐标绘制怪物,而不是他们自己的坐标。