我正在關注這個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')) {