i have a Life class. which just prints a image of heart.

i want to have 6 lifes so iam adding 6 classes in arraylist. 1st heart one left. 2nd heart next to it.....

int life = 6;
//LIFE - create 6 heats and store in arraylist
lifeClass = new Life(0,0,10,10);    //create heart
lifeStore.add(lifeClass);          //store in array
lifeClass = new Life(20,0,10,10);   //create heart
lifeStore.add(lifeClass);          //store in arraylist
lifeClass = new Life(40,0,10,10);   //create heart
lifeStore.add(lifeClass);          //store in arraylist
lifeClass = new Life(60,0,10,10);    //create heart
lifeStore.add(lifeClass);          //store in array
lifeClass = new Life(80,0,10,10);    //create heart
lifeStore.add(lifeClass);          //store in array
lifeClass = new Life(100,0,10,10);    //create heart
lifeStore.add(lifeClass);          //store in array

les say i want to add a life.

lifeClass.setLife(lifeClass.getLife() + 1)  //add one to variable
lifeClass = new Life(/*help*/);    //create heart
lifeStore.add(lifeClass);          //store in array[/code]

in this code above. i am adding one to variable. than create one more heart and add to arraylist.

the problem is that [code]lifeClass = new Life(/help/);[/code] i cant hard code the x,y,width,height postion. bc i dont which x postion to add to it. it x could be 0, 20, 40, 80, 100.

Recommended Answers

All 4 Replies

It is important to be clear about the distinction between objects and classes. Life is a class. The new operator creates a new object, not a new class. lifeClass is misnamed because it is a reference to an object, not a class.

I won't pretend to understand the purpose of the Life class. The setLife and getLife methods are especially mysterious and don't seem to fit with your description of Life, but it's clear that your code is highly repetitive and you should fix that by using loops. If you create a method that fills lifeStore with any given number of Lifes, then that should make it easy to have as many Lifes as you like in lifeStore at any moment.

Please consider using loops, but to directly solve you problem, I think this would work:

lifeClass = new Life(lifeStore.size() * 20, 0, 10, 10);

That should work because x seems to always be 20 times the number of Lifes already in lifeStore.

works perfectly. thanks alot. just one question on ur comment.

so these are objects?

Life lifeObject = new Life();    //Life.java
Random rObject = new Random();
Color redObject = Color.red;
Color blackObject = new Color(0,0,0);

objects, instances, ... yup.

for instance:

Person me = new Person("Stultuske");
Person you = new Person("game06");

In the above lines of code, there's only one class: the Person class.
there are, on the other hand, two instances of it: me and you.

ah i see. thanks alot guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.