我看过两个关于 Easeljs 的不同教程,一个是 David Rousset 的,另一个是 Lee Brimelow 的。我不确定哪个更好用,以及有什么区别。示例 1(大卫·鲁塞特):
(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 和一个只是一个基本的位图。
在示例 1 中:
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 中运行 new Player() 时实际发生了什么。
4)将tick设置为Player的属性意味着你必须在你的主tick函数中调用这个tick函数,在easljs中使用Ticker类的内置onTick事件处理程序不是更好(如示例2中所做的那样) )?
以上哪些模式是“最佳实践”,为什么?
此外,这两种模式都依赖于创建 Player 对象的 main.js(并且 Player 对象被设置为窗口的属性)。为了使所有内容远离全局范围或能够在例如 node.js 上使用此代码,最好将 main.js 也包装在一个对象中,类似地,并将此 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
}
这可能/可行吗?