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"));}
阅读全文
0 0
- UE4学习笔记8th:变量, 计时器,和事件
- UE4学习笔记8th
- UE4学习笔记9th:在编辑器中修改变量和函数
- UE4学习笔记4th
- UE4学习笔记5th
- UE4学习笔记6th
- UE4学习笔记7th
- UE4学习笔记9th
- UE4学习笔记10th
- UE4学习笔记11th
- UE4学习笔记12th
- UE4学习笔记13th
- UE4学习笔记14th
- UE4学习笔记15th
- UE4学习笔记16th
- UE4学习笔记17th
- UE4学习笔记18th
- UE4学习笔记19th
- 源码 Activity 的 window 创建过程
- 对一个“刷机无效”的锁屏APP的分析
- 【转载】JAVA面试经验内容整理
- jQuery源码学习笔记(09)
- Grafana 安装手册
- UE4学习笔记8th:变量, 计时器,和事件
- sql小计
- kaggle 房价预测经典文章
- 使用JDK自带jvisualvm远程监控tomcat
- 利用 -before -after伪类实现鼠标悬浮动画效果
- weka连接mysql
- 初入IT,你所需要知道的---纯属本人谏言
- Joao恶意样本技术分析与防护方案
- spring boot Scheduler