Phaser:Key Down + Collision Kill

时间:2017-02-24 00:56:39

标签: javascript collision-detection phaser-framework

预警:半新手

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

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

另外一个注意事项:截至目前,我正在使用随机的spawn循环删除“令牌”。我宁愿使用定时模式进行令牌丢弃。如果您对此有任何建议,请分享。谢谢:]

1 个答案:

答案 0 :(得分:0)

不确定,所以这是一个猜测,但你正在应用&#39;这个&#39;的最后一个参数。功能&#39; lfthit&#39;这是在gumballGoGo.Preloader.prototype对象之外。您在控制台中遇到什么错误?