0

我正在关注这个MelonJS教程。我正在熟悉 OOP——类、构造函数等……我对构造函数有一些疑问。

在以下代码片段中...

1) 是initmelonJS 的特殊功能(我通过 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')) {
4

1 回答 1

1

init是一个构造函数——使用 Object.extend() 方法创建的每个对象都将实现一个定义init方法的接口。

至于thisvs. me- 请参阅文档

  • me指 melonJS 游戏引擎 - 所以所有 melonJS 函数都定义在me命名空间中。

  • this将引用给this定上下文中的任何内容。例如,在您提供的代码片段中,它将引用播放器实体的实例。

于 2014-06-16T14:53:47.113 回答