首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript构造函数和melonJS问题

JavaScript构造函数和melonJS问题
EN

Stack Overflow用户
提问于 2014-03-06 07:27:19
回答 1查看 140关注 0票数 0

我正在学习这篇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;

代码语言:javascript
复制
    /*------------------- 
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')) {
EN

回答 1

Stack Overflow用户

发布于 2014-06-16 22:53:47

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

至于thisme的对比,请参阅documentation

  • me指的是melonJS游戏引擎--所以所有的melonJS函数都是在me中定义的,而this将在给定的上下文中引用。例如,在您提供的代码片段中,它将引用播放器实体的实例。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22211632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档