Libgdx Environment Construction

Source: Internet
Author: User
Tags libgdx

1. Download the SDK

1) libgdx Development Kit download: Google Code (the latest libgdx-0.9.7.zip 2012.11.12)

2) download libgdx main source code: GitHub tags

II,Environment Construction

1) Add a development jar package

Libgdx Android Development Kit mainly include GDX. jar, gdx-backend-android.jar, as well as armeabi and armeabi-v7a (difference)

Decompress the libgdx -x.x.x.zip file and you can see the following directory tree:

When libgdx is developed, copy the blue horizontal line development kit to the libs directory of your android project, as shown below:

2) Add the source code jar package

During development, you may need to link to view libgdx source code, so you need to add gdx-backend-android-sources.jar and gdx-sources.jar source package

Add steps: Click libgdx development jar package reference function -- View Source code -- attach source... -- select gdx-backend-android-sources.jar and gdx-sources.jar source package

3) after adding the development package jar and source package jar in your project mylibgdx, the effect is as follows:

3.,Simple Example

1) Create an activity

In mylibgdx of your project, create a mainactivity that inherits from the parent class androidapplication of libgdx.

public class MainActivity extends AndroidApplication {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initialize(new MyGame(), false);}}

2) custom mygame

In initialize () of mainactivity, the custom mygame class is used to implement the libgdx parent class interface applicationlistener. The detailed definition is as follows:

Public class mygame implements applicationlistener {private spritebatch batch; // initialize the Game Genie private bitmapfont BF; // initialize the font painting @ overridepublic void create () {// create batch = new spritebatch (); BF = new bitmapfont (); BF. setcolor (color. red); BF. setscale (1.0f) ;}@ overridepublic void render () {// render GDX. GL. glclear (gl10.gl _ color_buffer_bit); GDX. GL. glclearcolor (0, 0, 0, 0); batch. begin (); BF. draw (batch, "Hello libgdx", GDX. graphics. getwidth ()/2, GDX. graphics. getheight ()/2); batch. end () ;}@ overridepublic void resize (INT arg0, int arg1) {// Layout Adjustment} @ overridepublic void pause () {// pause} @ overridepublic void resume () {// restore} @ overridepublic void dispose () {// destroy BF. dispose (); batch. dispose ();}}

Running result:

Source code download

Thu,Libgdx Lifecycle

The preceding simple example shows the libgdx running process. The specific process is as follows:

1) The main mainactivity inherits from the libgdx parent class androidapplication, while the androidapplication inherits the basic Android activity and implements the libgdx custom interface class application. The relationship is as follows:

Public class androidapplication extends activity implements Application

2) customize mygame and implement the libgdx parent interface applicationlistener. The applicationlistener interface is as follows:

public class MyGame implements ApplicationListener {   public void create () {   }   public void resize (int width, int height) {    }   public void render () {           }   public void pause () {    }   public void resume () {   }   public void dispose () {    }}

The logical process is as follows:

Function Description of the logical flow:

Method Name

Function Description

Create ()

When an application is created, it is called only once.

Resize (INT width, int height)

This method is called every time the game screen is reset and not paused. It is called only once after create (). The parameter is the new width and height (pixels) after the screen is reset)

Render ()

This method is called cyclically every time rendering occurs. Game logic update is often used in this method, that is, refresh cyclically to draw the logical picture of the game.

Pause ()

In Android, when the home key is pressed or re-entered into the program, this is a good time to save the game status, resume () may not be called

Resume ()

This method is called only in Android. When the application obtains the focus from the pause status again, resume

Dispose ()

This method is called when the application destroys destroyed, after pause ()

 

The logic flowchart of libgdx application development:

V. Summary

The preceding example and flowchart show that the libgdx game development procedure is as follows:

1) andriodapplication, inherited from the android base class activity, plays a role in an activity, but we do not do much work in this "activity, of course, you can save the context, such as onsaveinstancestate () and onrestoreinstancestate ().

2) The actual display work in libgdx development is handed over to a class that implements the applicationlistener interface. All functions required by libgdx include creation, rendering (refresh), pause, and restoration, destruction. So the actual situation is that most of our drawing work in the game is done on this page.

Reference recommendations:

Libgdx Overview

The life-cycle (libgdx wiki)

Libgdx game engine tutorial applicationlistener

Android game development framework libgdx development framework Overview

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.