我一直在用 VS 2012 测试新的打字稿,但在我的第一个项目中遇到了这个问题:
我已将此 JS 粘贴到我的 ts 文件中,但我无法访问 Sprite 对象,它位于匿名范围内。如何从打字稿访问它。
(function () {
function LoaderProxy() {
return {
draw: $.noop,
fill: $.noop,
frame: $.noop,
update: $.noop,
width: null,
height: null
};
}
function Sprite(image, sourceX, sourceY, width, height) {
sourceX = sourceX || 0;
sourceY = sourceY || 0;
width = width || image.width;
height = height || image.height;
return {
draw: function(canvas, x, y) {
canvas.drawImage(
image,
sourceX,
sourceY,
width,
height,
x,
y,
width,
height
);
},
fill: function(canvas, x, y, width, height, repeat) {
repeat = repeat || "repeat";
var pattern = canvas.createPattern(image, repeat);
canvas.fillColor(pattern);
canvas.fillRect(x, y, width, height);
},
width: width,
height: height
};
};
Sprite.load = function(url, loadedCallback) {
var img = new Image();
var proxy = LoaderProxy();
img.onload = function() {
var tile = Sprite(this);
$.extend(proxy, tile);
if(loadedCallback) {
loadedCallback(proxy);
}
};
img.src = url;
return proxy;
};
var spriteImagePath = "images/";
window.Sprite = function(name, callback) {
return Sprite.load(spriteImagePath + name + ".png", callback);
};
window.Sprite.EMPTY = LoaderProxy();
window.Sprite.load = Sprite.load;
}());
希望这能解释我的问题!
感谢您的帮助