Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Every time I create a new asteroid the previous one is no longer rendered?

I did some debugging and printed out the size of Array-List 'Small' and when a new asteroid is created it doesn't go down, so the thread is still there it's just not being rendered, Why?

StatePlay:

    package me.bleedobsidian.astroidjump;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class StatePlay extends BasicGameState
{
    int stateID = 10;

    Player player;
    Asteroids asteroids;

    StatePlay(int stateID) 
    {
       this.stateID = stateID;
    }

    @Override
    public int getID()
    {
        return stateID;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    {
        ResManager.loadImages();
        player = new Player();
        asteroids = new Asteroids();
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
    {
        g.setAntiAlias(true);
        player.render(g);
        asteroids.render(g);
        g.drawString("Asteroids: " + Asteroids.small.size(), 10, 25);
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
        player.update(gc, delta);
        asteroids.update(delta);
    }

}

Asteroids:

package me.bleedobsidian.astroidjump;

import java.util.ArrayList;
import java.util.Timer;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;

public class Asteroids
{
    public static ArrayList<Asteroid_Small> small = new ArrayList<Asteroid_Small>();

    static SpriteSheet small_sprites = new SpriteSheet(ResManager.asteroids_small_ss, 32, 32);

    static Image small_1 = small_sprites.getSubImage(0, 0);
    static Image small_2 = small_sprites.getSubImage(1, 0);
    static Image small_3 = small_sprites.getSubImage(2, 0);
    static Image small_4 = small_sprites.getSubImage(3, 0);

    static boolean asteroids = true;

    static int diff = 0;

    Asteroids()
    {
        Task_Asteroids TaskA = new Task_Asteroids();
        Timer timer = new Timer("Asteroids");

        if(diff == 0)
        {
            timer.schedule(TaskA, 0, 4000);
        }
        else if(diff == 1)
        {
            timer.schedule(TaskA, 0, 3000);
        }
    }

    public static Image chooseSmallImage(int i)
    {
        if(i == 0)
        {
            return small_1;
        }
        else if(i == 1)
        {
            return small_2;
        }
        else if(i == 2)
        {
            return small_3;
        }
        else if(i == 3)
        {
            return small_4;
        }
        else
        {
            return small_1;
        }
    }

    public static void level_manager(float x)
    {
        if(x < 1000)
        {
            diff = 0;
        }
        else if(x < 2000)
        {
            diff = 1;
        }
        else if(x < 3000)
        {
            diff = 2;
        }
        else if(x < 5000)
        {
            diff = 3;
        }
        else if(x < 10000)
        {
            diff = 4;
        }
        else
        {
            diff = 5;
        }
    }

    public void update(int delta)
    {
        for(int s = 0; s < small.size(); s++)
        {
            Asteroid_Small as = small.get(s);
            as.update(delta);
        }
    }

    public void render(Graphics g)
    {
        for(int s = 0; s < small.size(); s++)
        {
            Asteroid_Small as = small.get(s);
            as.render(g);
        }
    }

    public static void setAsteroids(boolean tf)
    {
        asteroids = tf;
    }
}

Asteroid_Small:

package me.bleedobsidian.astroidjump;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

public class Asteroid_Small
{
    private static Image me;

    private static float x = 0;
    private static float y = 0;
    private static float speed = 0;
    private static float rotation = 0;
    private static float rotation_speed = 0;

    Asteroid_Small(Image i, float x, float y, float rs, float sp)
    {
        me = i;

        Asteroid_Small.x = x;
        Asteroid_Small.y = y;
        Asteroid_Small.rotation_speed = rs;
        Asteroid_Small.speed = sp;
    }

    public void update(int delta)
    {
        x -= speed * delta;
        rotation += rotation_speed * delta;

        me.setRotation(rotation);
    }

    public void render(Graphics g)
    {
        g.drawImage(me, x, y);
    }
}

Task_Asteroid:

package me.bleedobsidian.astroidjump;

import java.util.TimerTask;

public class Task_Asteroids extends TimerTask
{
    public void run()
    {
        if(Asteroids.diff == 0)
        {
            int randImage = (int) (Math.random() * 4);
            int randHeight = (int) (Math.random() * 480);

            Asteroids.small.add(new Asteroid_Small(Asteroids.chooseSmallImage(randImage), Player.x + 960, randHeight, 0.05f, 0.04f));
        }
    }
}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Have you used a System.out.println(i) call inside your asteroid render loop to check whether it is in fact being iterated over multiple times? -- If it's not that loop, it's something else.

Also, you may want to ensure that the positions of your asteroids are not overlapping, by building some minimal test case. That's more than likely where the problem lies -- it probably is rendering everything, but you just see it rendering one because they're overlapping.

Frankly, from long experience, I suspect that your problem comes in where you batch your asteroids into one big Asteroids object. That is not good structure. Having one class/struct per asteroid is, and then iterating through them as individual loops, in your StatePlay glass, in each of its update and render methods.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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