我一直在用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;
}());
希望这能解释我的问题!
感谢您的帮助
答案 0 :(得分:0)
编译器给你的第一个错误就在这里......
Sprite.load
它检测到名为load
的Sprite对象上没有属性或函数。
第二个错误就在这里......
var tile = Sprite(this);
Sprite构造函数有五个参数,你只传递了1个。
第三次......
window.Sprite
Window对象没有名为Sprite的属性。
所有其他问题都是这个问题的变体。
这是一个建议,让事情变化很小。更好的解决方案是将您的Sprite重写为TypeScript中的类
var Sprite;
(function () {
function LoaderProxy() {
return {
draw: $.noop,
fill: $.noop,
frame: $.noop,
update: $.noop,
width: null,
height: null
};
}
Sprite = function (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/";
Sprite = function(name, callback) {
return Sprite.load(spriteImagePath + name + ".png", callback);
};
Sprite.EMPTY = LoaderProxy();
Sprite.load = Sprite.load;
}());