2014-03-05 33 views
0

我正在關注這個MelonJS教程。我熟悉OOP類,構造函數等......我對構造函數有一些疑問。JavaScript構造函數和melonJS問題

在下面的代碼片段...

1)是一個init特殊melonJS功能(我讀直通API,http://melonjs.github.io/docs/me.ObjectEntity.html,似乎並沒有被瓜),或JavaScript?它似乎在playerEntity創建時自動調用...什麼叫init

2)看起來有時叫thisthis.setVelocity),有時叫meme.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')) { 

回答

1

init是一個構造 - 與該Object.extend()方法創建將實現定義一個init方法的接口的每一個對象。

對於thisme - 看到documentation

  • me指melonJS遊戲引擎 - 因此所有melonJS功能都在me命名空間中定義 。

  • this將參考任何this是在給定的上下文中。例如,在您提供的代碼片段中,它將引用玩家實體的 實例。