javascript - 在游戏中排除某个方向的问题
问题描述
我正在努力实现我的玩家如何只能在一个方向上行走......例如:当玩家在 x 方向上行走时,他不能只在 y 方向上在 -x 方向上行走,对于 y 也是如此。例如,如果他在 -y 中行走,他就无法更改为 y。
谢谢你的帮助!下面是我如何切换播放器的方向:
this.changeDirection = function(direction) {
switch(direction) {
case "Up":
this.xSpeed = 0;
this.ySpeed = -scale;
break;
case "Down":
this.xSpeed = 0;
this.ySpeed = scale;
break;
case "Left":
this.xSpeed = -scale;
this.ySpeed = 0;
break;
case "Right":
this.xSpeed = scale;
this.ySpeed = 0;
break;
}
}
解决方案
对于Up
and ,在每个赋值块之前Down
添加:if (this.ySpeed === 0)
case "Up":
if (this.ySpeed === 0) {
this.xSpeed = 0;
this.ySpeed = -scale;
}
break;
Left
并为&做类似的事情Right
。
推荐阅读
- postgresql - 将保存点与 JOOQ 和 JUnit 一起使用
- python - 在我的代码中,由于某种原因出现“索引超出范围”的错误,我实际上只是启动了 Python,因此不知道如何解决这个问题
- javascript - js防止textarea模糊
- c++ - Connect 4 Winning 横向功能
- javascript - 多步形式的 Javascript 验证
- javascript - 生成具有要求的随机密码
- java - 确保已安装 Java 并在 Amazon SageMaker Jupyter Notebook 中为“java”设置 PATH
- flutter - 除非我在 initState() 函数中放置断点,否则 Admob 横幅不会显示
- java - JDK 中的 SSL 证书执行从 1.8.0_51 升级到 1.8.0_202
- python - 如何提取具有相同名称的 XML 标记/实体?