我看过两个关于Easeljs的教程,一个是David Rousset的,另一个是Lee Brimelow的。我不确定哪一个更好,以及它们之间的区别是什么。以下是David Rousset的示例:
(function (window) {
function Player(imgPlayer, x_start, x_end) {
this.initialize(imgPlayer, x_start, x_end);
}
Player.prototype = new createjs.BitmapAnimation();
// public properties:
Player.prototype.alive = true;
// constructor:
Player.prototype.BitmapAnimation_initialize = Player.prototype.initialize; //unique to avoid overiding base class
var quaterFrameSize;
Player.prototype.initialize = function (imgPlayer, x_end) {
var localSpriteSheet = new createjs.SpriteSheet({
images: [imgPlayer], //image to use
frames: { width:64, height:64, regX:32, regY: 32 },
animations: {
walk: [0, 9, "walk", 4]
}
});
createjs.SpriteSheetUtils.addFlippedFrames(localSpriteSheet, true, false, false);
this.BitmapAnimation_initialize(localSpriteSheet);
this.x_end = x_end;
quaterFrameSize = this.spriteSheet.getFrame(0).rect.width / 4;
// start playing the first sequence:
this.gotoAndPlay("idle"); //animate
this.isInIdleMode = true;
}
Player.prototype.tick = function () {
//specific tick function for the player
}
window.Player = Player;
} (window));
并且示例2(Lee Brimelow):
(function(window) {
function Player(){
// Adding the easeljs bitmap as a property of Player:
this.view = new createjs.Bitmap("assets/pics/player1.png")
// Setting som local parameters
var height = stage.canvas.height;
var width = stage.canvas.width;
var playerRadius = 70;
var offset = 200;
var x = 0;
var y = 0;
this.view.regX = this.view.regY = playerRadius;
// Adding the tickfunction below
this.view.onTick = tick;
}
function tick(e) {
//
}
window.Player = Player;
})(window);
请忽略一个使用BitmapAnimation而另一个只是基本的Bitmap。
在Example1中:
1)将以下行替换为相同吗:
Player.prototype.alive = true;
使用:
this.alive = true;
2) 以下术语含义是什么:
Player.prototype.BitmapAnimation_initialize = Player.prototype.initialize; //unique to avoid overiding base class
我不明白评论的意思...
3) 这行代码是否添加到初始化函数的开头:
Player.prototype = new createjs.BitmapAnimation();
我不确定在示例1中新建Player()时实际发生了什么。
4) 将tick作为Player的属性设置会导致你必须在主要的tick函数中调用此tick函数,使用easljs的内置onTick事件处理程序(如示例2中所做)是否更好?
上述模式中哪种是“最佳实践”,为什么?
此外,这两个模式都依赖于创建Player对象的main.js文件(并且Player对象被设置为window的属性)。为了避免全局范围内的影响或能够在像node.js这样的环境中使用此代码,将主要的main.js也包装成一个对象,并将此Main对象作为参数传递给函数,是否更好呢?
假设你制作了这个main js:
Main = {
init: function() {
//set up and create Player
var player = new Player;
},
//then adding som properties, variables to Main... for instance
propA: 0
}
你认为这是否可行?