User Tools

Site Tools


guides:beginners:changing_direction

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, orxNULL);
}

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 graphics are centered so that when we flip left and right, there won't be any issues with the physically body shifting incorrectly:

[HeroGraphic]
Texture        = soldier_full.png
TextureOrigin  = (0,0,0)
TextureSize    = (32,32,0)
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.

guides/beginners/changing_direction.txt · Last modified: 2016/12/31 02:37 (3 months ago) by sausage