摘星星之角色的水平移动

 

Cocos Creator 中的键盘控制与边界检测:实现角色的水平移动

在游戏开发中,角色的移动控制和边界检测是常见的需求。本文将通过一个具体的代码示例,解析如何在 Cocos Creator 中实现基于键盘输入的角色水平移动,并防止角色越过屏幕边界。

代码解析

以下代码实现了一个简单的角色水平移动逻辑,包括加速度、最大速度限制以及边界检测。
TypeScript复制
 1 cc.Class({
 2     extends: cc.Component,
 3 
 4     properties: {
 5         // 最大移动速度
 6         @property(cc.Integer)
 7         maxMoveSpeed: 0,
 8         // 加速度
 9         @property(cc.Integer)
10         accel: 0,
11     },
12 
13     private xSpeed: number = 0; // 当前水平速度
14     private accLeft: boolean = false; // 是否向左加速
15     private accRight: boolean = false; // 是否向右加速
16     private halfScreenWidth: number = 0; // 屏幕宽度的一半
17 
18     onLoad() {
19         // 初始化屏幕宽度的一半
20         this.halfScreenWidth = this.node.parent.width / 2;
21 
22         // 初始化加速度方向开关和水平速度
23         this.accLeft = false;
24         this.accRight = false;
25         this.xSpeed = 0;
26 
27         // 注册键盘事件监听器
28         cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
29         cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
30     },
31 
32     onDestroy() {
33         // 取消键盘事件监听器
34         cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
35         cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
36     },
37 
38     onKeyDown(event) {
39         // 使用 switch 判断按键值
40         switch (event.keyCode) {
41             case cc.macro.KEY.a: // 按下 A 键
42                 this.accLeft = true;
43                 break;
44             case cc.macro.KEY.d: // 按下 D 键
45                 this.accRight = true;
46                 break;
47             default:
48                 console.log("Other key is pressed: " + event.keyCode);
49                 break;
50         }
51     },
52 
53     onKeyUp(event) {
54         // 使用 switch 判断按键值
55         switch (event.keyCode) {
56             case cc.macro.KEY.a: // 抬起 A 键
57                 this.accLeft = false;
58                 this.xSpeed = 0;
59                 break;
60             case cc.macro.KEY.d: // 抬起 D 键
61                 this.accRight = false;
62                 this.xSpeed = 0;
63                 break;
64             default:
65                 console.log("Other key is released: " + event.keyCode);
66                 break;
67         }
68     },
69 
70     update(dt) {
71         // 根据当前运动方向,用加速度每帧更新速度
72         if (this.accLeft) {
73             this.xSpeed -= this.accel * dt;
74         } else if (this.accRight) {
75             this.xSpeed += this.accel * dt;
76         }
77 
78         // 限制角色的最大速度
79         if (Math.abs(this.xSpeed) > this.maxMoveSpeed) {
80             this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
81         }
82 
83         // 更新角色的位置
84         this.node.x += this.xSpeed * dt;
85 
86         // 防止角色越过边界
87         const halfNodeWidth = this.node.width / 2;
88         if (this.node.x > this.halfScreenWidth - halfNodeWidth) {
89             this.node.x = this.halfScreenWidth - halfNodeWidth;
90         }
91         if (this.node.x < -this.halfScreenWidth + halfNodeWidth) {
92             this.node.x = -this.halfScreenWidth + halfNodeWidth;
93         }
94     }
95 });

 

代码功能解析

1. 属性定义

  • maxMoveSpeed:角色的最大水平移动速度。
  • accel:角色的加速度。
  • xSpeed:角色当前的水平速度。
  • accLeft 和 accRight:布尔值,表示角色是否正在向左或向右加速。
  • halfScreenWidth:屏幕宽度的一半,用于边界检测。

2. 键盘事件监听

  • onKeyDown:当按下键盘时触发。根据按键值设置 accLeft 或 accRight。
  • onKeyUp:当释放键盘时触发。重置 accLeft 或 accRight,并停止角色的水平移动。

3. 速度更新

在 update 方法中:
  • 根据 accLeft 和 accRight 的状态,每帧更新角色的水平速度。
  • 使用 Math.abs 检查当前速度是否超过最大速度限制,并进行限制。

4. 位置更新

  • 根据当前水平速度更新角色的位置。
  • 使用边界检测防止角色越过屏幕边界。

关键点解析

1. 加速度与时间缩放

this.xSpeed -= this.accel * dt;
  • accel:加速度(单位:像素/秒²)。
  • dt:当前帧的时间间隔(单位:秒)。
  • accel * dt:当前帧的速度变化量(单位:像素/秒)。
  • 通过 dt 确保角色的移动速度与帧率无关,从而实现平滑的动画效果。

2. 速度限制

if (Math.abs(this.xSpeed) > this.maxMoveSpeed) {
    this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
}
  • 确保角色的速度不会超过最大限制。
  • 使用 Math.abs 获取速度的绝对值,并根据当前方向调整速度。

3. 边界检测

if (this.node.x > this.halfScreenWidth - halfNodeWidth) {
    this.node.x = this.halfScreenWidth - halfNodeWidth;
}
if (this.node.x < -this.halfScreenWidth + halfNodeWidth) {
    this.node.x = -this.halfScreenWidth + halfNodeWidth;
}
  • 防止角色越过屏幕边界。
  • 使用 halfScreenWidth 和 halfNodeWidth 计算边界位置。

总结

通过上述代码,我们实现了一个基于键盘输入的角色水平移动逻辑,并通过边界检测防止角色越过屏幕边界。这种实现方式简单且高效,适用于大多数 2D 游戏场景。
希望这篇博客能帮助你更好地理解如何在 Cocos Creator 中实现角色的移动控制和边界检测。如果你有任何问题或需要进一步的扩展,请随时留言讨论!

希望这篇博客对你有帮助!如果有任何需要调整的地方,请告诉我。
 
posted @ 2025-02-16 14:05  mdyyyds_blog  阅读(132)  评论(0)    收藏  举报