首页 > 解决方案 > 我有一个代码无法识别的声明变量。为什么是这样?

问题描述

我正在编写游戏,这是其中的一部分。我希望程序循环浏览我上传的精灵运行的不同图片。我创建了一个变量,让程序知道正在运行的角色屏幕上显示的是哪张图片。程序无法识别它是一个正在声明的变量。我只是在使用 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。

标签: javascriptphaser-framework

解决方案


尝试playerBoard在构造函数中声明:

constructor() {
  super({key: "Example1"});
  this.playerBoard = 1;
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

同样的runningStance,请查看上面的链接以检查课程的运作方式


推荐阅读