Android game engine and engine Learning Series 2: simple example

Source: Internet
Author: User

How to Learn efficiently is the most worth learning.

 

 

First import the shelf package, download: http://download.csdn.net/detail/duancanmeng/4060082 lib folder

Like the android program entends activity, we also need extends basegameactivity at the beginning, and four functions must be overloaded:

public class Main extends BaseGameActivity {      public void onLoadComplete() {               }      public Engine onLoadEngine() {          return null;      }      public void onLoadResources() {               }      public Scene onLoadScene() {          return null;      }    } 

 

You only need to implement the above four methods and add the business logic and data to complete a simple small example.

First:

 

 

The Code is as follows:

Public class shapemodifierexample extends basegameactivity {Private Static final int camera_width = 720; Private Static final int camera_height = 480; private camera mcamera; private texture mtexture; private jsonobjects; @ overridepublic engine onloadengine () {log. I ("test", "onloadengine"); // game camera, which has two functions in camera of andengine. One is used to adjust the display area of the screen, second, use the HUD class to draw the game screen on the mobile phone. // Four parameters in new camera. The first and second parameters represent the left vertex of camera, and the third and fourth parameters represent the width and height of camera. mcamera = new camera (, camera_width, camera_height); // engine is the core of the andengine. // It centrally manages important components such as camera and scene in the andengine, // but it must be used with basegameactivity. // The engineoptions class can be used to configure necessary parameters. // Parameters in engineoptions: // the first judge whether the screen is full screen // The second determines whether the screen is a horizontal screen or a vertical screen // The third ratioresolutionpolicy (adjust the screen size proportionally, to adapt to the screen size), // The fourth parameter is our own // The third parameter can also be: baseresolutionpolicy (in addition to checking the screen size, nothing will be done), // fillresolutionpolicy (stretch the game screen to full screen fill, depending on the camera size, there will be different degrees of deformation), // fixedresolutionpolicy (forcibly set the game screen to a fixed size, this setting does not automatically adapt to the screen size), // ratioresolutionpolicy (adjust the screen size proportionally to fit the screen size), // relativeresolutionpolicy (based on the scaling parameter when the relativeresolutionpolicy is built, scales the game screen to a specified ratio ). Return new engine (New engineoptions (true, screenorientation. landscape, new ratioresolutionpolicy (camera_width, camera_height), this. mcamera);} @ overridepublic void onloadresources () {log. I ("test", "onloadresources"); // The texture constructor has three parameters: // the first one: the width of the texture image to be used // The second one: the height of the texture image to be used // The Third: texture Rendering Method // The third parameter contains the following conditions: // 1. Nearest (nearest filter, the implementation depends on gl_nearest for non-smooth filtering. The texture surround mode is gl_clamp_to_edge, and the display speed is fast and the image quality is poor. // 2. bilinear (bilinear interpolation, the implementation depends on GL _ Linear performs linear filtering. The texture surround mode is gl_clamp_to_edge, and the display speed is low.) // 3. Repeating (nearest is the same as nearest filtering, but gl_repeat is the texture surround mode, the blank area on the texture is automatically filled, and the display speed is fast and the image quality is poor. // 4. repeating_bilinear (the same as bilinear, but the texture surround mode is gl_repeat, the blank area on the texture will be automatically filled, and the display speed is very slow (low-end machines run in this mode is very tragic, high-end machines are still acceptable )) // 5. nearest_premultiplyalpha (the difference between textureoptions at the end of all [premultiplyalpha] and other classes with the same name is only whether transparent textures can be set based on the Alpha value, as shown below) // 6, bilinear_premultiplyalpha // 7, repeating_premultiplyalpha // 8, repeating_bili Static objects such as near_premultiplyalpha. // Etc. This. mtexture = new texture (64, 32, textureoptions. bilinear_premultiplyalpha ); // The first two parameters indicate that a space of // pwidth * pheight is used to store the texture map loaded by createfromasset. // Note 1 // paste the texture image to our definition above. in texture. // The parameters are as follows: the second is the context, and the fourth is the X coordinate of the texture attached to texture. The fifth is the Y coordinate of the texture pasted to texture. The sixth is the number of columns to be pasted, 7: indicates the number of lines to be pasted in the texture. This. mfacetextureregion = textureregionfactory. createtiledfromasset (this. mtexture, this, "face_box_tiled.png", 0, 0, 2, 1); this. mengine. ge Ttexturemanager (). loadtexture (mtexture) ;}@ overridepublic scene onloadscene () {log. I ("test", "onloadscene"); // The registerupdatehandler function is registered to obtain the iupdatehandler interface. The onupdate and reset functions are waiting for implementation, almost all andengine applications will surely see it. It is also one of the main methods for andengine to add specific businesses to the game business thread. // It is mainly used for external method calling to update the service. // you can see that this is written in all exmaples. mengine. registerupdatehandler (New fpslogger (); // Note 2 // scenario container, which is similar to screen in lgame and can be called as a game module in a specific scenario, we can use it to switch the current game's screen and touch screen monitoring. The switching method is to use the engine. setscenefinal scene = new scene (1); // The color value must be between 0.0 and 1.0, for example (, 0. setbackground (New colorbackground (0.09804f, 0.6274f, 0.8784f); Final int centerx = (camera_width-this. mfacetextureregion. getwidth ()/2; Final int centery = (camera_height-this. mfacetextureregion. getheight ()/2; log. I ("test", "centerx:" + centerx + ", centery:" + centery); // defines a square final rectangle rect = new rectangle (centerx + 100, centery, 32, 32); rect. setcolor (1, 0, 0); // define an animation Genie and set some attributes final animatedsprite face = new animatedsprite (centerx-100, centery, this. mfacetextureregion); face. animate (100); face. setblendfunction (gl10.gl _ src_alpha, gl10.gl _ progress); // Note 3 // mainly sets the actions of the two components defined above // loopshapemodifier (final ishapemodifierlistener pshapemodiferlistener, final int ploopcount, final iloopshapemodifierlistener ploopmodifierlistener, final ishapemodifier pshapemodifier) // you can see from the printed statement that the first parameter is some operations performed when the entire action ends, the second parameter is the number of cycles of the action, the third parameter is the number of operations performed after the action is completed, and the fourth parameter is the final loopshapemodifier shapemodifier = new loopshapemodifier (New ishapemodifierlistener () {@ overridepublic void onmodifierfinished (final imodifier <ishape> pshapemodifier, final ishape pshape) {shapemodifierexample. this. runonuithread (New runnable () {@ overridepublic void run () {toast. maketext (shapemodifierexample. this, "sequence ended. ", toast. length_long ). show () ;}) ;}}, 1, new round () {@ overridepublic void onloopfinished (final loopmodifier <ishape> ploopshapemodifier, final int ploopsremaining) {shapemodifierexample. this. runonuithread (New runnable () {@ overridepublic void run () {toast. maketext (shapemodifierexample. this, "loops remaining:" + ploopsremaining, toast. length_short ). show () ;}}) ;}}, new sequenceshapemodifier (// here, the first is duration, the second is from, and the third is toNew rotationmodifier (1, 0, 90), new alphamodifier (2, 1, 0), new alphamodifier (1, 0, 1), new scalemodifier (2, 1, 0.5f), new delaymodifier (0.5f ), // here are some combined actions new parallelshapemodifier (New scalemodifier (3, 0.5f, 5), new rotationbymodifier (3, 90), new parallelshapemodifier (New scalemodifier (3, 5, 1), new rotationmodifier (3,180, 0); face. addshapemodifier (shapemodifier); rect. addshapemodifier (shapemodifier. clone (); scene. gettoplayer (). addentity (FACE); scene. gettoplayer (). addentity (rect); Return scene ;}@ overridepublic void onloadcomplete () {// todo auto-generated method stublog. I ("test", "onloadcomplete ");}}

Note 1: Because the texture image is:, it will be defined according to the specific situation of the texture.

 

After running the program, you can find that the background quickly prints out: onloadengine, onloadresources, onloadscene, centerx: 328, centery: 224, onloadcomplete

Then execute a series of actions to see the background print:

It can be determined that the function of registration in Note 2 is to constantly refresh the screen, and the ontickupdate function in the engine class is called externally.

 

NOTE 3: here do not understand, can refer to the http://www.cnblogs.com/yujunyong/archive/2011/04/13/2015467.html

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.