AndEngine Game Development Series tutorial (2)

Source: Internet
Author: User

Previous address: http://blog.csdn.net/lan410812571/article/details/9716743

Vi. Scenario scene

Scenario Scene is a subclass of Entity. In AndEngine, this class is used to create scenarios in the game. Each Scene object has a background. The default background is a black color block. You can use setBackground (final IBackgruond pBackground) to modify it.
Constructor:
Scene (final int pLayerCount)
The parameter is the number of layers in the graph. Note that the number of layers in the graph cannot be changed once the Sence object is created. So if you want to add a new layer to the game, you need to re-instantiate the Snece object with the new value.
Layer layer: A Layer class is a subclass of the Entity class. It is used to organize the images required by the Scene class and layers are superimposed to form a scenario. (For example, the near layer moves a little faster, and the distant layer moves slowly to form a depth of field effect)
SubScene object:
The Scene object can have a non-layer sub-object. The management method is as follows:
Void setChildrenScene (final Scene pChildrenScene)
Scene getChildrenScene ()
Upper-level Scene object:
A Scene object can have a upper-level Scene object. When a scene object is added as a sub-object to another scene object, its upper-level scene object is automatically set. Of course, AndEngin is also a management method void setParentScene (final Scene pParentscene)
The Scene object has a set of systems for corresponding touch events, which will be detailed later. Some Scene classes subclass to build special scenarios.
1. CameraScene
Camerascene (final int playercount, finl camera pcamera)
Added the camera function on the basis of common scene.
2. menuscene
Menuscene (final camera pcamera, final ionmenuitemclicklistener ponmenuitemclicklistener)
In the previous chapter 3rd, I used this scenario to create menus.
3. popupscene
You can temporarily pop up the scene object on the current scene.

7. modifier Modifier

The previous sections are boring concepts, and the next tutorial will be interesting. In andengine, the modifier is an entity modifier. As long as it is an entity, you can modify the relevant attributes through modifier. To use modifier, you must call the registerentitymodifier (final ientitymodifier pentitymodifier) method for registration. The modifier method described below is a subclass of entitymodifier.

Location-related Modifier:
Movemodifier (final float pduration, final float pfromx, final float ptox, final float pfromy, final float ptoy, final implements pentitymodifierlistener, final ieasefunction peasefunction)
Red indicates the listener callback and easing function when the modifier is completed. This will be discussed later.
Pduration is the number of seconds that the movement lasts.
In addition, there are movexmodifier (...) and moveymodifier (...). As the name suggests, an object can be used to move in an orthogonal direction.

Modifier related to scaling:
Scalemodifier (final float pduration, final float pfromscale, final float ptoscale, final ientitymodifierlistener pentitymodifierlistener, final ieasefunction peasefunction)
The original size when the scaling factor is 1.0f.

this.mMenuScene.registerEntityModifier(new ScaleAtModifier(0.5f, 1.0f, 0.0f,CAMERA_WIDTH/2,CAMERA_HEIGHT/2));

In addition, there is also scaling based on the specified center point
Scaleatmodifier (float pduration, float pfromscale, float ptoscale, float pscalecenterx, float pscalecentery)
Latency-related Modifier:
Delaymodifier (final float pdurationfinal ientitymodifierlistener pentitymodifierlistener)
Pduration is the delay time. pentitymodifierlistener calls back when the delay action is completed.

Modifier related to rotation:
Rotationmodifier (float pduration, float pfromrotation, float ptorotation)
Rotationatmodifie (...)
Transparency-related Modifier:
AlphaModifier (float pDuration, float pFromAlpha, float pToAlpha)
Color-related Modifier:
ColorModofier (....)

Modifier combination
Sometimes, only one effect is not enough. In this case, you need to build a series of Modifier combinations to change.
ParallelEntityModifier: used when more than two modifiers need to be applied to an Entity at the same time
ParallelEntityModifier (IEntityModifier... pEntityModifiers)
SequenceEntityModifier: used when more than two modfiers need to be applied to an Entity in sequence.
SequenceEntityModifier (IEntityModifier... pEntityModifiers)

card.registerEntityModifier(new SequenceEntityModifier(new ParallelEntityModifier(new AlphaModifier(5, 0.2f, 0.8f),new ScaleModifier(5, 0.2f, 0.8f), new MoveModifier(5,CAMERA_WIDTH / 2- this.mCardTextureRegion.getWidth()/ 2, CAMERA_WIDTH / 2- this.mCardTextureRegion.getWidth()/ 2, -this.mCardTextureRegion.getHeight(), CAMERA_HEIGHT / 2- this.mCardTextureRegion.getHeight()/ 2,EaseElasticInOut.getInstance())),new ParallelEntityModifier(new AlphaModifier(3, 0.8f, 1.0f),new RotationModifier(3, 0, 361),new MoveModifier(3,CAMERA_WIDTH / 2- this.mCardTextureRegion.getWidth()/ 2, -135,CAMERA_HEIGHT / 2- this.mCardTextureRegion.getHeight()/ 2,-195),new ScaleModifier(3, 0.8f, 0.2f))));

Example free: http://download.csdn.net/detail/lan410812571/5858831

VIII. easefunction easing Function

In the previous section, we can find that the card games have a rebound effect, with code segments.

new MoveModifier(5,CAMERA_WIDTH / 2- this.mCardTextureRegion.getWidth()/ 2, CAMERA_WIDTH / 2- this.mCardTextureRegion.getWidth()/ 2, -this.mCardTextureRegion.getHeight(), CAMERA_HEIGHT / 2- this.mCardTextureRegion.getHeight()/ 2,EaseElasticInOut.getInstance())

What is the last parameter? It is the easing function described in this section. When an Entity does not use EaseFunction, it changes linearly, that is, it changes at a constant speed. For example, when MoveModifer is used, the speed of moving from point A to point B remains unchanged. If EaseFunction is used, it will be a variable speed movement.
EaseFunction class naming rules:
Vertex <type> <endpoint> such as EaseElasticInOut
Type refers to the type of the easing function, such as Circular, Elastic, and Quarter. The endpoint is the stage In which the function affects the action. In is the initial stage of time, Out is the end stage, and InOut is affected by both stages.
AndEngine provides 34 types of easing functions.
EaseBackInOut
EaseBounceInOut
EaseCircularInOut
EaseCublicInOut
EaeExponentialInOut
EaseLinearInOut
EaseElasticInOut
EaseQuadInOut
EaseQuartInOut
EaseQuintInOut
EaseSineInOut
EaseStrongInout

Dynamic Effect http://easings.net/zh-cn of slow-moving Function

9. draw lines and rectangles

AndEngine does not provide many plotting functions. For most games, image resources are composed of a series of bitmaps. Few games need to draw a large number of graphics during game operation. AndEngine allows you to draw lines and rectangles.
Line:
Line (final float pX1, final float pY1, final float pX2, final float pY2, final foat pLineWidth)
It's easy to determine a line segment at two points. PLineWidth is the line width.
Rectangle:
Rectangule (final float px, final float py, fianl float pWidth, final flat pHeight, final RectangleVretexBuffer pRectangleVertexBuffer)
Px and py are coordinates in the upper left corner of the rectangle. pWidth and pHeight are width and height. The optional parameter pRectangleVertexBuffer is used to increase the drawing speed when a large number of rectangles are drawn. It can also be used to distort a rectangle.
Use Line to draw a six-mountain star

// Draw the six-star final float X0 = CAMERA_WIDTH/2, Y0 = CAMERA_HEIGHT/2; // draw the base point Line star1 = new Line (X0-100, Y0-57, X0 + 100, Y0-57, 3.0f); Line star2 = new Line (100,173, 0, 100,173, 3.0f); Line star3 = new Line (-0,114,-, 0, 3.0f); Line star4 = new Line, -200,114, 3.0f); Line star5 = new Line (-100,-57,-200,114, 3.0f); Line star6 = new Line (-100,-57,0, 114, 3.0f); star1.attachChild (star2); star1.getLastChild (). attachChild (star3); star1.getLastChild (). attachChild (star4); star1.getLastChild (). attachChild (star5); star1.getLastChild (). attachChild (star6); star1.setRotationCenter (100, 57); star1.setScaleCenter (100, 57); Round (new RotationModifier (3.0f, 0.0f, 360.0f), new ScaleModifier (0.5f, 1.0f, 9.0f); scene. getLastChild (). attachChild (star1 );

10. Texture

Textures are the bitmaps drawn on Sprite objects. AndEngine stores all textures in the form of Texture objects in the memory and manages all Texture objects in the game through TextureManager. The following code is often displayed in the previous sections.

mBgTexture = new Texture(1024, 1024,TextureOptions.BILINEAR_PREMULTIPLYALPHA);mBgTextureRegion = TextureRegionFactory.createFromAsset(mBgTexture,this, "level1_bg.jpg", 0, 0);mEngine.getTextureManager().loadTexture(mBgTexture);

Texture class
Texture (int pWidth, int pHeight, TextureOptions pTextureOptions)
Because the width and height of the texture loaded in the andengine must be the power of 2, we can find that the pwidth and pheight values follow. If the width and height values are not the full power of 2, an illegalargumentexception exception is thrown. However, the width and height of images used in the game are diverse, and they cannot all be a power of two. So we have the concept of textureregion in the texture area. We can now understand texture as a container for storing images.
The texureoptions parameter is used to control how OpenGL renders images. The default value is usually used.

Texureregionfatory class
After creating a blank texture storage area, use textureregion to load the image for the texture object.
1. Create a normal texture texureregion:
Use asset resources: Use bitmap stored in the assets folder, you can use textureregionfactory. setassetbasepath (string passetbasepath) is used to set the path for searching resources. The parameter is a local path name. It must end with a slash (/) and cannot be empty. Otherwise, an exception is thrown.
Textureregionfactory. createfromasset (texture ptexture, context pcontext, string passetpath, int ptexturepositionx, int ptexturepositiony)
Ptexture: Specifies the texture object for which textures need to be loaded.
Pcontext: Context Environment object of the current activity object.
Passetpath: name of the resource file, which must contain a suffix. Supports PNG, JPG, and BMP.
Int pTexturePositionX, int pTexturePositionY: position occupied by the loaded image in Texture. This position is the position in the upper left corner of the image, so Texture must be large enough to accommodate the image.
Use Resource:
CreateFromResource (Texture pTexture, Context pContext, int pDrawableResourceID, int pTexturePositionX, int pTexturePositionY)
PDrawableResourceID: such as R. drawable. pic

2. Create a tile texture TileTextureRegion:
TileTextureRegion usually contains more than one image. Each image has the same width and height. These images are organized in a matrix. Each image can be located by position in the matrix. Tile pasters can be used to create animated Genie and store map blocks.
TextureRegionFactory. createTiledFromAsset (Texture pTexture, Context pContext, String pAssetPath, int pTexturePositionX, int pTexturePositionY, int pTileColumns, int pTileRows)
TextureRegionFactory. createTiledFromResource (Texture pTexture, Context pContext, int pDrawableResourceID, int pTexturePositionX, int pTexturePositionY, int pTileColumns, int pTileRows)

Therefore, Texture and TextureRegion are integrated, and images are required in the game.

11. buildabletexture

In the previous section, we will find that Texture is really troublesome. Each time we need to tell the location of the image loaded by TextureRegionFactory in the Texture object. We need to plan how to arrange these images and calculate the coordinates used to call the TextureRegionFactory method with caution. Therefore, AndEngine provides a set of methods for automatically arranging image positions.
BuidableTexture class

// Force, speed, and blood volume status mark group mPropertyTexture = new BuildableTexture (512,512, TextureOptions. BILINEAR_PREMULTIPLYALPHA); mCaseTextureRegion = TextureRegionFactory. createFromAsset (mPropertyTexture, this, "case.png"); mSwordTextureRegion = TextureRegionFactory. createFromAsset (mPropertyTexture, this, "sword.png"); mShiftTextureRegion = TextureRegionFactory. createFromAsset (mPropertyTexture, this, "shift.png"); mBloodTextureRegion = TextureRegionFactory. createFromAsset (mPropertyTexture, this, "blood.png"); try {mPropertyTexture. build (new BlackPawnTextureBuilder (2);} catch (final TextureSourcePackingException e) {Log. d ("Leekao", "don't fit");} this. mEngine. getTextureManager (). loadTexture (mPropertyTexture );

After creating the TextureRegion object used, call the build method before using BuildableTexture to pass in a builder. In this way, if the execution fails, the exception will be caught.

// Attribute status Group Final sprite casebox = new sprite (566, 3, mcasetextureregion); Final sprite sword = new sprite (579, 14, mswordtextureregion ); final sprite shift = new sprite (653, 14, mshifttextureregion); Final sprite blood = new sprite (731, 14, mbloodtextureregion); scene. getlastchild (). attachchild (casebox); scene. getlastchild (). attachchild (sword); scene. getlastchild (). attachchild (shift); scene. getlastchild (). attachchild (blood );

You may find it troublesome to arrange the coordinates of images. So I used a tool to export the zwoptex coordinates for me.

Http://www.zwopple.com/zwoptex/assets/files/zwoptex-flashversion.zip

Import images to the tool and adjust the location

Then file -- export coordinates to get a plist file. open the file and you will see the coordinates of each graph.

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.