3 Replies - 191 Views - Last Post: 07 April 2013 - 12:26 AM Rate Topic: -----

#1 game06  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 21-March 13

adding classes

Posted 06 April 2013 - 09:04 PM

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
lifeClass = new Life(/*help*/);
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.

Is This A Good Question/Topic? 0
  • +

Replies To: adding classes

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Re: adding classes

Posted 06 April 2013 - 10:07 PM

From what I've seen, there is a pattern.
lifeStore.add(new Life(20 * i ,0,10,10));



You could try writing a loop out of this.
Was This Post Helpful? 1
  • +
  • -

#3 game06  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 21-March 13

Re: adding classes

Posted 06 April 2013 - 10:28 PM

o wow thanks, i did not see that pattern.

//LIFE - create 6 heats and store in arraylist
for(int i = 0; i < 6; i++){
	lifeClass = new Life(20 * i, 0, 10, 10);
	lifeStore.add(lifeClass);
}


just one question on this topic. in different class i want to add a another heart. note player could have 4, 2, or 6 hearts. if player has 3 hearts, than 4 heart should go after 3rd heart. so i need to get the x postion of 3rd heart.

the way i am thinking was this: but this is not adding.
for(int i = 0; i < lifeStore.size(); i++)   //loop though arraylist
{
    Life lifeClass = lifStore.get(i); 
    if(lifeClass == null)    
    {
     i--;
      Life  lifeClass2 = new Life(20*i, 0, 10, 10);
      lifeStore.add(lifeClass2);
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 game06  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 21-March 13

Re: adding classes

Posted 07 April 2013 - 12:26 AM

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



got it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1