UE4使用定时器

上面的文章中,我们创建了一个门,并且当人才上去的时候,门就会打开,当离开的时候,门就自动关闭了。大概但是我们希望当我们离开以后,至少还要有一段时间才能关闭,这时就用到了定时器。

  1. 定义一个定时器句柄
FTimerHandle switchTimer;
  1. 使用定时器
GetWorldTimerManager().SetTimer(switchTimer, this, &AFloorSwitch::closeTheDoor, 2.0);

第一个参数:把时间控制权交给谁,TimeHandle
第二个参数: 哪个类的对象
第三个参数: 时间到时执行的函数指针,(去执行closeTheDoor()函数)
第四个参数: 间隔多久(2s)去执行closeTheDoor()函数
第五个参数: false - 只执行一次 true - 循环执行

// 清空
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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值