Use of the android game development framework libgdx (10)-dual stage

Source: Internet
Author: User
Tags libgdx

The most common game screens are the addition of a series of user interaction roles and components to a background with few changes. You can also create a group for the background to facilitate management.

But sometimes I don't think of this problem when writing, or the background is not just an image or something. The background and role have some mixed logic distributed in two stages.

It is too troublesome for me to rewrite it. I think spritbatch is used to draw it out. It's a big deal to use multiple cameras on the dual stage. Try it now.

Let's take a look at the draw method of stage:

 
/**Renders the stage*/ 
Public VoidDraw (){
Camera. Update ();
If(! Root. Visible)Return;
Batch. setprojectionmatrix (camera. Combined );
Batch. Begin ();
Root. Draw (batch, 1 );
Batch. End ();
}

 

The two stages can be shared in batch. Use stage (width, height, stretch, batch) to instantiate the second stage.

CodeAs follows:

Package Com. cnblogs. htynkn. game;

Import Com. badlogic. GDX. applicationlistener;
Import Com. badlogic. GDX. GDX;
Import Com. badlogic. GDX. inputprocessor;
Import Com. badlogic. GDX. Graphics. gl10;
Import Com. badlogic. GDX. Graphics. texture;
Import Com. badlogic. GDX. Graphics. g2d. textureregion;
Import Com. badlogic. GDX. Scenes. scene2d. stage;
Import Com. badlogic. GDX. Scenes. scene2d. UI. image;

Public Class Javagame Implements Applicationlistener {

Stage stage1;
Stage stage2;
Float Width;
Float Height;

@ Override
Public Void Create (){
Width = GDX. Graphics. getwidth ();
Height = GDX. Graphics. getheight ();
Stage1 = New Stage (width, height, True );
Stage2 = New Stage (width, height, True , Stage1.getspritebatch ());
Image image = New Image ( New Textureregion ( New Texture (GDX. Files
. Internal ("img/sky.jpg"), 50, 50,480,320 ));
Stage1.addactor (image );
Image image2 = New Image ( New Textureregion ( New Texture (GDX. Files
. Internal ("img/baihu.png"), 217,157 ));
Image2.x = (width-image2.width)/2;
Image2.y = (height-image2.height)/2;
Stage2.addactor (image2 );
}

@ Override
Public Void Dispose (){
// Todo auto-generated method stub

}

@ Override
Public Void Pause (){
// Todo auto-generated method stub

}

@ Override
Public Void Render (){
GDX. gl. glclear (gl10.gl _ color_buffer_bit );
Stage1.act (GDX. Graphics. getdeltatime ());
Stage2.act (GDX. Graphics. getdeltatime ());
Stage1.draw ();
Stage2.draw ();
}

@ Override
Public Void Resize ( Int Width, Int Height ){
// Todo auto-generated method stub

}

@ Override
Public Void Resume (){
// Todo auto-generated method stub

}
}

Effect:

If you want to improve efficiency, you can consider modifying the buffer size of spritbatch.

Note that the background stage should be drawn first, and other components should be drawn later. Otherwise, the effect will be:

Input control of the stage cannot be used simply:

 
GDX. Input. setinputprocessor (stage1 );
GDX. Input. setinputprocessor (stage2 );

We should do this:

 
Inputmultiplexer =NewInputmultiplexer ();
Inputmultiplexer. addprocessor (stage1 );
Inputmultiplexer. addprocessor (stage2 );
Related Article

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.