Programming Games for Android

Programming Games for Android 16: Actors

42 videos

238 minutes

Come up with the creation of the first actor. Right now I have two images. Missing to put some, that and will be putting. but minijoe and skewers already have images. We could create an actor named minijoe to indicate who is the protagonist, although it is true that going to change textures and we will make available to the user more if you do not like it, but start with something simple we'll start talking about Minijoe,

and we will create an actor representing our protagonist, so I'm going to do is create a package ... oh, this is not a package, forgiveness. This is a package. the icons appear. In fact I'm going to create here, as subfolder or sub-package, to keep the actors. And now, I will create a class called ActorJugador to indicate that is the class that represents my character. The actors in Scene2D have a particularity, and that should extend to a

Actor class called. Obviously we have to import the good kind, it must be the class that begins com.badlogic.gdx. The fact is that in libgdx players are added to the screen, Stage. For example in the Stage I call addActor method, and thereby add an actor. All actors include their own behavior and are independent from each other. This is very advantageous because we added to the stage all actors

we are interested in, and then the actor can define the behavior separately. When render () ask that a call to act () or draw () is made, what the Stage is, respectively, he calls on all actors to be updated or draw. Therefore, Actor we will provide two elementary methods: one is to act (), and the other is draw (). The first, act (), be invoked when you call act (). Scene2D calls all actors

and asks them to be updated. One of the parameters is the delta, which works like the Delta render (), so if my actor must be updated as time goes by you can. And the other is draw (), which has two parameters: one is the SpriteBatch have associated Scene2D the stage, as you can see it is Batch type, is the parent class of SpriteBatch, but left draw. The other is a parameter called parentAlpha representing

alphas or opacities. You will have to see when working with groups so that if we do that in a group have to be transparent something, all players become transparent at a time. The groups make it very easy to work with certain operations. For now let's see how we could draw our player. Before focusing on the subject to draw let's see how this actor instanciaría. Instantiate an actor is not very difficult.

I think your body as a player, and then you say it's a new ActorJugador, and call the method that I have presented addActor () to add the player to my Stage. The advantage of working with actors is also Scene2D work deals with everything that is positions, sizes, because obviously the actors must be positioned somewhere they should be. The Actor class what you have is a bunch of methods like setBounds,

to establish its dimensions or setColor, to color. Or setX, setY to say where to place; setwith to indicate the width of the object; setHeight, to change the height; setPosition, setRotation for rotation; setScale ... you do to the idea that we have a lot of methods. setTouchable, which we will use during entry to check when we can click on things touching them with your finger so that events are triggered

in an almost magical way. We could use these methods to define the player's position. For example, jugador.setPosition (20, 100). What I want is to appear and to then when you turn the soil appears that the ground is there. As I do not know how high is the mobile screen for now I work with a 640x480 screen. In this case 100 assures us that is not the bottom, not the top, but it will have a space that

will leave to see the world. 20 I will put it in the X because I want is to be placed on the left: the character will move forward and I want you to have all the screen on the side of the right to prevent the spikes when they come or when it is time to take flight to react, because otherwise no. But this does nothing because the actor is empty. Let's see how to make the actor does something really.

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.