Javascript变量变得未定义

Javascript变量变得未定义,javascript,html,Javascript,Html,我的代码无法正常工作。我使用XMLHttpRequest加载和解析JSON文件,这很好,但是在我的setup()函数之后,我无法对我定义的任何变量(如spriteDictionary)调用任何其他函数,即使在各种设置函数中,我可以读取这些变量,并且看起来都很好 有什么想法吗?在下面的示例中,当我调用console.log(parsedJSON)时;它是未定义的,这很奇怪,因为我可以在我的设置代码中读取它的内容。谢谢 <!DOCTYPE html> <html> <

我的代码无法正常工作。我使用XMLHttpRequest加载和解析JSON文件,这很好,但是在我的setup()函数之后,我无法对我定义的任何变量(如spriteDictionary)调用任何其他函数,即使在各种设置函数中,我可以读取这些变量,并且看起来都很好

有什么想法吗?在下面的示例中,当我调用console.log(parsedJSON)时;它是未定义的,这很奇怪,因为我可以在我的设置代码中读取它的内容。谢谢

<!DOCTYPE html>

<html>
<head>
<title>Page Title</title>
</head>
<body id="body">
</body>

<script type="application/x-javascript">
var parsedJSON;   
var ctx;
var canvas;
var atlas;
var sprites = [];
var spriteDictionary = {};

var sprite = function(name, atl, x, y, w, h) {
    this.name = name;
    this.atlas = atl;
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.cx =  -w/2.0;
    this.cy = -h/2.0;

}

function setup() {
    var body = document.getElementById("body");

    canvas = document.createElement("canvas");
    canvas.width = 1200;
    canvas.height =720;
    body.appendChild(canvas);

    var ctx = canvas.getContext('2d');

    ctx.fillStyle="#000000";
    ctx.fillRect(0,0,1200,720);

    var xhr = new XMLHttpRequest();
    xhr.open("GET","game_gfx.json",true);
    xhr.onload = function (){
        parsedJSON = JSON.parse(this.responseText);
        load_assets(parsedJSON);
    }
    xhr.send();
    ctx = canvas.getContext('2d');

}

function load_assets(pJSON) {
    atlas = new Image();
    atlas.onload = function() {
        console.log("atlas loaded");
    }
    atlas.src= pJSON['meta']['image'];
    var frame;
    for (var i in pJSON['frames']){
        frame = pJSON['frames'][i];
        spriteDictionary[frame['filename']] =  new sprite(frame['filename'],atlas,frame['frame']['x'],frame['frame']['y'],frame['frame']['w'],frame['frame']['h']);
        i++;
    }
}

setup();

console.log(parsedJSON);

</script>


</html>

页面标题
var-parsedJSON;
var-ctx;
var帆布;
var图谱;
变量sprites=[];
var spriteDictionary={};
变量sprite=函数(名称、atl、x、y、w、h){
this.name=名称;
this.atlas=atl;
这个.x=x;
这个。y=y;
这个.w=w;
这个,h=h;
这个.cx=-w/2.0;
该系数为-h/2.0;
}
函数设置(){
var body=document.getElementById(“body”);
canvas=document.createElement(“canvas”);
画布宽度=1200;
canvas.height=720;
body.appendChild(画布);
var ctx=canvas.getContext('2d');
ctx.fillStyle=“#000000”;
ctx.fillRect(0,01200720);
var xhr=new XMLHttpRequest();
open(“GET”,“game_gfx.json”,true);
xhr.onload=函数(){
parsedJSON=JSON.parse(this.responseText);
加载_资产(parsedJSON);
}
xhr.send();
ctx=canvas.getContext('2d');
}
函数加载资源(pJSON){
atlas=新图像();
atlas.onload=函数(){
控制台日志(“加载了atlas”);
}
src=pJSON['meta']['image'];
var框架;
for(pJSON['frames']中的var i){
frame=pJSON['frames'][i];
spriteDictionary[frame['filename']=新精灵(frame['filename'],atlas,frame['frame']['x'],frame['frame']['y'],frame['frame']['w'],frame['frame']['h']);
i++;
}
}
设置();
console.log(parsedJSON);

不能将异步调用视为同步调用

在Ajax调用返回之前,您正在调用console.log行

onload侦听器中的一个简单console.log语句将向您显示这一点

xhr.onload = function (){
    console.log("I AM HERE!");
    ...
原木看起来像

undefined
"I AM HERE"
"atlas loaded"

type=“application/x-javascript”
的目的是什么?不用了。目的是科莫多编辑自动把它扔进去,我懒得把它删除。啊,谢谢。所以当我调用其他命令时,我的XHR没有找到。我感谢你的帮助。