我正在关注这个MelonJS教程。我熟悉OOP类,构造函数等......我对构造函数有一些疑问。JavaScript构造函数和melonJS问题
在下面的代码片段...
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')) {