UE4学习笔记8th:变量, 计时器,和事件

来源:互联网 发布:网络女主播视频种子 编辑:程序博客网 时间:2024/05/21 02:21

创建C++项目,命名为HowTo_VTE
这里写图片描述

创建一个新的C++类,父类选择Actor,命名为Countdown
这里写图片描述

在Countdown.h中输入定义:

int32 CountdownTime;UTextRenderComponent* CountdownText;void UpdateTimerDisplay();

这里写图片描述

在Countdown.cpp中输入:

CountdownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));CountdownText->SetHorizontalAlignment(EHTA_Center);CountdownText->SetWorldSize(150.0f);RootComponent = CountdownText;CountdownTime = 3;

这里写图片描述

开启定时器

在最下面定义一个新函数,显示倒数:

void ACountdown::UpdateTimerDisplay(){CountdownText->SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));}

这里写图片描述

在Countdown.h里添加定义:

        void AdvanceTimer();void CountdownHasFinished();FTimerHandle CountdownTimerHandle;

这里写图片描述

回到Countdown.cpp,添加:

void ACountdown::AdvanceTimer(){--CountdownTime;UpdateTimerDisplay();if (CountdownTime < 1){//停止计时GetWorldTimerManager().ClearTimer(CountdownTimerHandle);CountdownHasFinished();}}void ACountdown::CountdownHasFinished(){//显示“GO”CountdownText->SetText(TEXT("GO"));}

这里写图片描述

接下来就是编译,,,将Countdown拖入视口中
这里写图片描述

按下Alt+P,你会看到原本Text处会出现倒数,出现GO后停止。

这里写图片描述

这里写图片描述

Countdown.h// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "GameFramework/Actor.h"#include "Countdown.generated.h"UCLASS()class HOWTO_VTE_API ACountdown : public AActor{GENERATED_BODY()public:// Sets default values for this actor's propertiesACountdown();// Called when the game starts or when spawnedvirtual void BeginPlay() override;// Called every framevirtual void Tick( float DeltaSeconds ) override;int32 CountdownTime;UTextRenderComponent* CountdownText;void UpdateTimerDisplay();void AdvanceTimer();void CountdownHasFinished();FTimerHandle CountdownTimerHandle;};Countdown.cpp // Fill out your copyright notice in the Description page of Project Settings.#include "HowTo_VTE.h"#include "Countdown.h"// Sets default valuesACountdown::ACountdown(){ // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;PrimaryActorTick.bCanEverTick = false;CountdownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));CountdownText->SetHorizontalAlignment(EHTA_Center);CountdownText->SetWorldSize(150.0f);RootComponent = CountdownText;CountdownTime = 3;}// Called when the game starts or when spawnedvoid ACountdown::BeginPlay(){Super::BeginPlay();UpdateTimerDisplay();GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &ACountdown::AdvanceTimer, 1.0f, true);}// Called every framevoid ACountdown::Tick( float DeltaTime ){Super::Tick( DeltaTime );}void ACountdown::UpdateTimerDisplay(){CountdownText->SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));}void ACountdown::AdvanceTimer(){--CountdownTime;UpdateTimerDisplay();if (CountdownTime < 1){//停止计时GetWorldTimerManager().ClearTimer(CountdownTimerHandle);CountdownHasFinished();}}void ACountdown::CountdownHasFinished(){//显示“GO”CountdownText->SetText(TEXT("GO"));}
原创粉丝点击