Why are only some of my objects being rendered?

Posted by BleedObsidian on Game Development See other posts from Game Development or by BleedObsidian
Published on 2012-12-06T20:18:47Z Indexed on 2012/12/06 23:27 UTC
Read the original article Hit count: 209

Filed under:
|
|

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

© Game Development or respective owner

Related posts about java

Related posts about lwjgl