How to use an ArrayList here?
Hey guys. I wanted to see if someone could help me here. I have the following piece of code that which displays one asteroid on the screen.
Code :
// draw asteroid
if(!status.isNewAsteroid())
{
// draw the asteroid until it reaches the bottom of the screen
if(asteroid.getY() + asteroid.getSpeed() < this.getHeight())
{
asteroid.translate(0, asteroid.getSpeed());
// Displays one asteroid on the screen:
graphicsMan.drawAsteroid(asteroid, g2d, this);
}
else
{
asteroid.setLocation(rand.nextInt(getWidth() - asteroid.width), 0);
}
}
Image of game running with only one asteroid on the screen:
http://i1226.photobucket.com/albums/...dSpaceHelp.jpg
I want to draw more than one asteroid on the screen at the same time. How do I do this? Do I use an Array List? I think I have to use an Enhanced For loop but I am not too sure.
P.S. The new asteroid obviously can't be just a modification of the current asteroid with modified x and y coordinates. It has to be a brand new asteroid of its own of a class Asteroid which I also have and it has to behave differently from the current asteroid. That is why I want to solve the problem with an array list.
Thank you all very much.
Re: How to use an ArrayList here?
If you put the items to be drawn in an arraylist, the paintComponent() method could go through the list using a loop and call each item's draw method so it could draw itself.
Re: How to use an ArrayList here?
hi,
i guess you need to have arraylist of asteroid object and asteroid should have some sort of mechanism (may be pseudo random generator for co-ordinates) so that asteroids have same behaviour but different states..
Re: How to use an ArrayList here?
Quote:
Originally Posted by
Raven123
hi,
i guess you need to have arraylist of asteroid object and asteroid should have some sort of mechanism (may be pseudo random generator for co-ordinates) so that asteroids have same behaviour but different states..
You are correct in the sense that the asteroids have a random generator in its class so that it always starts at a different location. This is the constructor of the Asteroid class:
Code :
public Asteroid(GameScreen screen)
{
this.setLocation(rand.nextInt(screen.getWidth() - asteroidWidth),0);
this.setSize(asteroidWidth, asteroidHeight);
}
What I try to do is to create one asteroid with the parameter GameScreen and then put it inside an Array or ArrayList using a loop. Like this:
Code :
this.asteroid = new Asteroid(screen);
for(int i = 0; i <= 2; i++)
{
Asteroid[] asteroids = new Asteroid[i];
asteroids[i] = this.asteroid;
}
I think that this array isn't working because it seems that I am putting the same asteroid inside every element of the array. How do I create an array that fills up with different asteroids and all of them have the parameter GameScreen?
Re: How to use an ArrayList here?
A problem is that the variable defined inside the loop: asteroids which goes away when execution leaves the loop.
Quote:
How do I create an array that fills up with different asteroids
A list of the steps the code needs to do:
create an array
begin loop
create a new Asteroid
add that new Asteroid to the array
end loop
Re: How to use an ArrayList here?
Quote:
Originally Posted by
Norm
A problem is that the variable defined inside the loop: asteroids which goes away when execution leaves the loop.
A list of the steps the code needs to do:
create an array
begin loop
create a new Asteroid
add that new Asteroid to the array
end loop
Do you suggest an array or an array list?
Re: How to use an ArrayList here?
Re: How to use an ArrayList here?
Quote:
Originally Posted by
Norm
An arraylist.
It worked. Thank you very much. I already gave you the rep :)
Re: How to use an ArrayList here?
Hello, this seems a little related topic for my Q. I am a Java beginner and would like to confirm answer to this -
Quesiton:
CAN WE DO THIS -
if (myClassList instanceof List<MyClass>)
Thanks
Re: How to use an ArrayList here?
Give it to a compiler and see what it says.
You'll get an answer much faster and more correct from the compiler.