I'm attempting to make a simple game of Pairs for Android.
Program Structure:
Menu.java (Menu activity initially loaded)
Game.java (Game activity, started by Menu)
GameThread.java (Handles gameloop, calls render process in GameView)
GameView.java (Handles all drawing to the screen)
Graphics.java (Stores loaded images)
The Problem:
The game features 15 different types of card, each of which requires around 14 frames for animation (flipping, destroying, etc). I'm currently reading these off a PNG spritesheet, and then chopping them into a Bitmap array (Bitmap[15][14]) using the following code:
for (int i=0; i<15; i++) {
for (int j=0; j<14; j++) {
card[i][j] = Bitmap.createBitmap(spriteSheet,
j*cardWidth, i*cardHeight, cardWidth, cardHeight);
}
}
The problem arises when I initially load the GameView, the card graphics need to be loaded, which seems to take around 2 seconds to process (resulting in an unresponsive app).
Is there a better way I can do this?
Thanks for your help in advance.
If the problem is indeed that your app is unresponsive, I would recommend loading the images via a thread while showing the user an indeterminate dialog box. This is generally done via a Thread and a Handler. There are plenty of examples of how to do this online.
If however the problem is that of memory your going to have to figure out a different way to handle all the images. That could be loading only one animation at a time, resizing your bitmaps, or fixing a memory problem. There is no way to tell without seeing the errors and/or more code.
Finally, I see from your logic that you may have more Bitmaps in memory than necessarily are required. Once again I can't be certain without seeing more logic.