上面的文章中,我们创建了一个门,并且当人才上去的时候,门就会打开,当离开的时候,门就自动关闭了。大概但是我们希望当我们离开以后,至少还要有一段时间才能关闭,这时就用到了定时器。
- 定义一个定时器句柄
FTimerHandle switchTimer;
- 使用定时器
GetWorldTimerManager().SetTimer(switchTimer, this, &AFloorSwitch::closeTheDoor, 2.0);
第一个参数:把时间控制权交给谁,TimeHandle
第二个参数: 哪个类的对象
第三个参数: 时间到时执行的函数指针,(去执行closeTheDoor()函数)
第四个参数: 间隔多久(2s)去执行closeTheDoor()函数
第五个参数: false - 只执行一次 true - 循环执行文章来源:https://www.toymoban.com/news/detail-646415.html
// 清空
GetWorldTimerManager().ClearTimer(switchTimer);
// 暂停
GetWorldTimerManager().PauseTimer(switchTimer);
// 结束暂停
GetWorldTimerManager().UnPauseTimer(switchTimer);
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FloorSwitch.generated.h"
UCLASS()
class FIRSTPROJECT_API AFloorSwitch : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFloorSwitch();
//触发盒子
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category="FloorSwitch")
class UBoxComponent* traggerBox = nullptr;
//地板开关
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "FloorSwitch")
class UStaticMeshComponent* floorSwitch = nullptr;
//门
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "FloorSwitch")
class UStaticMeshComponent* door = nullptr;
UFUNCTION()
void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult&SweepResult);
UFUNCTION()
void onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent*OtherComp, int32 OtherBodyIndex);
UFUNCTION(BlueprintImplementableEvent, Category="FloorSwitch")
void riseDoor();
UFUNCTION(BlueprintImplementableEvent, Category = "FloorSwitch")
void lowerDoor();
//门的初始位置
UPROPERTY(BlueprintReadWrite, Category = "FloorSwitch")
FVector initDoorLocation;
//地板开关的初始位置
UPROPERTY(BlueprintReadWrite, Category = "FloorSwitch")
FVector initFloorSwitchLocation;
UFUNCTION(BlueprintImplementableEvent, Category = "FloorSwitch")
void riseFloorSwitch();
UFUNCTION(BlueprintImplementableEvent, Category = "FloorSwitch")
void lowerFloorSwitch();
UFUNCTION(BlueprintCallable, Category="FloorSwitch")
void updateDoorLocation(float z);
UFUNCTION(BlueprintCallable, Category = "FloorSwitch")
void updateFloorSwitchLocation(float z);
private:
FTimerHandle switchTimer;
void closeTheDoor();
bool bIsOnFloor = false;//是否站在地板开关上
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "FloorSwitch.h"
#include "Components/BoxComponent.h"
// Sets default values
AFloorSwitch::AFloorSwitch() {
// 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;
traggerBox = CreateDefaultSubobject<UBoxComponent>(TEXT("TriggerBox"));
RootComponent = traggerBox;
traggerBox->SetCollisionEnabled(ECollisionEnabled::QueryOnly);//只检测碰撞事件
traggerBox->SetCollisionObjectType(ECC_WorldStatic);//设置对象类型
traggerBox->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);//忽略所有通道
traggerBox->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);//对Pawn类型开放事件重叠事件
traggerBox->SetBoxExtent(FVector(50.0, 50.0, 50.0));
floorSwitch = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FloorSwitch"));
floorSwitch->SetupAttachment(GetRootComponent());
door = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Door"));
door->SetupAttachment(GetRootComponent());
}
void AFloorSwitch::onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
UE_LOG(LogTemp, Warning, TEXT("AFloorSwitch::onBeginOverlap"));
riseDoor();
lowerFloorSwitch();
bIsOnFloor = true;
}
void AFloorSwitch::onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
UE_LOG(LogTemp, Warning, TEXT("AFloorSwitch::onEndOverlap"));
bIsOnFloor = false;
GetWorldTimerManager().SetTimer(switchTimer, this, &AFloorSwitch::closeTheDoor, 2.0);
}
void AFloorSwitch::updateDoorLocation(float z) {
FVector newLocation = initDoorLocation;
newLocation.Z += z;
door->SetWorldLocation(newLocation);
}
void AFloorSwitch::updateFloorSwitchLocation(float z) {
FVector newLocation = initFloorSwitchLocation;
newLocation.Z += z;
floorSwitch->SetWorldLocation(newLocation);
}
void AFloorSwitch::closeTheDoor() {
if (!bIsOnFloor){
riseFloorSwitch();
lowerDoor();
}
}
// Called when the game starts or when spawned
void AFloorSwitch::BeginPlay() {
Super::BeginPlay();
//绑定事件
traggerBox->OnComponentBeginOverlap.AddDynamic(this, &AFloorSwitch::onBeginOverlap);
traggerBox->OnComponentEndOverlap.AddDynamic(this, &AFloorSwitch::onEndOverlap);
initDoorLocation = door->GetComponentLocation();
initFloorSwitchLocation = floorSwitch->GetComponentLocation();
}
// Called every frame
void AFloorSwitch::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
}
aaa文章来源地址https://www.toymoban.com/news/detail-646415.html
到了这里,关于UE4使用定时器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!