您需要定義這些屬性爲靜態的,那麼你可以很容易地訪問它像這樣,
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;
}
}
這編譯爲:
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
您將在哪裏設置CANVAS_WIDTH的值? –
在用Bullet類導入模塊的遊戲類中GameObjects = module(「GameObjects」) – Nikos
在Game類中有CANVAS_WIDTH,需要在Bullet類中訪問它。我對嗎? –