2014-03-05 32 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是在给定的上下文中。例如,在您提供的代码片段中,它将引用玩家实体的 实例。