Libgdx screen Switching

Source: Internet
Author: User
Tags libgdx

We usually divide the development into multiple screens to implement different functions, such:

Menuscreen is used to display menus, gamescreen main game interface, and helpscreen game help interface.

You can directly use game. setscreen to switch the scenario. However, this is a little stiff. In addition, there may be too much instantaneous memory usage in terms of memory usage, which may cause the game to exit or something.

Similar to cocos2dx, A screenone performs the following process during the screentwo switchover:

1. screenone is running. It has loaded resources.

2. instantiate screentwo and load the resources of screentwo (At this time, the resources of one and two are in the memory.)

3. Switch the display to screentwo, that is, game. setscreen (screentwo)

4. screenone destroys and releases its resources.

If the two screens load a lot of resources, the program is prone to problems.

For example, IOS limits the maximum memory size of a single app to 10 MB (I forgot the actual memory size, for example, please refer to the document), while screenone and screentwo occupy 8 Mb respectively, and there will be 16 Mb in the instant of switching, now there is a problem.

 

When I was playing 2dx in the past, my buddy told me one way: Add a transitional dedicated screen in the middle. Of course, it should be very small.

Then the switchover process becomes like this:

1. screenone is running. It has loaded resources.

2. instantiate transscreen and load transscreen resources. Of course, you do not need resources. It is a black screen.

3. Switch the display to transscreen, that is, game. setscreen (transscreen)

4. screenone destroys and releases its resources.

5. instantiate screentwo and load the resources of screentwo.

6. Switch the display to screentwo, that is, game. setscreen (screentwo)

7. transscreen destroys and releases its resources.

 

In this way, a large amount of resource usage is staggered.

Code on

1 public class screentrans implements screen {2 Private screentype next; 3 private game; // directly play the game when you need to switch the scenario. setscreen 4 private stage; 5 Private Static final logger = new logger (screentrans. class. getname (), application. log_debug); 6 Private Static final float duration = 0.5f; 7 private texture logotexture; 8 private skin; 9 private progressbar bar; 10 private gametimer timer; 11 12 public Enum screentype {13 screen_one, screen_two 14} 15 16 public screentrans (game, screentype next) {17 logger. debug ("init"); 18 this. game = game; 19 this. next = next; 20 21 stage = new stage (); 22 // GDX. input. setinputprocessor (null); 23 24 logotexture = new texture (GDX. files. internal ("badlogic.jpg"); 25 image logo = new image (logotexture); 26 logo. setcenterposition (GDX. graphics. getwidth ()/2, GDX. graphics. getheight ()/2); 27 stage. addactor (logo); 28 29 Skin = new skin (); 30 pixmap = new pixmap (1, 1, pixmap. format. rgba8888); 31 pixmap. setcolor (color. white); 32 pixmap. fill (); 33 skin. add ("white", new texture (pixmap); 34 35 progressbar. progressbarstyle style = new progressbar. progressbarstyle (); 36 style. knob = Skin. newdrawable ("white", color. green); 37 style. knobbefore = Skin. newdrawable ("white", color. green); 38 style. knobafter = Skin. newdrawable ("white", color. gray); 39 style. background = Skin. newdrawable ("white", color. red); 40 41 style. knob. setminheight (10); 42 style. knobbefore. setminheight (10); 43 style. knobafter. setminheight (10); 44 45 bar = new progressbar (0,100, 1, false, style); 46 bar. setwidth (logo. getwidth (); 47 bar. setcenterposition (GDX. graphics. getwidth ()/2, (GDX. graphics. getheight ()-logo. getheight ()/2-5); 48 stage. addactor (bar); 49 50 timer = new gametimer (duration); 51} 52 53 @ override 54 public void render (float delta) {55 GDX. GL. glclearcolor (0, 0, 0, 1); 56 GDX. GL. glclear (gl1_gl _ color_buffer_bit); 57 58 float d = commonutils. getdelta (DELTA); 59 timer. update (d); 60 if (timer. isfinished () {61 This. trans (); 62} 63 bar. setvalue (timer. getlivetime () * 100/duration); 64 stage. act (DELTA); 65 stage. draw (); 66} 67 68 private void trans () {69 screen nextscreen = NULL; 70 switch (next) {71 case screen_one: 72 nextscreen = new screenone (this. game); 73 break; 74 case screen_two: 75 nextscreen = new screentwo (this. game); 76 break; 77} 78 This. game. setscreen (nextscreen); 79 This. dispose (); 80} 81 82 @ override 83 public void resize (INT width, int height) {84 stage. getviewport (). update (width, height, true); 85} 86 87 @ override 88 public void show () {89 logger. debug ("show"); 90} 91 92 @ override 93 public void hide () {94 95} 96 97 @ override 98 public void pause () {99 100} 101 102 @ override103 public void resume () {104 105} 106 107 @ override108 public void dispose () {109 logger. debugging ("dispose"); 110 stage. depose (); 111 logotexture. dispose (); 112 skin. dispose (); 113} 114}

In the above Code, the enumerated scenario type is used to identify the next step. Of course, you can also use Int or a more advanced class. <? Extends screen>, and then use class. newinstance.

Gametimer is a very simple timer.

I added a false loading progress bar to this transition screen. Of course it is false. At this time, the next screen has not been instantiated yet. It is actually a timer =. =

There is also an image. I joined this process to see if the switchover was hard.

The following are:

 

The following are debugging information:

Com. Tide. g2d. screeneffect. Two. screenone: init
Com. Tide. g2d. screeneffect. Two. screenone: Show
Com. Tide. g2d. screeneffect. screentrans: init
Com. Tide. g2d. screeneffect. screentrans: Show
Com. Tide. g2d. screeneffect. Two. screenone: dispose

----- A transitional screen is displayed.
Com. Tide. g2d. screeneffect. Two. screentwo: init
Com. Tide. g2d. screeneffect. Two. screentwo: Show
Com. Tide. g2d. screeneffect. screentrans: dispose
Com. Tide. g2d. screeneffect. screentrans: init
Com. Tide. g2d. screeneffect. screentrans: Show
Com. Tide. g2d. screeneffect. Two. screentwo: dispose

----- A transitional screen is displayed.
Com. Tide. g2d. screeneffect. Two. screenone: init
Com. Tide. g2d. screeneffect. Two. screenone: Show
Com. Tide. g2d. screeneffect. screentrans: dispose
Com. Tide. g2d. screeneffect. screentrans: init
Com. Tide. g2d. screeneffect. screentrans: Show
Com. Tide. g2d. screeneffect. Two. screenone: dispose

----- A transitional screen is displayed.
Com. Tide. g2d. screeneffect. Two. screentwo: init
Com. Tide. g2d. screeneffect. Two. screentwo: Show
Com. Tide. g2d. screeneffect. screentrans: dispose

 

Libgdx screen Switching

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.