Results 1 to 7 of 7
- 05-24-2011, 12:18 PM #1
Member
- Join Date
- May 2011
- Posts
- 21
- Rep Power
- 0
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)
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 codeJava Code:public class A3JPanel extends JPanel implements KeyListener, ActionListener { private Ball m_ball = 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...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); } }
A3JPanel.java:107: cannot find symbol
symbol : method move()
location: class Ball[]
m_ball.move(); // Move the ball.
- 05-24-2011, 12:24 PM #2
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
- 05-24-2011, 12:40 PM #3
Member
- Join Date
- May 2011
- Posts
- 21
- Rep Power
- 0
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..
- 05-24-2011, 12:50 PM #4
You're trying to move the array, not the balls within it.
- 05-24-2011, 12:51 PM #5
Member
- Join Date
- May 2011
- Location
- Herentals, Belgium
- Posts
- 14
- Rep Power
- 0
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:
- 05-24-2011, 01:59 PM #6
Member
- Join Date
- May 2011
- Posts
- 21
- Rep Power
- 0
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...
- 05-24-2011, 02:24 PM #7
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
-
Single to multi-dimension array
By Migy in forum New To JavaReplies: 2Last Post: 04-05-2011, 06:12 PM -
How do I modify the value of a single element in an array?
By OriginalKopy in forum New To JavaReplies: 1Last Post: 02-14-2011, 06:47 PM -
how to change a single index of a string
By ftrengnr in forum New To JavaReplies: 5Last Post: 11-26-2010, 04:08 AM -
array to single char
By rfviki in forum New To JavaReplies: 5Last Post: 11-04-2010, 02:58 PM -
variable to accept a single object
By Rgfirefly24 in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks