Programming Games for Android

Programming Games for Android 18: Act Actor

42 videos

238 minutes

After seeing the ActorJugador class, obviously we still have to explain the operation of the act () method. What I will do is instantiate, or create a new class, a new type of actor, in this case ActorPinchos to represent my spikes, which is not very complicated to make, It is exactly the same. Only in this case, as you remember when I brought Skewers, this is the texture that I used as an excuse to cram

speaking of TextureRegions, so ActorPinchos will need us to pass, not a texture as a parameter, but a TextureRegion, so you know where the boundaries of the texture to use to draw are. This is the same: I the builder and passed as a parameter these spikes. And like before, I say batch.draw (skewers, getX (), getY ()). In this case, instantiate it is equally simple. First I make a texture spikes.

pinchos = new Texture ( 'pinchos.png'). In this case I have, not only to take care of the texture, but in the region. TextureRegion regionPinchos. You begin to understand why a assetManager becomes clearer because look how it is running. RegionPincho be a new region, and if I remember correctly the numbers should be 0, 64, 128, 64. memory. Now what I do is instantiate my ActorPinchos, which I pass the region,

and with this I have it loaded. stage.addActor (skewers), and I will put in the position (400, 100). Really what I want is to explain how it works the act method, so that appears on the right side'm content. Let's see where it appears, because frankly I do not know. All right. Very clever of me forget to call the correct variable, but the important thing is that appear on the right side of the screen. Where I'm going with this?

I'm interested skewers move to the left to hit the player. To do this I will use the act for my skewer method has a movement constant leftward. This requires using the delta variable. Why? Because someone will say, 'I say here in act, I want to move it to the left, and is simple. I look where my spikes are, and readjusted in X - 10 pixels. ' This is a mistake. Do not do this. First,

this will be done 60 times per second so it will go very fast. Too fast for my taste. Second, because if our game starts to fluctuate, imagine that the mobile start doing things background or any problems occur, as will lower the rate at which it is called to act, because it lowers the number times that can be called per second, will speed up, slow down and create an unpleasant effect. Instead what is done is a motion effect

uniform speed, taking advantage we have the delta parameter. To do this I have open Skitch because I want to show what I want to achieve. Delivery of the next base. I have a screen, I assume this white rectangle to simplify things, occupying 640 or so wide. I have here my spikes and my character have here. Let's assume that the character has to the right a distance of 600 pixels,

although it seems a bit excessive, so what we will do is reduce it to 500. The spikes move 500 pixels to the left and hit the player. What I want is to give the player a time of 2 seconds. I do not want to put it very difficult but do not want to make it as easy in the world. The intention is that the skewers class scroll within two seconds a distance of 500 pixels. If we do the calculation, and this is a rule of 3,

my objetito must travel speed -250 pixels in the X direction; ie every second its X position should decremented by 250 pixels. The only problem is we do not know when to go a second, only every 0.01 or 0.02 seconds to invoke the act method, and you are going to say how many Seconds have passed since the last time it was called to act, being a very small decimal value. So the last thing to do is divide this

in small packages and form a rule 3: if you go in a 1 250 pixels, 0.01 seconds X. If you move I make it, I have to do is figure speed linearly multiplying by the speed delta in pixels per second. For example, to make a character of these progress to a -250 speed px / sec, I have to multiply by -250 delta, to determine how much to move this bundle of a couple of milliseconds

us has been granted. For example, setX (getX () - 250 * delta). Thus if 0.01 seconds has now passed 2.5 pixels will move, if I have done well the accounts. If the game is slower and takes 40 milliseconds ... multiply it, but still leaving a very small value. Through these increases we get a safer ride. If now I place in the 500 position I will have 2 seconds of time until it disappears

on the left side of the screen. Such that well. In fact I even past because it is very slow. The important thing is, as you see We have a somewhat serious problem, because if you repeat the animation, the character is not unfazed by the existence of the spikes. Introduce collisions need to detect that there has been a collision.

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.