User Tools

Site Tools


Part 12 - Changing Direction

So far the hero has only been able to face right. He needs to face left.

There are many ways to do this. The more correct way would be to create flipped animation graphics and hook up a bunch more animations and links. But we will do it the easiest way possible. When pressing the left key, we will flip the entire hero around in code:

orxVECTOR flipLeft = { -2, 2, 1 };
orxVECTOR flipRight = { 2, 2, 1 };
if (orxInput_IsActive("GoLeft"))
    orxObject_SetScale(hero, &flipLeft);
    orxObject_ApplyImpulse(hero, &leftSpeed, orxNULL);
    orxObject_SetTargetAnim(hero, "HeroRun");
else if (orxInput_IsActive("GoRight"))
    orxObject_SetScale(hero, &flipRight);
    orxObject_ApplyImpulse(hero, &rightSpeed, orxNULL);
    orxObject_SetTargetAnim(hero, "HeroRun");
else {
    orxObject_SetTargetAnim(hero, "HeroIdle");

The orxObject_SetScale function is being used here instead of the usual orxObject_SetFlip. The latter does not flip child objects or body positions, but scale does. We will need this later when our hero has a child gun object.

Notice that we need to restore the flip when the right key is pressed.

Finally, ensure the pivot for our hero's graphic and animation set are both centered so that when we flip left and right, there won't be any issues with the physically body shifting incorrectly:

Texture        = soldier_full.png
TextureOrigin  = (0,0,0)
TextureSize    = (32,32,0)
Pivot          = center
Texture = soldier_full.png
FrameSize = (32, 32, 0)
HeroRun = 6
HeroIdle = 1
StartAnim = HeroIdle
HeroIdle-> = HeroIdle # .HeroRun
HeroRun-> = HeroRun # HeroIdle
Pivot          = center

Compile and run. We can run left and right. That should be working well, our hero can now run left and right, or stand idle either left or right.

Next, Part 13 - getting our hero to shoot.

en/guides/beginners/changing_direction.txt · Last modified: 2018/02/14 04:47 (3 months ago) by iarwain