【问题标题】:I have a declared variable that the code is not recognizing. Why is this?我有一个代码无法识别的声明变量。为什么是这样?
【发布时间】:2019-04-10 03:43:59
【问题描述】:

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

【问题讨论】:

  • runningScene 在哪里调用/传递?
  • runningScene 可能未声明,顺便说一句,run1 和 run2 也未在任何地方声明

标签: javascript phaser-framework


【解决方案1】:

尝试在构造函数中声明playerBoard

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

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

runningStance 也是如此,请查看上面的链接以查看类的工作方式

【讨论】:

    【解决方案2】:

    这一行:

         if (this.playerBoard = 1) {
    

    不好有两个原因。首先,= 显然是指=====。其次,playerBoard 是一个变量,在这里你尝试访问和对象属性。前面几行你声明了var playerBoard = 1;,所以只需阅读它

         if (playerBoard === 1) {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      相关资源
      最近更新 更多