javascript - 我有一个代码无法识别的声明变量。为什么是这样?
问题描述
我正在编写游戏,这是其中的一部分。我希望程序循环浏览我上传的精灵运行的不同图片。我创建了一个变量,让程序知道正在运行的角色屏幕上显示的是哪张图片。程序无法识别它是一个正在声明的变量。我只是在使用 Javascript 时犯了一个简单的错误吗?
我试图在我的代码周围移动变量声明,并在其中放入不同的东西。似乎没有任何效果。
class Example1 extends Phaser.Scene {
constructor() {
super({key: "Example1"});
}
//variable for the current running stance which does not work
var playerBoard = 1;
preload() {
this.load.image('Background', 'assets/Background.jpg');
this.load.image('4 JUMP_000', 'assets/4 JUMP_000.png');
let run1 = this.load.image('3 RUN_000', 'assets/3 RUN_000.png');
let run2 = this.load.image('3 RUN_001', 'assets/3 RUN_001.png');
let run3 = this.load.image('3 RUN_002', 'assets/3 RUN_002.png');
let run4 = this.load.image('3 RUN_003', 'assets/3 RUN_003.png');
let run5 = this.load.image('3 RUN_004', 'assets/3 RUN_004.png');
}
/*another variable to help return to the first running stance when the sprite stops running*/
let runningStance = run1;
//function that switches the running stances
runningScene(x,y){
if(this.input.keyboard.on("keyDown_D")){
while(this.input.keyboard.on("keyDown_D")) {
if (this.playerBoard = 1) {
this.playerBoard1 = 2;
this.image = this.add.image(this.image.x, this.image.y, '3 RUN_001');
runningStance = run2;
run1.visable = false;
}
程序中的一些代码不包括在内,因为它会占用太多空间。不起作用的是变量的声明,因为它不允许 if 函数正常工作。我安装了移相器,你可以在代码中参考。输出应该隐藏起始图片并显示序列中的下一张,这将是 run2。
解决方案
尝试playerBoard
在构造函数中声明:
constructor() {
super({key: "Example1"});
this.playerBoard = 1;
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
同样的runningStance
,请查看上面的链接以检查课程的运作方式
推荐阅读
- sql - 用名称中的句点重命名clickhouse中的列
- javascript - 检查用户是否正在与 $in 运算符进行对话
- javascript - 基于组的对象组数组
- firebase - 我无法从 Firestore 获取数据“‘Future 实例’
'" - python - “超级”对象没有属性“__getattr__”,谁能帮帮我?
- angular - Angular如何拆分字符串
- java - 如何将变量从活动传递到片段?我对流行的解决方案有疑问
- javascript - 如何在文件中的 RPi 上的 javascript 之后获取完整的 html
- javascript - 当我进行贝宝付款时,它不会移动到数据库中。这可以纠正吗?
- java - @PreAuthorize 中的 Spring Boot 复杂拼写