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.

Compile and Run. We can run left and right and the flipping is correct. But… there is a huge gap when the hero switches directions.

Therefore we need to 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. That should be working much better. Our hero can 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/06/28 07:45 (6 years ago) by