Use of the android game development framework libgdx (4)-stage and actors

Source: Internet
Author: User
Tags libgdx

The libgdx used in this article is version 0.92, which may be different from the latest version. The full text is for reference only.

Let's take a look at a few games first to understand what is a stage.

Please observe the elements in the image carefully. Some items cannot be moved, some can be moved, some have special effects, and some do not. Some are buttons and some are images, but they can all be called actors ).

The entire game interface is our stage.

Let's look at another shooting game.

The actors are

Actors are a common object in game design. They accept unified management of the stage and have some public events, such as touch and click, but they also have their own responses and attributes.

The stage is a place for actors. It manages all actors in a unified manner, accepts input, and provides a convenient framework for operating actor time changes.

Let's take a look at the stage class:

 
Protected final group root; protected final spritebatch batch; protected camera;

It has a group, a spritebatch, and a camera.

Spritebatch, as we have mentioned in the previous articles, will not be repeated here.

A group is a class used to hold and control actors. However, it should be noted that the Group itself actually inherits from the actor.

The camera is skipped here. Later, it can be understood as a tool for controlling the viewing angle and indicator conversion.

When we have an actor, we can call the addactor method to join the stage.

You can obtain the input on the stage, but you need to set it.

 
GDX. Input. setinputprocessor (stage );

The following is a column to control the movement of a person.

Button for controlling characters:

Put the desired image in Assert

Three new classes are created:

Firstgame to implement the applicationlistener Interface

Firstactor, inherited from actor

Narrowbutton, inherited from actor

Let's take a look at firstgame.

Declare a stage, instantiate firstactor and narrowbutton, add the two to the stage, and finally set the input response to stage.

Package COM. cnblogs. htynkn. listener; import Java. util. date; import Java. util. random; import javax. microedition. khronos. opengles. GL; import android. util. log; import COM. badlogic. GDX. applicationlistener; import COM. badlogic. GDX. GDX; import COM. badlogic. GDX. graphics. gl10; import COM. badlogic. GDX. graphics. g2d. bitmapfont; import COM. badlogic. GDX. scenes. scene2d. stage; import COM. cnblogs. htynkn. domain. firstactor; import COM. cnblogs. htynkn. domain. narrowbutton; public class firstgame implements applicationlistener {private stage; private firstactor; private narrowbutton; @ override public void create () {stage = new stage (GDX. graphics. getwidth (), GDX. graphics. getheight (), true); firstactor = new firstactor ("renwu"); button = new narrowbutton ("narrow"); stage. addactor (firstactor); stage. addactor (button); GDX. input. setinputprocessor (stage) ;}@ override public void dispose () {stage. dispose () ;}@ override public void pause () {// todo auto-generated method stub} @ override public void render () {GDX. GL. glclear (gl10.gl _ color_buffer_bit); stage. act (GDX. graphics. getdeltatime (); stage. draw () ;}@ override public void resize (INT width, int height) {// todo auto-generated method stub} @ override public void resume () {// todo auto-generated method stub }}

Let's take a look at firstactor.

Declare a texture for painting. The height and width are obtained in the constructor to facilitate later hit time judgment.

Package COM. cnblogs. htynkn. domain; import COM. badlogic. GDX. GDX; import COM. badlogic. GDX. graphics. texture; import COM. badlogic. GDX. graphics. g2d. spritebatch; import COM. badlogic. GDX. scenes. scene2d. actor; public class firstactor extends actor {texture; @ override public void draw (spritebatch batch, float parentalpha) {batch. draw (texture, this. x, this. y) ;}@ override public actor hit (float X, float y) {If (x> 0 & Y> 0 & this. height> Y & this. width> X) {return this;} else {return NULL; }}@ override public Boolean touchdown (float X, float y, int pointer) {// todo auto-generated method stub return false;} @ override public void touchdragged (float X, float y, int pointer) {// todo auto-generated method stub} @ override public void touchup (float X, float y, int pointer) {// todo auto-generated method stub} public firstactor (string name) {super (name); texture = new texture (GDX. files. internal ("actor1.gif"); this. height = texture. getheight (); this. width = texture. getwidth ();}}

In narrowbuttonCodeThe draw part and the following content mainly address the issue of controlling the actions of the person after clicking.

Modify the touchdown event:

Get firstactor through group and control the value of X.

 
Public Boolean touchdown (float X, float y, int pointer) {actor = This. Parent. findactor ("renwu"); actor. x + = 10; return false ;}

Effect:

So far, we have implemented the simplest character control. However, there are still many examples to be improved. For example, the direction button does not have the effect of clicking and the character does not have the effect of moving.

We can use animation. Add an image

For details, let's take a look at the animation class:

Public class animation {final textureregion [] keyframes; public float frameduration;/** constructor, storing the frame duration and key frames. ** @ Param frameduration the time between frames in seconds. * @ Param keyframes the {@ link textureregion} s representing the frames. */Public animation (float frameduration, list keyframes) {This. frameduration = frameduration; this. keyframes = new textur Eregion [keyframes. size ()]; for (INT I = 0, n = keyframes. size (); I <n; I ++) {This. keyframes [I] = (textureregion) keyframes. get (I) ;}/ ** constructor, storing the frame duration and key frames. ** @ Param frameduration the time between frames in seconds. * @ Param keyframes the {@ link textureregion} s representing the frames. */Public animation (float frameduration, textureregion... keyframes) {T His. frameduration = frameduration; this. keyframes = keyframes;}/** returns a {@ link textureregion} Based on the so called state time. this is the amount of seconds an object has spent in the * state this animation instance represents, e.g. running, jumping and so on. the Mode specifies whether the animation is * looping or not. * @ Param statetime the time spent in the State represented by this Nimation. * @ Param looping whether the animation is looping or not. * @ return the textureregion representing the frame of animation for the given state time. */Public textureregion getkeyframe (float statetime, Boolean looping) {int framenumber = (INT) (statetime/frameduration); If (! Looping) {framenumber = math. Min (keyframes. Length-1, framenumber);} else {framenumber = framenumber % keyframes. length;} return keyframes [framenumber] ;}}

We can see that the so-called animation is actually a constant switching of an image (in fact, all animations look like this ).

We construct an image list and retrieve it continuously based on event changes. re-draw the image to form an animation.

Pay attention to the input time and image list size. Modify the firstactor Code as follows:

Package COM. cnblogs. htynkn. domain; import COM. badlogic. GDX. GDX; import COM. badlogic. GDX. graphics. texture; import COM. badlogic. GDX. graphics. g2d. animation; import COM. badlogic. GDX. graphics. g2d. spritebatch; import COM. badlogic. GDX. graphics. g2d. textureregion; import COM. badlogic. GDX. scenes. scene2d. actor; public class firstactor extends actor {texture texture1; texture extends; animation; Comment [] Comment sframe; float statetime; @ overridepublic void draw (spritebatch batch, float parentalpha) {statetime + = GDX. graphics. getdeltatime (); textureregion currentframe = animation. getkeyframe (statetime, true); batch. draw (currentframe, this. x, this. y) ;}@ overridepublic actor hit (float X, float y) {GDX. app. log ("info", x + "" + this. width); If (x> 0 & Y> 0 & this. height> Y & this. width> X) {return this;} else {return NULL; }}@ overridepublic Boolean touchdown (float X, float y, int pointer) {// todo auto-generated method stubreturn false;} @ overridepublic void touchdragged (float X, float y, int pointer) {// todo auto-generated method stub} @ overridepublic void touchup (float X, float y, int pointer) {// todo auto-generated method stub} public firstactor (string name) {super (name); texture1 = new texture (GDX. files. internal ("actor1.gif"); texture2 = new texture (GDX. files. internal ("actor2.gif"); this. height = texture1.getheight (); this. width = vertex (); textureregion region1; textureregion region2; region1 = new textureregion (texture1); region2 = new vertex (texture2); vertex sframe = new textureregion [30]; for (INT I = 0; I <30; I ++) {if (I % 2 = 0) {javassframe [I] = region1 ;} else {javassframe [I] = region2 ;}} animation = new animation (0.25f, javassframe );}}

Effect:

Note that we need to convert texture to textureregion. This is because the images in actual development are integrated. For example, all the images used by the role are placed in a single segment, and then separated and intercepted. The corresponding auxiliary method is textureregion. Split.

In addition, we can find that there are a large number of code duplicates in narrowbutton and firstactor. Some may think we should extract the Code. In fact, libgdx has already helped us. For more information, see

There are some common UI controls, which can be discussed in the next article.

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.