繁体   English   中英

Phaser:Key Down + Collision Kill

[英]Phaser: Key Down + Collision Kill

预警:半新手

基本上,如果用户左下方的光标和“令牌”与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++;

}

最终目标是重新创建这种类型的游戏

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM