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メソッドを定義するインターフェイスを実装します。

this対については-ドキュメントmeを参照してください:

  • meは melonJS ゲーム エンジンを参照するため、すべての melonJS 関数はme名前空間で定義されます。

  • thisthis指定されたコンテキストにあるものは何でも参照します。たとえば、提供されたコード スニペットでは、プレーヤー エンティティのインスタンスを参照します。

于 2014-06-16T14:53:47.113 に答える