Array of an Object

Started by
3 comments, last by Halostein 14 years, 1 month ago
Hi guys. I'm actually making this game, and everything works fine. However, when I decided to make this player-controlled object (fox) an array, the fox didn't show anymore (including its methods). I initialized the fox first to be an array of 1, then I tried to access the fox[0]'s methods, but it won't. However, when I turn it back as a normal object (not an array anymore), it works. public class Main extends Applet implements Runnable { // variables private Thread th; private Player fox[]; // distance constants that refers to how far a fox can go every turn. private final int JumpLeft = -30; private final int JumpRight = 30; private final int JumpUp = -30; private final int JumpDown = 30; // booleans that triggers if the key button is pressed private boolean BoolJumpLeft; private boolean BoolJumpRight; private boolean BoolJumpUp; private boolean BoolJumpDown; // double buffering private Image meadow; private Image foximg; private Image rabimg; private Image dbImage; private Graphics dbg; public void init() { setBackground (Color.white); fox = new Player[1] meadow = getImage(getCodeBase(), "mead.jpg"); foximg = getImage(getCodeBase(), "fox.jpg"); } public void start () { th = new Thread(this); th.start (); } public void stop() { th.stop(); } public void destroy() { th.stop(); } public void run () { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (true) { // move player if(BoolJumpLeft) { fox[0].moveX(JumpLeft); } else if(BoolJumpRight) { fox[0].moveX(JumpRight); } else if(BoolJumpUp) { fox[0].moveY(JumpUp); } else if(BoolJumpDown) { fox[0].moveY(JumpDown); } repaint(); try { Thread.sleep(100); } catch (InterruptedException ex) { } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public boolean keyDown(Event e, int key) { if(key == Event.LEFT) { BoolJumpLeft = true; } else if(key == Event.RIGHT) { BoolJumpRight = true; } else if(key == Event.UP) { BoolJumpUp = true; } else if(key == Event.DOWN) { BoolJumpDown = true; } return true; } public boolean keyUp(Event e, int key) { if(key == Event.LEFT) { BoolJumpLeft = false; } else if(key == Event.RIGHT) { BoolJumpRight = false; } else if(key == Event.UP) { BoolJumpUp = false; } else if(key == Event.DOWN) { BoolJumpDown = false; } return true; } public void update (Graphics g) { if (dbImage == null) { dbImage = createImage (this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics (); } dbg.setColor (getBackground ()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); dbg.setColor (getForeground()); paint (dbg); g.drawImage (dbImage, 0, 0, this); } public void paint (Graphics g) { // draw background g.drawImage (meadow, 0, 0, this); for (int x=0; x<=300; x+=30) { //g.setColor(Color.blue); g.drawLine (x,0, x,300); } // Draw the horizontal lines of the grid: for (int y=0; y<=300; y+=30) { //g.setColor(Color.red); g.drawLine (0,y, 300,y); } // draw player fox[0].drawPlayer(g); g.drawImage (foximg, fox[0].getX(), fox[0].getY(), this); } }
Advertisement
The code fox = new Player[1]; only sets up the array, but doesn't initialize any of the elements in that array. You're probably seeing this as null pointer exceptions. You need to also do fox = new Player(); for every entry i in the array so that everything is initialized.
OOOH. Yeah!! Ack.

So, should I retain this code:
fox = new Player[1];

then add this code?
fox = new Player();

OR

in my declaration of variables:
private Player fox[1];

then
fox = new Player();


Your old code is fine, just that you need to initialize things after you create the array (like in your first example). It looks like this is Java, and Java won't allow you to declare arrays like you did in your second example (i.e., with the size specified at the declaration level).

Just to be extra clear, when I said fox I intended for you to replace i with whatever indicies as necessary. Something of this nature:

MyPlayerClass [] players = new MyPlayerClass[10];for(int index = 0; index < players.length; ++index)    players[index] = new MyPlayerClass();

Since you currently only have one item, just going fox[0] = new Player(); would be fine.

P.S. For future posts, you can put [ source ][ /source ] tags (without the spaces) around your code to get the nice syntax highlighted + scrolling code boxes, like I used above.
Okay, thanks! It finally worked!

This topic is closed to new replies.

Advertisement