//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 }


