Results 1 to 7 of 7
  1. #1
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default Wanting to change from a single object to an array.

    I had a singular object which referred to a class named "Ball"
    I had it like this originally (I will only show the relevant code)
    Java Code:
    public class A3JPanel extends JPanel implements KeyListener, ActionListener {
    	private Ball m_ball = new Ball(125, 0, 10, 10);
    I've been following some instructions and I now want to create several of these objects in an array manner. I've changed the following pieces of code

    Java Code:
    public class A3JPanel extends JPanel implements KeyListener, ActionListener {
    	private Ball[] m_ball;
    
            public A3JPanel() {
    		m_ball = new Ball[6];
    		for (int i = 0; i < m_ball.length; i++) {
    			m_ball[i] = new Ball(125, 0, 10, 10);
    		}
    	}
    However I have some methods in the Ball.java file which now no longer execute correctly and I'm wondering what I have to do to amend this...

    A3JPanel.java:107: cannot find symbol
    symbol : method move()
    location: class Ball[]
    m_ball.move(); // Move the ball.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    The variable m_ball refers to the array. An array doesn't have a method move().

    Recommended reading: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    db

  3. #3
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    In the class I created, I made my own move method which simply changes some int variables.

    Thanks, I read over it. I know the basics of an array but can't figure this one out..

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    You're trying to move the array, not the balls within it.

  5. #5
    Join Date
    May 2011
    Location
    Herentals, Belgium
    Posts
    14
    Rep Power
    0

    Default

    Try m_ball[1].move()... This refers to a Ball object in the array, where m_ball just refers to an array and as DarrylBurke said: an array doenst have a method called move().

    so recap:
    m_ball refers to array -> doenst have method move()
    m_ball[1] refers to the second Ball object in array of Balls -> should have method move()

    Hope this is correct and helps you!!

    Peace!
    Enjoy your milk!! :cool:

  6. #6
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    Cheers nature! I'm trying to implement the balls appearing from random locations around the window but the use of Math.random doesn't seem possible due to a double being required. Have tried changing everything to double but the "fillOval" method doesn't seem to accept doubles...

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    You could use java.util.Random
    Random (Java Platform SE 6)

    Java Code:
    Random generator = new Random();
    int x = generator.nextInt(5);
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Single to multi-dimension array
    By Migy in forum New To Java
    Replies: 2
    Last Post: 04-05-2011, 06:12 PM
  2. How do I modify the value of a single element in an array?
    By OriginalKopy in forum New To Java
    Replies: 1
    Last Post: 02-14-2011, 06:47 PM
  3. how to change a single index of a string
    By ftrengnr in forum New To Java
    Replies: 5
    Last Post: 11-26-2010, 04:08 AM
  4. array to single char
    By rfviki in forum New To Java
    Replies: 5
    Last Post: 11-04-2010, 02:58 PM
  5. variable to accept a single object
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:41 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
  •