In this article, we need to first master some simple libgdx knowledge to make it easier:
Use of the android game development framework libgdx (4)-stage and actorsHttp://www.apkbus.com/android-19750-1-1.html
Libgdx learning notes 2 draw imagesBytes.
- Public class progressbar extends Actor implements disposable {
- Texture platform;
- Texture bar;
- Int height;
- Int width;
- Float progress;
- // A simple adaptation is made. powerx and powery are the weights of the current device resolution respectively, based on the current mainstream 800*480
- Float powerx;
- Float powery;
- @ Override
- Public void draw (spritebatch batch, float arg1 ){
- // Todo auto-generated method stub
- Batch. draw (platform, (GDX. graphics. getwidth ()-bar. getwidth () * powerx)/2, 0, platform. getwidth () * powerx, platform. getheight () * powery );
- Batch. draw (Bar, (GDX. graphics. getwidth ()-bar. getwidth () * powerx)/2, 0, bar. getwidth () * progress/100f * powerx, bar. getheight () * powery );
- }
- @ Override
- Public actor hit (float arg0, float arg1 ){
- // Todo auto-generated method stub
- Return NULL;
- }
- Public progressbar (int x, int y ){
- Super ();
- // Set the location of the actor. It is useless here to introduce it to you.
- This. x = X;
- This. Y = y;
- Platform = new texture (GDX. Files. Internal ("black.png "));
- Bar = new texture (GDX. Files. Internal ("green.png "));
- Height = GDX. Graphics. getheight ();
- Width = GDX. Graphics. getwidth ();
- // A simple adaptation is made. powerx and powery are the weights of the current device resolution respectively, based on the current mainstream 800*480
- Powerx = GDX. Graphics. getwidth ()/800f;
- Powery = GDX. Graphics. getheight ()/480f;
- }
- Public void setprogress (float progress ){
- This. Progress = progress;
- }
- Public void dispose (){
- // Todo auto-generated method stub
- Platform. Dispose ();
- Bar. Dispose ();
- }
- }
Copy codeThe preceding variables and statements are described as follows: texture platform; texture bar; the former is the base image of the progress bar, while the bar represents the picture of the progress bar. As for the draw () method in actor, This is a method automatically called by the system. It describes how the actor instance is drawn. The draw () method does not need to be manually called, instead, after the actor is added to the stage, the stage is continuously called in the render () function of applicationlistener. draw (), the system will automatically call the draw () method of the actor that has been added to the stage, and draw the actor together. For details, see:Use of the android game development framework libgdx (4)-stage and actorsHttp://www.apkbus.com/android-19750-1-1.html
The remaining statements are also very simple. In draw (), two diagrams are drawn using spritebatch. The bar length varies according to the Progress progress, which produces the progress bar effect.
This time, we rewrite an applicationlistener and start it with androidapplication. The source code is modified and added based on the third lecture.
- Public class progress implements applicationlistener {
- Progressbar bar;
- Stage stage;
- @ Override
- Public void create (){
- // Todo auto-generated method stub
- Bar = new progressbar (0, 0 );
- // Create a stage
- Stage = new stage (GDX. Graphics. getwidth (), GDX. Graphics. getheight (), true );
- Stage. addactor (bar );
- }
- @ Override
- Public void dispose (){
- // Todo auto-generated method stub
- Bar. Dispose ();
- }
- @ Override
- Public void pause (){
- // Todo auto-generated method stub
- }
- @ Override
- Public void render (){
- // Todo auto-generated method stub
- GDX. gl. glclear (gl10.gl _ color_buffer_bit );
- GDX. gl. glclearcolor (1f, 1f, 1f, 0f );
- Stage. Act (GDX. Graphics. getdeltatime ());
- Stage. Draw ();
- If (bar. Progress <100)
- Bar. Progress + = 0.5;
- // Reset zero
- If (bar. Progress = 100)
- Bar. Progress = 0;
- }
- @ Override
- Public void resize (INT arg0, int arg1 ){
- // Todo auto-generated method stub
- }
- @ Override
- Public void resume (){
- // Todo auto-generated method stub
- }
- }
Copy codeThis is different from the previous Demo: GDX.GL. Glclear (gl10.Gl_color_buffer_bit); GDX.GL. Glclearcolor (1f, 1f, 1f, 0f); you can see that the color after the clear screen is set to (,), that is, white. You can see the effect below. Start with androidapplication:
- Public class mygame implements applicationlistener {
- Spritebatch batch;
- Bitmapfont BF;
- Particle;
- Particle effect TEM;
- Participant pool;
- Arraylist <participant leeffect> Participant lelist;
- Public void create (){
- // Stub
- Batch = new spritebatch ();
- BF = new bitmapfont ();
- // Initialize the particle variable
- Particle = new maid ();
- Particle. Load (GDX. Files. Internal ("particle. P"), GDX. Files. Internal (""));
- Particle pool = new maid tpool (particle, 5, 10 );
- Participant lelist = new arraylist <participant leeffect> ();
- }
- Public void render (){
- // Stub
- GDX. gl. glclear (gl10.gl _ color_buffer_bit );
- GDX. gl. glclearcolor (0f, 0f, 0f, 0f );
- Batch. Begin ();
- BF. Draw (batch, "testin mkey libgdx (3)", GDX. Graphics. getwidth () * 0.4f, GDX. Graphics. getheight ()/2 );
- Batch. End ();
- If (true ){
- If (GDX. Input. istouched ()){
- // When the distance between the touch point and the previous touch point is greater than a certain value, a new particle system is triggered, which reduces the burden on the system.
- TEM = maid ();
- TEM. setposition (GDX. Input. getx (), GDX. Graphics. getheight ()-GDX. Input. Gety ());
- Participant lelist. Add (TEM );
- }
- Batch. Begin ();
- For (INT I = 0; I <participant lelist. Size (); I ++ ){
- Participant lelist. Get (I). Draw (batch, GDX. Graphics. getdeltatime ());
- }
- Batch. End ();
- // Clear the completed Particle System
- Particle effect temparticle;
- For (INT I = 0; I <participant lelist. Size (); I ++ ){
- Temparticle = maid. Get (I );
- If (temparticle. iscomplete ()){
- Maid. Remove (I );
- }
- }
- }
- }
- Public void resize (INT width, int height ){
- // Stub
- }
- Public void pause (){
- // Stub
- }
- Public void resume (){
- // Stub
- }
- Public void dispose (){
- // Stub
- Batch. Dispose ();
- BF. Dispose ();
- // Never forget to release the memory
- Particle. Dispose ();
- If (TEM! = NULL)
- TEM. Dispose ();
- Particle pool. Clear ();
- }
- }
Copy code
Just run it. It's done! Appendix
Welfare diagram: Haha, the following is a big game I wrote with libgdx. I haven't fully written it yet. I can leak some spy photos first. If you like libgdx, please provide more support for my testin cup libgdx series tutorials. if many people support it and everyone wants the source code, I will consider releasing the source code for everyone to learn.
In fact, a game is a pet-developed game. It can be used for normal battles, Bluetooth battles, pet evolution, and so on. I don't know if you can see any function from the source code name? Haha, This is a waiting page for asynchronous loading written using the progress bar in this article. It is not complete yet. Spy photos !!!