I’ve been testing the new typescript with VS 2012 but I have this issue in my first project:
I have pasted this JS into my ts file but I can’t access the Sprite object is its in the anonymous scope. How can I access it from typescript.
(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;
}());
Hope this explains my issue!
Thanks for any assistance
The first error the compiler gives you is here…
It detects that there is no property or function on the Sprite object called
load.The second error is here…
The Sprite constructor takes five parameters and you have only passed 1.
Third up…
The Window object doesn’t have a property named Sprite.
All of the other problems are variations of this issue.
Here is a suggestion to get things working with minimal changes. The better solution would be to rewrite your Sprite as a class in TypeScript