I've been trying to get my characters to spawn at random points through out the map. I have a camera which is following my main character (2D RPG Style) around the screen, but I want these "enemies" to spawn in random places amongst the map.

I can't seem to get this to work, no enemies are spawning. With my method summonMonsters(showTime) statement in 'render', it seems that my characters are spawning relative to where I am on the screen (so not at all).

What I really want is these entities to spawn within the map without the camera being on them or relative to me at all.

The coordinates I'm printing out as to where these enemies should be spawning shows ranges that should appear on the map, which is leading me to think they are spawning relative to the camera (where I am), which I don't want.

This is what I have so far:

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;

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


    font = new BitmapFont();

public void dispose(){

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

    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


    //these update the coordinates every render refresh
    posX = player.sprite.getX();
    posY = player.sprite.getY();

    time += Gdx.graphics.getDeltaTime();

    player.sprite.draw(batch); //draw the sprite to the batch


    font.draw(ui, "Time: "+time, 50, 600);
    font.draw(ui,"posX: "+posX, 50, 550);
    font.draw(ui,"posY: "+posY, 50, 525);

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

//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.

Monsters class:

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

public void moveTo(float playerX, float playerY, float enemyX, float enemyY) {
    // TODO Auto-generated method stub


public float getX() {
    return x;

float getY() {
    return y;

You're monster locations are based on a random number:

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

You initialize width and height with:

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

So your monsters will be placed on a random location in screen coordinates.

Now I'm not sure about your drawing code but it seems to me that you always only draw the first monster and draw it at the location of the player:

posX = player.sprite.getX();
posY = player.sprite.getY();


It seems to me there are three bugs here.

  1. Monsters should be placed on a location on the map so use map dimenisions
  2. When drawing a monster the monster's coordinates should be translated from map coordinates to where that position on the map is on the screen right now.
  3. Monsters are currently drawn using the player coordinates, not their own coordinates.
  • \$\begingroup\$ Sorry, that first bit of code was just to see whether something is printing out, those lines wouldn't be there. What I actually want to happen is the summonMonsters method in the main class to spawn them using screen coordinates when gameTime==0. Though, nothing seems to be appearing on the map, and when I print out the coordinates at which they should be printing at, they are in valid locations on the map. .posX = player.sprite.getX(); posY = player.sprite.getY(); showTime.draw(enemyMonster.get(0).img,posX,posY); \$\endgroup\$ – magna_nz Aug 28 '14 at 11:18
  • \$\begingroup\$ .posX = player.sprite.getX(); posY = player.sprite.getY(); showTime.draw(enemyMonster.get(0).img,posX,posY); So these lines here are just to test. My main issue is why does my summonMonsters method not print sprites to the screen on the map, when I've given them screen coordinates. Thanks very much for your help so far though Roy \$\endgroup\$ – magna_nz Aug 28 '14 at 11:25
  • 1
    \$\begingroup\$ Solved, had to keep the enemies location relative to player! \$\endgroup\$ – magna_nz Aug 28 '14 at 12:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.