Im not entirely sure if this is the right section of the forum to be in but i'll ask my question anyway.

I just started learning java and well object oriented code in general iv'e coded in languages before real basic stuff but never a object oriented program. I understand the basic concept of something like

Code:

Runner Test = new Runner(10,30);

To create a new object in the class Runner called Test with parameters filled in of 10 and 30.

However, how do i create multiple objects in my code by something such as a button press with a undefined amount of objects possible to be created. In my current situation i'm making a real simple game that will have the player press space to create a bullet. My issue being i can't use the above code to create a bullet object since if the player presses space more then twice the Test bullet will already have been created and i'll be writing over the first bullet made.

I have a guess that this will involve arrays or arraylists but i don't know how to use arrays in classes and I've never used a arraylist before all i know about it is it's a array with infinite parameters.

Any help would be nice thank you.

01-05-2011, 12:13 AM

quafflepunch

you need an array of your object

Code:

public void fireBullet(){
fireBlocks[] Bullet =new fireBlocks[];
}

//then for each bullet do

bullet[i]= new fireBlocks("Images/Bullet.GIF");

then everytime you press space increase the limit of the array

01-05-2011, 02:38 AM

patcc2

are you sure thats the only way to do it i feel like an arraylist would make this easier?

also if i have a class mainBlocks that has code that builds two classes of coloredBlocks called p1 and p2... then inside of the coloredBlocks class i have the code to create bullets... Inside the main class if i want to call on blocks would it be something like p1.Bullet[i] and p2.Bullet[i] or would the code be looking for a array inside of the coloredBlocks class called Bullet?

//update the images and then paint them in a image for the next paint call
update();
jim.draw(g);
fred.draw(g);

}

public void update(){

//Move the players up and down
if (u==true){
jim.moveUp();
}
if (d==true){
jim.moveDown();
}
if (l==true){
fred.moveDown();
}
if (r==true){
fred.moveUp();
}
//ensure rapid fire does not happen as well as construct bullets (construction part does not work
if (space==true && spaceHeld!=1){
spaceHeld=1;
jim.fireBullet();
}
if (space==false){
spaceHeld=0;
}
if (enter==true && enterHeld!=1){
enterHeld=1;
jim.fireBullet();
}
if (enter==false){
enterHeld=0;
}
}

that is the main controller class that does the logic and the drawing. my launch code is commented out right now since I placed launched inside the init just had two different ideas left them both in for now.

Code:

import java.awt.*;

public class Blocks {

int x,y,i;
Image img;

public Blocks(int x,int y){
this.x=x;
this.y=y;
}

//constructor that also sets the image for the object
public Blocks(int x,int y, String s){
this.x=x;
this.y=y;
img=Toolkit.getDefaultToolkit().getImage(s);
}

this is my code for the blocks aka players themselves as you can see ive commented out some different concepts for making the fireblocks class since they had errors I can not remember what right now though sorry.

Not really sure what quafflepunch is talking about with limitless arrays (the code sample he gave that omits the size isn't valid java), but arrayLists are definitely the way to go when the number of elements is unknown.

Plain arrays are fine if you are storing a known number of things - say 4 frame of animation. But ArrayLists are Dynamic arrays and have expansion and a lot of other features built in. For example:

Thank you Quad64Bit I could have sworn I was right with the arraylist thing.

Now then as I stated before I have never used array lists before so it would be nice if you explained some stuff.

1.ArrayList<fireBlocks> blocks = new ArrayList<fireBlocks>();
what does it mean when you are doing the <fireBlocks> thing I understand it would work but I would like to understand what it exactly it is doing.

2.I have seen the for loop you used in method2 for(fireBlocks fb : blocks){ however I have only worked with a normal for loop before I am not sure how that kind of a for loop works at all again explaining would be nice.

01-11-2011, 03:11 AM

user0

hi - I am not sure how your game code works. But I can help you with the ArrayList question. So if you are familiar with arrays, and say you wanted to create an array of 10 Strings and an array of 10 fireBlocks, you would do it as follows:

ArrayList<fireBlocks> is simply a type specification, meaning you are creating an ArrayList of fireBlocks.

Hope that helps you understand.

Best,

01-11-2011, 03:26 AM

user0

Quote:

2.I have seen the for loop you used in method2 for(fireBlocks fb : blocks){ however I have only worked with a normal for loop before I am not sure how that kind of a for loop works at all again explaining would be nice.

Quad is using what is called a For Each loop. This type of loop can be used when you want to access each element in your list and do something with it, regardless of its position in the list (note that the for each loop does not use an index variable to move through your arraylist). As an example, lets say you have an array ints[] which has 10 integers, you would write a normal for loop as follows:

Code:

for(int i = 0, i < ints.length; i++) {
do something with each element;
}

The same thing can be done with a for each loop, however, you give up the ability to know exactly where in your array a particular element resides for example. So I can rewrite the above for loop using a for each loop:

Code:

for(int x: ints) {
do something with x;
}

This simply reads as: for each int element x in the array ints, do something with x.

here is a link to some tutorials on for each loops and ArrayList API for you to refer to:

Great comments user0!
Also to add to what he said about type specification - in java <TypeHere> is called 'Generics'. Since data structures like ArrayList normally store only Objects, in the old days before generics, it could get messy. Generics allow you to not only enforce what types of objects are stored in a collection, but also relieves you of the task of casting the stored object back into its original data type when its retrieved.

Its also to be noted that Arrays are a language feature:

Code:

SomeType[] array = new SomeType[10];

and that ArrayLists are objects written in java and contained in the class library that have a collection of methods you can use. Their primary feature is dynamic expansion, arbitrary maximum capacity, insertions and removals to name a few.