有没有一种方便的方法可以在模块中访问全局变量,没有编译器错误,即下面使用的CANVAS_WIDTH?
export class Bullet {
x: number = 22;
y: number = 22;
constructor (speed: number) {
this.xVelocity = speed;
}
inBounds() {
return this.x >= 0 && this.x <= CANVAS_WIDTH &&
this.y >= 0 && this.y <= CANVAS_HEIGHT;
};
}
}
Run Code Online (Sandbox Code Playgroud)
Raj*_*l 웃 27
您需要将这些属性定义为静态,然后您可以像这样轻松地访问它,
export class Game {
static canvas: JQuery;
static CANVAS_WIDTH: number;
static CANVAS_HEIGHT: number;
bullet: Bullet;
constructor(canvasElem: JQuery) {
Game.canvas = canvasElem;
Game.CANVAS_WIDTH = Game.canvas.width();
Game.CANVAS_HEIGHT = Game.canvas.height();
}
}
export class Bullet {
x: number = 22;
y: number = 22;
public inBounds() {
// accessing static properties
return this.x >= 0 && this.x <= Game.CANVAS_WIDTH && this.y >= 0 && this.y <= Game.CANVAS_HEIGHT;
}
}
Run Code Online (Sandbox Code Playgroud)
这编译为:
define(["require", "exports"], function(require, exports) {
var Game = (function () {
function Game(canvasElem) {
Game.canvas = canvasElem;
Game.CANVAS_WIDTH = Game.canvas.width();
Game.CANVAS_HEIGHT = Game.canvas.height();
}
return Game;
})();
exports.Game = Game;
var Bullet = (function () {
function Bullet() {
this.x = 22;
this.y = 22;
}
Bullet.prototype.inBounds = function () {
// accessing static properties
return this.x >= 0 && this.x <= Game.CANVAS_WIDTH && this.y >= 0 && this.y <= Game.CANVAS_HEIGHT;
};
return Bullet;
})();
exports.Bullet = Bullet;
});
//# sourceMappingURL=dhdh.js.map
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
52450 次 |
最近记录: |