Results 1 to 5 of 5
  1. #1
    pumpbreaks is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Issues with Populating an Arraylist within a blockbreaker game

    hello everyone!

    I am studying computer science in uni and i have been given a block breaking game assignment, i have got the ball to move and bounce off each wall, bounce off the paddle, move the paddle, lose a life and game over. This was with lots of help from my tutor.

    I need to write a nested for loop to populate an array list with bricks and I've been looking everywhere how to do this. I just can't get my head around this problem and i feel like I've exhausted every option. I'm hoping you guys can help me progress and work out how to get this done as I'm panicking a little bit as it's due in on Wednesday and my head feels totally blagged.



    i have a class called ArrayBrick:
    Java Code:
    package blockbreakerextreme;
    
    import java.util.ArrayList;
    
    /**
     *
     * @author 1097
     */
    public class ArrayBrick {
        int index = 0;
        Brick brick;
    
    
    
    ArrayList<Brick> allBrick = new ArrayList<Brick>();
    
    
    public int getIndex(){
    return index;
        }
    
    
    public void add(Brick brick) { 
            allBrick.add(brick);
        }
    
    
        public Brick getBrick(int index) { 
            brick = allBrick.get(index);
            return brick;
        }
    
    
    
          public int getAllBricks() { 
            return allBrick.size();
        }
    
          public void remove(int index) {
             allBrick.remove(index);
          }
    
    }
    in my game panel i have created a new instance of ArrayBrick and brick and the arraylist, (i have one brick just drawn to prove the brick would show)

    i need to write a method to populate the array list with bricks this is what i have in PanelGame

    the bricks need to be spaced 10 by 3

    Java Code:
        
        
        static Brick brick;
        static ArrayBrick array;
        ArrayList<Brick> allBrick = new ArrayList<Brick>();
    
     public void loadBricks() {
            //this is where i am stuck
        }
    i hope you guys can help me out,

    thanks in advance
    pumpbreaks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Java Code:
        static ArrayBrick array;
        ArrayList<Brick> allBrick = new ArrayList<Brick>();
    Your ArrayBrick already has an ArrayList<Brick> in it, so what is this other one for?

  3. #3
    pumpbreaks is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    I have gotten confused, i dont need that in there. as i said i am not very good.

    thanks for your reply

  4. #4
    pumpbreaks is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    this is what i have so far


    public void loadBricks() {
    for (int i = 0; i < 30;i++){
    array.add(brick);

    }
    }

    this just adds 30 bricks to the array

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    this just adds 30 bricks to the array
    Not really.
    It adds a reference to the same brick 30 times. There is only one brick with 30 references to it.
    To get get 30 different bricks, you need to create a new brick every time before adding it to the array list.

Similar Threads

  1. Issues looping and breaking in Pigs Dice Game
    By clown2921 in forum New To Java
    Replies: 4
    Last Post: 01-04-2014, 11:43 AM
  2. Key Binding issues in game
    By Fortu in forum New To Java
    Replies: 2
    Last Post: 01-17-2011, 12:21 AM
  3. Issues populating MySQL tables
    By Randy_Dennis in forum JDBC
    Replies: 2
    Last Post: 01-12-2011, 08:49 AM
  4. populating a JList
    By hariza in forum AWT / Swing
    Replies: 2
    Last Post: 09-30-2010, 03:53 AM
  5. Populating a JTable
    By toymachiner62 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •