Sign in to follow this  
Halostein

Array of an Object

Recommended Posts

Halostein    100
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); } }

Share this post


Link to post
Share on other sites
aryx    402
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[i] = new Player(); for every entry i in the array so that everything is initialized.

Share this post


Link to post
Share on other sites
Halostein    100
OOOH. Yeah!! Ack.

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

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

OR

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

then
fox[i] = new Player();


Share this post


Link to post
Share on other sites
aryx    402
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] 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this