# Please give some tips about C++ code for 2D game

**URL:** <https://forums.unrealengine.com/t/please-give-some-tips-about-c-code-for-2d-game/437052>\
**Category:** Asset Creation\
**Tags:** flipbook, side-scroller, unreal-engine, question, CPP\
**Created:** [November 5, 2018, 11:16am UTC](https://forums.unrealengine.com/t/please-give-some-tips-about-c-code-for-2d-game/437052 "2018-11-05T11:16:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![XTjiang](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XTjiang](https://forums.unrealengine.com/u/XTjiang)\
**Post date:** [November 5, 2018, 11:16am UTC](https://forums.unrealengine.com/t/please-give-some-tips-about-c-code-for-2d-game/437052/1 "2018-11-05T11:16:00Z")

</div>

I am beginner for UE4 program, now I am trying to learning how to use C++ to implement a simple 2D action side-scrolling game. The first step for that is try to set up a main character. I used 2D sprite and flipbook, then try to code some base function such as idle, walk, jump, and so on. I had a simple concept:

 ![258553-捕获.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/5/0/450a8ca101519be41b312a1e4d02fce86f3e29e2.png)

I also wrote some code for that but it didn’t work well, for example:  
in character class. I used some bool to control the animation status:

```
// Custom Params
IsLanding = true;
IsWalking = false;
IsRunning = false;

IsStartJump = false;
IsRising = false;
IsStartFalling = false;
IsInAir = false;
IsFalling = false;

```

in Tick() function, I checked these variables.  
The I used my custom function “UpdateAnimation” to set Flipbook. just like follow code:

```
// Standing
if (IsLanding && !IsRising && !IsRunning && !IsWalking)
{
	GetSprite()->SetFlipbook(IdleAnimation);
}
// Walking
if (IsLanding && IsWalking && !IsRising && !IsRunning)
{
	GetSprite()->SetFlipbook(WalkingAnimation);
}

// Running
if (IsRunning && IsLanding) 
{
	GetSprite()->SetFlipbook(RunningAnimation);
}

// Jump start
if (IsStartJump && !IsRising && !IsStartFalling)
{
	GetSprite()->SetFlipbook(JumpStartAnimation);
}
// Jump rising
if (IsRising && !IsStartFalling && !IsStartJump)
{
	GetSprite()->SetFlipbook(JumpRisingAnimation);
}
// Jump in air and start falling
if(IsStartFalling && !IsFalling && !IsStartJump)
{
	GetSprite()->SetFlipbook(JumpInAirAnimation);
}
// Falling
if(IsFalling)
{
	GetSprite()->SetFlipbook(JumpFallingAnimation);
}

```

Did I have mistakes or logic problem? Can somebody give me some tips for coding. Thx first.

By the way, the resource I used were downloaded from website, these were extracted from other games, I just used it to learn and find some fun. 🙂

---

<div class="post-metadata">

**Author:** ![Hawba](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hawba](https://forums.unrealengine.com/u/Hawba)\
**Post date:** [November 5, 2018, 2:30pm UTC](https://forums.unrealengine.com/t/please-give-some-tips-about-c-code-for-2d-game/437052/2 "2018-11-05T14:30:18Z")

</div>

Hi XTijang,

what you’re trying to do is a State Machine. You can see how to do that in Blueprints in the [8th video](https://www.youtube.com/watch?v=Af-jsQFlLFU&index=9&list=PLZlv_N0_O1gauJh60307mE_67jqK42twB&t=0s) of this great [Zack Parish series](https://www.youtube.com/playlist?list=PLZlv_N0_O1gauJh60307mE_67jqK42twB&action_edit=1).

In your case I would first consider refactoring animation statuses into an enum (short for enumeration). Your custom params would become something like:

```
UENUM()
enum AnimationType
{
  Landing,
  Walking,
  Running,
  StartJump,
  Rising,
  StartFalling,
  InAir,
  Falling,
  Idle
};

```

This goes in your character header file. Now your can add a variable to your character header file, that will hold an enumeration value:

```
UPROPERTY()
AnimationType ActiveAnimation = AnimationType::Idle; // set default to Idle (standing)

```

Now you can use a switch statement, and use the ActiveAnimation variable to set the right animation flipbook.

```
switch(ActiveAnimation ) {
    case AnimationType::Walking : 
			GetSprite()->SetFlipbook(WalkingAnimation);
            break;
	case AnimationType::Running :
			GetSprite()->SetFlipbook(RunningAnimation);
            break;
	case AnimationType::StartJump :
			GetSprite()->SetFlipbook(JumpStartAnimation);
            break;
	case AnimationType::Rising :
			GetSprite()->SetFlipbook(JumpRisingAnimation);
            break;
	case AnimationType::InAir :
			GetSprite()->SetFlipbook(JumpInAirAnimation);
            break;
	case AnimationType::Falling :
			GetSprite()->SetFlipbook(JumpFallingAnimation);
            break;
	default: 
			GetSprite()->SetFlipbook(IdleAnimation);
            break; 
}

```

Making these changes will require you to change your logic about setting the IsLanding, IsWalking,IsRunning, … variables. You will have to change the characters ActiveAnimation variable now. For example, if you wanted to set your character to falling, you would set it something like this:

```
ActiveAnimation = AnimationType::Falling;

```

There are other ways to code this logic, of course 🙂

But before you make any big changes, _make sure_ you have a copy of your work, or consider using a versioning system like Git or Subversion. (It’s like having a save game, but for your C++ code :D)

Happy coding!

---

<div class="post-metadata">

**Author:** ![XTjiang](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XTjiang](https://forums.unrealengine.com/u/XTjiang)\
**Post date:** [November 6, 2018, 1:25am UTC](https://forums.unrealengine.com/t/please-give-some-tips-about-c-code-for-2d-game/437052/3 "2018-11-06T01:25:18Z")

</div>

Thank you very much! I am beginner for writing code, your tips are very important for me, I will try it today. Cool ~~~~~  
Thx, Hawba 🙂
