虚幻4 ue4 学习笔记pwan篇 1.2 BindAxis与BindAction区别 与实现

时间:2022-12-01 20:56:37
虚幻4 ue4 学习笔记pwan篇 1.2 BindAxis与BindAction区别 与实现//pwan头文件部分
1
// Fill out your copyright notice in the Description page of Project Settings. 2
3 #pragma once
4
5 #include "CoreMinimal.h"
6 #include "GameFramework/Pawn.h"
7 #include "MyPawn.generated.h"
8
9 UCLASS()
10 class TEST2_API AMyPawn : public APawn
11 {
12 GENERATED_BODY()
13
14 public:
15 // Sets default values for this pawn's properties
16 AMyPawn();
17
18 protected:
19 // Called when the game starts or when spawned
20 virtual void BeginPlay() override;
21
22 public:
23 // Called every frame
24 virtual void Tick(float DeltaTime) override;
25
26 // Called to bind functionality to input
27 virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;//virtual作用在作为父类类型 引用时 防止使用父类方法
28 //override作用防止重写的不是父类的方法
29
30 void MoveForward(float AxisValue);//事件
31 void MoveRight(float AxisValue);//事件
32 void ParticleToggle2();//事件
33
34 void mouseClick(float AxisValue) {//事件与实现
35 if (AxisValue > 0) {
36 FString str1 = TEXT("x轴移动");
37 GEngine->AddOnScreenDebugMessage(-1, 0.5, FColor::Yellow, str1);
38 }
39 else if (AxisValue < 0) {

40 FString str1 = TEXT("y轴移动");41 GEngine->AddOnScreenDebugMessage(-1, 0.5, FColor::Purple, str1);
42 }
43 }
44 };
 
 //pawn cpp部分
1
// Fill out your copyright notice in the Description page of Project Settings. 2 #include "MyPawn.h"
3 // Sets default values
4 AMyPawn::AMyPawn()
5 {
6
7 // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
8 PrimaryActorTick.bCanEverTick = false;//不需要Tick
9 // 掌控默认玩家 这是关键没有这条语句 无法执行玩家操作
10 AutoPossessPlayer = EAutoReceiveInput::Player0;//要放在构造函数里 不要放在BeginPlay()里
11 }
12
13 // Called when the game starts or when spawned
14 void AMyPawn::BeginPlay()
15 {
16 Super::BeginPlay();
17
18 }
19 // Called every frame
20 void AMyPawn::Tick(float DeltaTime)
21 {
22 Super::Tick(DeltaTime);
23
24 }
25
26 // Called to bind functionality to input
27 void AMyPawn::SetupPlayerInputComponent(UInputComponent* InputComponent)
28 {//所有事件在这个函数里注册 不注册不能执行
29 Super::SetupPlayerInputComponent(InputComponent);
30
31 //BindAction
32 /*BindAction 第一个参数为 虚幻4->设置 ->输入->按键绑定的名称 */
33 /*参数二事件名称 IE_Pressed 按下 IE_Released松开 IE_DoubleClick双击 */
34 /*参数三当前对象*/
35 /*参数四 事件方法的函数指针 例 &类名::方法名 的写法*/
36 //会在内部用this调用注册的方法
37 InputComponent->BindAction("ParticleToggleWzh", IE_DoubleClick, this, &AMyPawn::ParticleToggle2);
38
39 //BindAxis
40 /*BindAction 第一个参数为 虚幻4->设置 ->输入->按键绑定的名称 一定要一样 重要!!! */
41 /*参数二 当前对象*/
42 /*参数三 事件方法的函数指针 例 &类名::方法名 的写法*/
43 //会在内部用this调用注册的方法
44 InputComponent->BindAxis("MoveForward", this, &AMyPawn::MoveForward);
45 InputComponent->BindAxis("MoveRight", this, &AMyPawn::MoveRight);
46 InputComponent->BindAxis("mouseClick", this, &AMyPawn::mouseClick);
47
48 //BindAxis与BindAction不同 BindAxis是被动执行的 一运行会一直调用 也就是说不能选择事件 需要加入条件判断语句结合AxisValue才能判断按键
49 }
50
51 void AMyPawn::MoveForward(float AxisValue)//移动前进
52 {
53
54 //AxisValue ue4里按键绑定的值 设置输入的向量值移动的大小
55 if (AxisValue > 0) {//一个事件可以绑定多个按键 这里用AxisValue来区分
56 FString str1 = TEXT("向前移动");// TEXT只能接受字符串常量 这里使用防止出现中文乱码 例如 : FString str1 = "向前移动"; //这样会乱码
57 GEngine->AddOnScreenDebugMessage(-1, 0.5, FColor::Red, str1);
58 }
59 else if (AxisValue < 0) {
60 FString str1 = TEXT("向后移动");
61 GEngine->AddOnScreenDebugMessage(-1, 0.5, FColor::Turquoise, str1);
62 }
63 }
64
65 void AMyPawn::MoveRight(float AxisValue)//移动向右
66 {
67 //AxisValue ue4里按键绑定的值 设置输入的向量值移动的大小
68 if (AxisValue > 0) {
69 FString str1 = TEXT("向右移动");
70 GEngine->AddOnScreenDebugMessage(-1, 0.5, FColor::Yellow, str1);
71 }
72 else if (AxisValue < 0) {
73 FString str1 = TEXT("向左移动");
74 GEngine->AddOnScreenDebugMessage(-1, 0.5, FColor::Purple, str1);
75 }
76 }
77
78 void AMyPawn::ParticleToggle2()//我特意用了和ue4里绑定的事件名称不同 不影响
79 {
80 FString str1 = TEXT("空格");
81 GEngine->AddOnScreenDebugMessage(-1, 0.5, FColor::Cyan, str1);
82 }
 
 

 

虚幻4 ue4 学习笔记pwan篇 1.2 BindAxis与BindAction区别 与实现

虚幻4 ue4 学习笔记pwan篇 1.2 BindAxis与BindAction区别 与实现

虚幻4 ue4 学习笔记pwan篇 1.2 BindAxis与BindAction区别 与实现