So I created a flipbook and set it
C++:
AAlien::AAlien(const class FObjectInitializer& PCIP) : Super(PCIP)
{
struct FConstructor
{
ConstructorHelpers::FObjectFinderOptional<UPaperFlipbook> AlienAsset;
FConstructor()
:AlienAsset(TEXT("PaperFlipbook'/Game/MetalSlug/Martian_Idle.Martian_Idle'"))
{
}
};
static FConstructor Constructor;
AlienAnimation = Constructor.AlienAsset.Get();
AlienComponent = PCIP.CreateAbstractDefaultSubobject<UPaperFlipbookComponent>(this, TEXT("AlienComponent"));
AlienComponent->SetFlipbook(AlienAnimation);
AlienComponent->AttachTo(RootComponent);`
};
And I would like to change the animation using an event but I don’t know how could I change the AlienAsset TEXT in the event.
Also:
void Start_Aim_Implementation(class UPaperFlipbook* AlienAnimation, class UPaperFlipbookComponent* AlienComponent)
Note: C++ beginner
samuelhely
(samuelhely)
October 30, 2016, 3:17pm
2
Do you mean changing the animation like running, jumping and etc or changing the character itself?
The paper 2D template already shows how you can load different animations
// Setup the assets
struct FConstructorStatics
{
ConstructorHelpers::FObjectFinderOptional<UPaperFlipbook> RunningAnimationAsset;
ConstructorHelpers::FObjectFinderOptional<UPaperFlipbook> IdleAnimationAsset;
FConstructorStatics()
: RunningAnimationAsset(TEXT("/Game/2dSideScroller/Sprites/RunningAnimation.RunningAnimation"))
, IdleAnimationAsset(TEXT("/Game/2dSideScroller/Sprites/IdleAnimation.IdleAnimation"))
{
}
};
static FConstructorStatics ConstructorStatics;
RunningAnimation = ConstructorStatics.RunningAnimationAsset.Get();
IdleAnimation = ConstructorStatics.IdleAnimationAsset.Get();
You can also implement your own function, like state machines, to change the animations.
Oh! Thank you! you saved me… But I still don’t know how to change the flipbooks… For example: My character is using Idle when not moving and when moving he will use the running, I know how to build this events, I just need to change the animation. Could you show me how to change the flipbook?plz.
samuelhely
(samuelhely)
October 31, 2016, 12:57am
4
I believe you want the “GetSprite()->SetFlipbook(FlipbookAnimation);” function.
This is my function to get the animation I want.
void YourClass::SetAnimation()
{
UPaperFlipbook* DesiredAnimation = nullptr;
const FVector PlayerVelocity = GetVelocity();
const float PlayerSpeedX = PlayerVelocity.X;
const float PlayerSpeedZ = PlayerVelocity.Z;
if (bIsDead)
{
GetSprite()->SetLooping(false);
DesiredAnimation = DeathAnimation;
}
else if (PlayerSpeedX == 0.0f && PlayerSpeedZ == 0.0f)
{
DesiredAnimation = IdleAnimation;
}
else if (PlayerSpeedX == 0.0f && PlayerSpeedZ != 0.0f)
{
DesiredAnimation = IdleJumpAnimation;
}
else if (PlayerSpeedX != 0.0f && PlayerSpeedZ != 0.0f)
{
DesiredAnimation = JumpAnimation;
}
else if (PlayerSpeedX != 0.0f && bIsRunning)
{
DesiredAnimation = RunAnimation;
}
else if (PlayerSpeedX != 0.0f && PlayerSpeedZ == 0.0f)
{
DesiredAnimation = WalkAnimation;
}
GetSprite()->SetFlipbook(DesiredAnimation);
}
I have different animations to jump while idle and running but you can see how it works.
Thank you so much! You are my hero!