我正在关注这个MelonJS教程。我正在熟悉 OOP——类、构造函数等……我对构造函数有一些疑问。
在以下代码片段中...
1) 是init
melonJS 的特殊功能(我通过 API 阅读,http ://melonjs.github.io/docs/me.ObjectEntity.html ,似乎不是瓜),还是 JavaScript?它似乎是在创建 playerEntity 时自动调用的......在调用init
什么?
2) 好像有时this
叫( this.setVelocity
),有时me
叫( me.game.viewport.follow
)。你什么时候给每个人打电话?
3)对于速度,为什么需要乘法accel * timer tick
?:this.vel.x -= this.accel.x * me.timer.tick;
/*-------------------
a player entity
-------------------------------- */
game.PlayerEntity = me.ObjectEntity.extend({
/* -----
constructor
------ */
init: function(x, y, settings) {
// call the constructor
this.parent(x, y, settings);
// set the default horizontal & vertical speed (accel vector)
this.setVelocity(3, 15);
// set the display to follow our position on both axis
me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
},
/* -----
update the player pos
------ */
update: function() {
if (me.input.isKeyPressed('left')) {
// flip the sprite on horizontal axis
this.flipX(true);
// update the entity velocity
this.vel.x -= this.accel.x * me.timer.tick;
} else if (me.input.isKeyPressed('right')) {