Ogengine game development genie (sprite)

Source: Internet
Author: User

Spirte Introduction

Sprite can be said to be the main character in the game. We can build various pictures and the display of characters is inseparable from it. Sprite classes are rich. If you have read the source code, it is not difficult to find that there are many available types: Basic genie spirte, animation genie animatedspirte, and buttons Sprite.

You can have multiple options. You can directly use animatedsprite to draw character animations, use buttonsprite to draw buttons, or inherit the sprite class to implement your own genie. Therefore, we will introduce the use of Sprite here. First, let us understand the relevant knowledge of Sprite:


Texture texture and bitmaptextureatlas

To draw a Sprite, you must load its texture. Texture is the bitmap drawn on the sprite object. The ogengine engine stores all textures in the form of texture objects in the memory and manages all texture objects in the game through texturemanager.

Bitmaptextureatlas can be considered as a map with many different textures. It is mainly used to combine the texture together into a large texture.

First, understand its structure:

  1. Public bitmaptextureatlas (final texturemanager ptexturemanager, final int pwidth, final int pheight ){

  2. This (ptexturemanager, pwidth, pheight, bitmaptextureformat. rgba_8888 );

  3. }

Copy code

Because the width and height of the texture loaded in ogengine must be the power of 2, all values of pwidth and pheight can be found to 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. Another noteworthy thing is textureregion, which can "Snap" a texture from bitmaptextureatlas.

  1. /** Create a texture image combination object with the power height and width twice */

  2. Bitmaptextureatlas bgtexture = new bitmaptextureatlas (

  3. Getengine (). gettexturemanager (), 2048,128 );


  4. /** Generate the texture region bgregion */

  5. Textureregion bgregion = bitmaptextureatlastextureregionfactory.

  6. Createfromasset (bgtexture, getactivity (). getassets (), "GFX/bg.jpg", 0, 0 );


  7. /** Load texture combinations in the engine */

  8. Getengine (). gettexturemanager (). loadtexture (bgtexture );

Copy code

(Bg.jpg must be placed in the GFX folder under the asset directory)


Create Sprite


(1) General Sprite

Sprite is a subclass of entity. Genie is the visual representation of people or objects in the game. The term "Genie" originally refers to an independent graphic image placed on the game background and animated. Most of its methods are from entity classes.

1) Scaling:

  1. Setscalex (final float pscalex)

  2. Setscaley (final float pscaley)

  3. Setscale (final float pscale)

  4. // Corresponding to the X direction, y direction, and overall scaling respectively.

Copy code


2) Transparency:

  1. Setalpha (float palpha) // corresponding transparency

Copy code

3) set the location:

  1. Setposition (float PX, float Py)

  2. Setposition (ientity potherentity)

  3. // You can directly give the coordinates of the previous one. This is often used when dragging and moving images. You can also directly set the relative coordinates,

  4. // For example, setleftpositionx (float px); settoppositiony (float Py );

  5. // An ientity object will be given later, and then it will obtain the coordinate value of the ientity object to assign the coordinate value of the current genie

Copy code


4) rotation:

  1. Setrotation (float protation)

  2. // The parameter is the angle, that is, 45 degrees (45f), 90 degrees (90f), and 180 degrees (180f)

  3. /** Secondary settings include the center of rotation */

  4. Setrotationcenterx (final float protationcenterx): // X coordinate

  5. Setrotationcentery (final float protationcentery): // y coordinate

  6. Setrotationcenter (final float protationcenterx, final float protationcentery): // X coordinate Y

Copy code


Create a genie in scene:

  1. /** Create a genie object */

  2. Sprite BG = new sprite (0, 0, BG, this. getvertexbufferobjectmanager ());

Copy code


Add the genie to the scene

  1. /** Add the genie in the scenario */

  2. This. attachchild (BG );

Copy code



(2) animation genie animatedsprite


I. usefulness

Ogengine encapsulates a tiledsprite class that can pass in the texture of tiledtextureregion to construct an animated sequence image that can be played continuously. The animation sprite inherits from the tiledsprite to describe some frame animation resources. The itiledtextureregion required in the animatedsprite constructor is tiled instead of a normal textureregion. That is to say, it is based on a single frame of animation resources!


II. The general usage of tiledtextureregion resource tiletextureregion is a large image consisting of many single-frame images. It is generally created by calling the createtiledfromasset (...) method in bitmaptextureatlastextureregionfactory.

  1. /** Create a texture image combination object with the power height and width twice */

  2. Bitmaptextureatlas bgtexture = new bitmaptextureatlas (getengine ()

  3. . Gettexturemanager (), 1024,102 4 );


  4. /** Generate the texture region mtextureregion */

  5. Tiledtextureregion mtextureregion = bitmaptextureatlastextureregionfactory

  6. . Createtiledfromasset (bgtexture,

  7. This. getactivity (). getassets (),

  8. "GFX/snapdragon_tiled.png", 0, 0, 4, 3 );


  9. /** Load texture combinations in the engine */

  10. Getengine (). gettexturemanager (). loadtexture (bgtexture );

Copy code




This is a common usage. These static methods make us ignore the tiledtextureregion constructor.

  1. Public tiledtextureregion (final itexture ptexture, final itextureregion... ptextureregions)

Copy code


Iii. Final Analysis

In fact, it is very easy to find a solution to this problem, that is, step by step, from the createtiledfromasset () of the vertex, to the createtiledfromsource () of the textureregionfactory, to the CREATE () of tiledtextureregion (), when you see the following code, you will know that the tiledtextureregion constructor is called after all...

4. Create an animation genie

  1. /** Create a genie object */

  2. Animatesprite BG = new animatesprite (0, 0, mtextureregion,

  3. This. getvertexbufferobjectmanager ());

Copy code



5. Add the genie to the scene

  1. /** Add the genie in the scenario */

  2. This. attachchild (BG );

  3. /** Set the playing frequency of the genie animation */

  4. BG. animate (100 );

Copy code


(3) button genie

Similar to animatesprite, buttonsprite constructor also requires tiledtextureregion. The difference is that buttonsprite has three states: Normal, pressed, and disabled. The default status is normal. When you press the button, 2nd frames of animation are displayed, and 3rd frames of animation cannot be displayed on time. Create buttonsprite and add methods to scene:

  1. /** Create a genie object */

  2. Buttonsprite bg2 = new buttonsprite (100,100, msnapdragontextureregion, this. getvertexbufferobjectmanager ());

  3. /** Add the genie in the scenario */

  4. This. attachchild (bg2 );

Copy code

To add a touch listener to buttonsprite, you must cancel the touch blocking first,

  1. /** Cancel touch blocking */

  2. Bg2.setignoretouch (false );

  3. Bg2.setonclicklistener (New onclicklistener (){

  4. @ Override

  5. Public void onclick (buttonsprite pbuttonsprite,

  6. Float ptoucharealocalx, float ptoucharealocaly ){

  7. // Todo auto-generated method stub

  8. Log. V ("tag", "bg2 ");

  9. }

  10. });

Http://www.eoeandroid.com/forum-863-1.html

Www.ogengine.com



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.