【问题标题】:Phaser: Key Down + Collision KillPhaser: Key Down + Collision Kill
【发布时间】:2017-07-14 17:07:58
【问题描述】:

预警:半新手

基本上,如果用户将左光标向下并且“令牌”与 lftRect 碰撞,我想杀死该令牌。由于某种原因,我的碰撞终止回调函数不起作用(以下是相关代码):

gumballGoGo.Preloader = function(game){
    this.player = null;
    this.ground = null;
    //this.tokens = null;
    this.ready = false;
};

var cursors, lftInit, rghtInit, ground, testDrp, sprite, tokens, rect, lftRect, ctrRect, rghtRect, lftToken;
var total = 0;

function lftHit(lftRect, lftToken) {
    if ( lftInit == true ){
        lftToken.kill()   
    }
};

gumballGoGo.Preloader.prototype = {
    preload: function(){
    },

    create: function() {

        // LFT BOX
        lftRect = this.add.sprite(0, this.world.height - 150, null);
        this.physics.enable(lftRect, Phaser.Physics.ARCADE);
        lftRect.body.setSize(100, 100, 0, 0);


        // CNTR BOX
        ctrRect = this.add.sprite(100, this.world.height - 150, null);
        this.physics.enable(ctrRect, Phaser.Physics.ARCADE);
        ctrRect.body.setSize(100, 100, 0, 0);


        // RGHT BOX
        rghtRect = this.add.sprite(200, this.world.height - 150, null);
        this.physics.enable(rghtRect, Phaser.Physics.ARCADE);
        rghtRect.body.setSize(100, 100, 0, 0);


        // INIT TOKEN GROUP  
        tokens = this.add.group();
        tokens.enableBody = true;
        this.physics.arcade.enable(tokens);

        testDrp = tokens.create(125, -50, 'token');
        testDrp.body.gravity.y = 300;



        // CONTROLS
        this.cursors = this.input.keyboard.createCursorKeys();

    },

    update: function() {
        this.ready = true;

        if (this.cursors.left.isDown)
            {
                lftInit = true;
            }
        else if (this.cursors.right.isDown)
            {
                rghtInit = true;
            }
        else
            {
                lftInit, rghtInit = false;

            }

        if (total < 20)
            {
                tokenSpawn();
            }

        this.physics.arcade.collide(lftRect, lftToken, lftHit, null, this);    

    }
};


function tokenSpawn() {

    lftToken = tokens.create(25, -(Math.random() * 800), 'token');

    lftToken.body.gravity.y = 300;

    total++;

}

最终目标是recreate this type of gameplay

另外一点:到目前为止,我正在使用随机生成循环来丢弃“令牌”。我宁愿使用定时模式来丢弃令牌。如果您对此有任何建议,也请分享。谢谢:]

【问题讨论】:

    标签: javascript collision-detection phaser-framework


    【解决方案1】:

    不确定,所以这是一个猜测,但您将“this”的最后一个参数应用于 gumballGoGo.Preloader.prototype 对象之外的函数“lfthit”。您在控制台中遇到什么错误?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      相关资源
      最近更新 更多