Results 1 to 6 of 6
  1. #1
    viper110110 is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default [SOLVED] Class array

    hey guys, I am working on a Brick Breaker game (hit the ball with the paddle to break bricks)

    I have made a Brick class, however I would like to use it as an array

    How do I do this?

    The Brick class:
    Java Code:
    import java.awt.*;
    
    public class Brick
    {
        static int state = 1;
        static boolean alive = false;
    }
    The line I am using and need to modify to make an array:
    Java Code:
    static Brick brick = new Brick();
    I would like to reference it like:
    Java Code:
    if (brick[i].alive = true)
    so that I have an array of 100 bricks, each with their own alive and state variables (and any others I add)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I strongly advise you to make Brick a true OOP class and make the variables non-static instance variables. Otherwise if you change alive for one brick, you change them for all. This way it is simple to create an array of x number of Bricks.

    I also recommend making alive private and use public getters / setters here though others may argue that for a small simple class this isn't as important as we make it out to be.

  3. #3
    viper110110 is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    but syntax wise, how do I make it an array?

    I changed the variables in the class to public like you suggested

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To create an array of Brick you do it like an array of any other object:
    Java Code:
    int brickCount = 100;
    Brick[] bricks = new Brick[brickCount];
    for (int i = 0; i < bricks.length; i++)
    {
      // don't forget to construct each brick element
      bricks[i] = new Brick();
    }

  5. #5
    viper110110 is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    Thanks, it works now

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. writing array values to another class
    By ronald christian in forum New To Java
    Replies: 27
    Last Post: 11-07-2008, 05:08 PM
  2. writing an array class
    By wardd85 in forum New To Java
    Replies: 5
    Last Post: 07-16-2008, 11:59 PM
  3. Replies: 0
    Last Post: 05-13-2008, 07:16 AM
  4. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 PM
  5. Replies: 0
    Last Post: 04-02-2008, 08:04 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
  •