首页 \ 问答 \ Phaser缓存加载问题(Phaser cache loading problems)

Phaser缓存加载问题(Phaser cache loading problems)

我正在用相位进行游戏。 我正在加载背景图片,其信息(文件位置)存储在JSON文件中。 当我尝试加载它时,背景是黑色和空白的,并在控制台中获得:

Phaser.Cache.getImage:在缓存中未找到密钥“background0”。

以下是我的代码中的相关摘录:

function create() {

    //>Load JSON file and background images found inside the file
    $.getJSON("levels.json", function(json) {
        for (var i = 0; i < json.levels.length; i++) {
            game.load.image('background' + i.toString(), json.levels[i].background);
        }
        game.load.start();
   });

    back_layer = game.add.group();
    var i = 0;
    var level_finished = 0;

    $.getJSON("levels.json", function(json) {
        if (i < json.levels.length) {
            console.log("Level " + (i + 1).toString());
            var current_background = back_layer.create(0, 0, 'background' + i.toString());

            check = setInterval(function() {
                if (level_finished == 1) {
                    i++;
                    current_background.destroy();
                    clearInterval(check);
                }
            }, 500)
        }
    });
}

这里是JSON文件:

{"levels":[
    {
        "background": "assets/img/Back.png",
        "portals": [
            {
                "locationX": 400,
                "locationY": 450,
                "toX": 100,
                "toY": 200,
                "spinSpeed": 1
            },
            {
                "locationX": 50,
                "locationY": 200,
                "toX": 100,
                "toY": 450,
                "spinSpeed": 2
            }
        ]
    }
]}

使用Chrome,Firefox和Opera进行测试,每次打开页面时,它都会随机出现错误,或者加载背景并正常工作。 我正在使用WAMP在本地托管该页面。


I am making a game in phaser. I am loading a background image, and its information (file location) is stored within a JSON file. When I try to load it, the background is black and empty, and in the console I get:

Phaser.Cache.getImage: Key "background0" not found in Cache.

Here is the relevant extract from my code:

function create() {

    //>Load JSON file and background images found inside the file
    $.getJSON("levels.json", function(json) {
        for (var i = 0; i < json.levels.length; i++) {
            game.load.image('background' + i.toString(), json.levels[i].background);
        }
        game.load.start();
   });

    back_layer = game.add.group();
    var i = 0;
    var level_finished = 0;

    $.getJSON("levels.json", function(json) {
        if (i < json.levels.length) {
            console.log("Level " + (i + 1).toString());
            var current_background = back_layer.create(0, 0, 'background' + i.toString());

            check = setInterval(function() {
                if (level_finished == 1) {
                    i++;
                    current_background.destroy();
                    clearInterval(check);
                }
            }, 500)
        }
    });
}

And here is the JSON file:

{"levels":[
    {
        "background": "assets/img/Back.png",
        "portals": [
            {
                "locationX": 400,
                "locationY": 450,
                "toX": 100,
                "toY": 200,
                "spinSpeed": 1
            },
            {
                "locationX": 50,
                "locationY": 200,
                "toX": 100,
                "toY": 450,
                "spinSpeed": 2
            }
        ]
    }
]}

Testing with Chrome, Firefox, and Opera, and every time I open the page, it seems to randomly have the error, or load the background and work fine. I am using WAMP to locally host the page.


原文:https://stackoverflow.com/questions/41408766
更新时间:2023-12-27 09:12

最满意答案

编译器能够将开关语句优化为查找表,并在处理枚举时对文字进行编译时检查。 话虽如此,你应该总是使用switch语句进行3次比较。 因此你的代码变成 -

private static final String EMPLOYEE_ID = "employeeid";
private static final String FIRST_NAME = "firstname";
private static final String LAST_NAME = "lastName";
private static final String GENDER = "gender";
private static final String MOBILE_NO = "mobileno";
private static final String EMAIL_ID = "emailid";

Person p =new Person ();
for(String element: myList) {
switch (element.toLowerCase()) {
  case EMPLOYEE_ID: p.setemployeeID("");
    break;
  case FIRST_NAME: p.setfirstName("");
    break;
  case LAST_NAME: p.setlastName("");
    break;
  case GENDER: p.setgender("");
    break;
  case MOBILE_NO: p.setMobileNo("");
    break;
  case EMAIL_ID: p.setEmailID("");
    break;
}

}

The compiler is able to optimize switch statements into a lookup table and perform compile-time checking for literals when dealing with enumerations. That being said, you should always use switch statements for over 3 comparisons. Hence your code becomes-

private static final String EMPLOYEE_ID = "employeeid";
private static final String FIRST_NAME = "firstname";
private static final String LAST_NAME = "lastName";
private static final String GENDER = "gender";
private static final String MOBILE_NO = "mobileno";
private static final String EMAIL_ID = "emailid";

Person p =new Person ();
for(String element: myList) {
switch (element.toLowerCase()) {
  case EMPLOYEE_ID: p.setemployeeID("");
    break;
  case FIRST_NAME: p.setfirstName("");
    break;
  case LAST_NAME: p.setlastName("");
    break;
  case GENDER: p.setgender("");
    break;
  case MOBILE_NO: p.setMobileNo("");
    break;
  case EMAIL_ID: p.setEmailID("");
    break;
}

}

相关问答

更多
  • 您已经找到了一个未定义行为的示例,导致优化器执行意外操作! GCC可以看到,如果循环运行10次,则存在未定义的行为。 此代码将通过filenames[10]即filenames[10]的第2到第11个元素)写入filenames[1] 。 但是filenames长度为10个元素,因此最后一个是未定义的行为。 因为不允许你有未定义的行为,它可以假设循环在它到达10之前会以其他方式停止(也许你有一个修改版本的printf会调用exit ?)。 并且它看到如果循环在它到达10之前仍然会停止,那么没有必要让代码只运 ...
  • version PostgreSQL 8.4.4 那是个问题。 请阅读此内容并考虑一个小版本,以获取有关安全漏洞和错误的修复: http://www.postgresql.org/support/versioning/ 这些有时会包含引入性能问题的错误。 要查看您将获得的修复,您可以在此处查看8.4.5至8.4.11注释: http://www.postgresql.org/docs/8.4/static/release.html 一些基于RAM的设置可能太低,但是不知道系统上有多少RAM以及其他运行的是 ...
  • 首先,您正在查看字节码编译器的输出。 典型的Java字节码编译器进行的优化很少。 重量级优化由JIT编译器在运行时完成。 所以你看到的javap -c显示的是未经优化的字节码。 我不明白为什么我们需要第1和第2行? 您正在查看未经优化的字节码。 前2条指令将1分配给局部变量b 。 我们可以看到局部变量可以很容易地被优化掉。 但是字节码编译器( javac )并没有打扰。 我听说Java Runtime环境会计算函数调用并在超过一定数量的调用时编译函数。 这就是典型的Hotspot JVM的工作原理。 这是否 ...
  • 迭代地构建数据data.frame逐行缩放。 相反,我建议将每个迭代的输出创建为一个list ,然后在最后绑定。 我没有你的数据,所以这是未经测试的。 我也会假设Matt对1,632个SQL查询的评论是不可避免的(因为这可能是一个很大的瓶颈)。 底线:您的问题领域可能(1)千次以上的SQL查询,以及(2)用rbind迭代地构建您的结果。 这预先构建了嵌套for循环中的所有迭代。 eg <- expand.grid(Year=x, classProduct=d, product=y, classBranch= ...
  • 编译器能够将开关语句优化为查找表,并在处理枚举时对文字进行编译时检查。 话虽如此,你应该总是使用switch语句进行3次比较。 因此你的代码变成 - private static final String EMPLOYEE_ID = "employeeid"; private static final String FIRST_NAME = "firstname"; private static final String LAST_NAME = "lastName"; private static fina ...
  • 你应该删除这个: int length = CAArray.Length; 并用这个替换循环: for (int i= 0; i < CAArray.Length; i++) { Value += CAArray[i].ClassB.Value * CAArray[i].Multiplier; } 像原始代码一样存储长度实际上会减慢 C#代码的速度 (我知道是违反直觉的)。 这是因为如果在for循环中直接使用Array.Length,那么抖动将跳过对循环的每次迭代进行数组边界检查。 另外,我强 ...
  • 问题中对newaxis版本的解释:这些不是矩阵乘法,ndarray乘法是逐个元素乘以广播 。 step [:,newaxis,newaxis]为num_steps x 1 x 1,point [newaxis,:,:]为1 x num_points x num_dimensions。 将形状(num_steps x 1 x 1)和(1 x num_points x num_dimensions)的ndarray一起广播将起作用,因为广播规则是每个维度应该是1或相同; 它只是意味着“重复数组的维度1与其他数组 ...
  • 手工制作的微基准测试很容易被愚弄 - 你永远不知道他们实际测量的是什么。 这就是为什么有像JMH这样的特殊工具。 但是让我们分析一下原始手工制作基准会发生什么: static class HDouble { double value; } public static void main(String[] args) { primitive(); wrapper(); } public static void primitive() { long start = Syste ...
  • 您将获得明确答案的唯一方法是运行这两种方法并对其进行分析以查看它们需要多长时间。 话虽如此,我相信运行一个具有一百个名称值对的UPDATE语句将比运行100个UPDATE语句更快。 The only way you'll get a definitive answer is to run both of these methods and profile it to see how long they take. With that said, I'm confident that running one ...
  • 通过将数据存储在数组中,在阵列上运行以及将数据存储回电子表格,您应该会看到性能的显着提升。 就像是: Dim data As Variant Dim result As Variant data = Range(Cells(2, 1), Cells(Row_Number, 1)) Redim result (1 To Row_Number, 1 To 7) As Variant 现在不是从Cells(i, 1)读取,而是从data(i, 1)读取而不是写入Cells(i, n)而是写入result(i, ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。