Archived

This topic is now archived and is closed to further replies.

laotzu

[java] Trouble with classes

Recommended Posts

I''m rather new to java and I''m having a problem with classes... Right now I''m trying to make a pacman game, the main class that gets called when the applet loads works fine, so then I tried to make a class for the ghosts. It works fine when I initialize just one instance, e.g. Ghost ghosts = new Ghost(); but when I try to initialize an array of four ghosts, the applet only shows a gray screen.. although it doesn''t display any errors I initialize the array of ghosts like this.. Ghost ghosts[] = new Ghost[4]; If you can think of any reason that could cause this please post. Thank you very much, Laotzu

Share this post


Link to post
Share on other sites
Remember that you need to initialize the objects in your array.

When you create the array with the line...

Ghost ghosts[] = new Ghost[4];

... you only create a place holder for 4 ghosts.

You should then have a loop or some other method of initializing the ghosts.

For example.

for (int loop = 0; loop < 4; loop++) {
ghosts[loop] = new Ghost();
}

This will create the Ghosts in your array.

I hope this helps.

borngamer

Share this post


Link to post
Share on other sites