Android Game Development Summary 1

Source: Internet
Author: User

I recently developed a shooting game that has encountered and solved some difficulties during the development process. I would like to share with you here, the level is not high. I hope you will not be surprised .........

1. Repeated image loading during Game Development

When we click play to run the game, the most annoying thing is that the game gets stuck. This is what we usually call the game not smooth enough. One reason for this problem is that the Code executed during game operation occupies too much memory, such as creating objects and repeatedly loading game images or sound resources. For example, if you need to issue a bullet to your host, we will take it for granted that you should create a bullet class and then create objects with no direction or different movement speeds after you click the send bullet button, then, draw a picture of the bullet object to the screen. For example, the following class:

Package org. loon. Test. Barrage;

Import java. util. List;
Import org. loon. Framework. Android. Game. Action. Map. shapes. rectbox;
Import org. loon. Framework. Android. Game. Core. lsystem;
Import org. loon. Framework. Android. Game. Core. Graphics. device. lgraphics;
Import org. loon. Framework. Android. Game. Core. Timer. ltimer;
Import org. loon. Framework. Android. Game. utils. graphicsutils;
Import Android. Graphics. Bitmap;
Import Android. Graphics. matrix;

Public class ball extends barrage {
// The bitmap object of the bullet in the main battle machine
Bitmap image;
// Set the type of the bullet
Int type;
Double V;
Int index;
Ltimer timer;
Private rectbox rect;
Matrix matrix;
Public ball (INT type, double direction ){
This. x = mybarrage. SPR. getmiddlepoint (). getx ();
This. Y = mybarrage. SPR. getmiddlepoint (). Gety ();
This. V = direction;
This. type = type;
Timer = new ltimer (300 );
Matrix = new matrix ();
Switch (type ){
Case 0:
Image = graphicsutils. LoadImage ("Res/main_bullets/common_bullet.jpg", false). getbitmap ();
// Image = loadimages. ball_type_0;
This. Life = 2;
This. Power = 1;
Break;
Case 1:
Image = graphicsutils. LoadImage ("Res/main_bullets/skill_bullet.jpg", false). getbitmap ();
// Image = loadimages. ball_type_1;
This. Life = 5;
This. Power = 4;
Break;
}
Matrix. postrotate (float) Direction );
Image = bitmap. createbitmap (image, 0, 0, image. getwidth (), image. getheight (), matrix, false );
This. width = image. getwidth ();
This. Height = image. getheight ();
}

Public Boolean iskilled (){
If (this. Remove ){
Return true;
}
If (this. Life <= 0 ){
This. Remove = true;
Return true;
}
Else if (x <0) | (x> = lsystem. getsystemhandler (). getwidth ())){
This. Remove = true;
Return true;
}
Else if (Y <0) | (Y> lsystem. getsystemhandler (). getheight ())){
This. Remove = true;
Return true;
}
Return false;
}
// Draw a bullet to the screen
Public void draw (lgraphics g ){

Switch (type ){
Case 0:
G. drawbitmap (image, (INT) x-image. getwidth ()/2, (INT) Y-image. getheight ()/2 );
Break;
Case 1:
G. drawbitmap (image, (INT) x-image. getwidth ()/2, (INT) Y-image. getheight ()/2 );
Break;
}
}

// Update the coordinates of bullets at any time
Public void Update (list <barrage> attacks, int PX, int Py ){
Double rad = (math. PI/180) * V;
Switch (type ){
Case 0:
X = math. Cos (RAD) * 10 + X;
Y = math. Sin (RAD) * 10 + Y;
Break;
Case 1:
If (timer. Action (30 )){
If (index <7 ){
Index ++;
} Else {
Index = 0;
}
}
X = math. Cos (RAD) * 5 + X;
Y = math. Sin (RAD) * 5 + Y;
Break;
}
}
// Collision Detection Method
Public Boolean checkcollision (double Tx, double ty, int W, int h ){
If (rect = NULL ){
Rect = new rectbox (INT) x-This. width/2, (INT) Y-This. Height/2, this. Width, this. Height );
} Else {
Rect. setbounds (INT) x-This. width/2, (INT) Y-This. Height/2, this. Width, this. Height );
}
Return rect. intersects (INT) TX-W/2, (INT) ty-H/2, W, h) | rect. contains (INT) Tx, (INT) Ty );
}
}
There are two lines of red code in this class. If new is used to create an object when a bullet is sent, the two lines of red code for loading image resources will be executed all the time, in this way, the memory resources of the Android mobile phone are quite consumed, so that the game will be very difficult in the main battle zone.

We can observe that once an image object is created in the red code, it will not change, so we can create a class to save the object that loads the image, then, when such an object is used, you can directly call an object that has already been generated. Therefore, we create the following class to create an object for image resources:

Package org. loon. Test. Barrage;

Import org. loon. Framework. Android. Game. Core. Graphics. limage;
Import org. loon. Framework. Android. Game. utils. graphicsutils;

Import Android. Graphics. Bitmap;

Public class loadimages {
Static bitmap ball_type_0;
Static bitmap ball_type_1;
Static bitmap enemyball_type_0;
Static bitmap enemyball_type_1;
Static bitmap enemyball_type_2;
Static bitmap enemyball_type_3;
Static bitmap tracker;
Static limage enemya_type_0;
Static limage enemya_type_1;
Static limage stones [];
Public void initialize (){
Ball_type_0 = This. loadbils (0 );
Ball_type_1 = This. loadbils (1 );
Enemyball_type_0 = This. loadenemybils (0 );
Enemyball_type_1 = This. loadenemybils (1 );
Enemyball_type_2 = This. loadenemybils (2 );
Enemyball_type_3 = This. loadenemybils (3 );
Tracker = This. loadtrackers ();
Enemya_type_0 = This. loadenemya (0 );
Enemya_type_1 = This. loadenemya (1 );
Stones = This. loadstones ();
}
Private bitmap loadbils (INT type ){
Bitmap bitmap = NULL;
If (type = 0 ){
Bitmap = graphicsutils. LoadImage ("Res/main_bullets/common_bullet.jpg", false). getbitmap ();
}
Else if (type = 1 ){
Bitmap = graphicsutils. LoadImage ("Res/main_bullets/skill_bullet.jpg", false). getbitmap ();
}
Return bitmap;
}
Private bitmap loadenemybils (INT type ){
Bitmap bitmap = NULL;
Switch (type ){
Case 0: // small enemy plane tracking bullets
Bitmap = graphicsutils. LoadImage ("Res/enemy_bils/common_bullet.png", false). getbitmap ();
Break;
Case 1: // boss tracking bullets
Bitmap = graphicsutils. LoadImage ("Res/enemy_bils/tracker_bullet.png", false). getbitmap ();
Break;
Case 2: // The Boss is surrounded by bullets
Bitmap = graphicsutils. LoadImage ("Res/enemy_bils/boss_bullet_1.png", false). getbitmap ();
Break;
Case 3: bitmap = graphicsutils. LoadImage ("Res/enemy_bils/boss_bullet_2.jpg", false). getbitmap ();
Break;
}
Return bitmap;
}
Private bitmap loadtrackers (){
Bitmap bitmap = NULL;
Bitmap = graphicsutils. LoadImage ("Res/enemys/tracker.png", false). getbitmap ();
Return bitmap;
}
Private limage loadenemya (INT type ){
Limage image = NULL;
Switch (type ){
Case 0:
Image = graphicsutils. LoadImage ("Res/enemys/common_plane.png", false );
Break;
Case 1:
Image = graphicsutils. LoadImage ("Res/enemys/bigger_plane.png", false );
Break;
}

Return image;

}
Private limage [] loadstones (){
Limage image [] = NULL;
Image = graphicsutils. getsplitimages ("Res/enemys/stones.png", 32, 32, false );
Return image;
}
}

In this way, we can replace the red code with the blue annotation code to avoid repeated image loading. At that time, place new LoadImage (). initialize (); in the program segment loaded by the game to initialize all objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.