javascript - 移相器 : Key Down + Collision Kill

标签 javascript collision-detection phaser-framework

预警:半新手

基本上,如果用户将左光标向下并且“ token ”与lftRect发生碰撞,我想杀死该 token 。由于某种原因,我的碰撞终止回调函数无法正常工作(下面是相关代码):

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

还有一点需要注意:截至目前,我正在使用随机生成循环来丢弃“ token ”。我宁愿使用定时模式来掉落 token 。如果您对此有任何建议,也请分享。谢谢:]

最佳答案

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

关于javascript - 移相器 : Key Down + Collision Kill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42428955/

相关文章:

javascript - 内容变化的模态

iphone - 根据碰撞影响移动图像

file - 如何在 Yesod 中为静态文件启用 'Access-Control-Allow-Origin' header ?

JavaScript 游戏可切换 Sprite

c++ - 处理质量 Spring 系统之间碰撞的好方法

javascript - 如何追踪Phaser网页游戏的用户点击?

javascript - 使用 wordpress 制作 Javascript 幻灯片

javascript - 如何禁用chrome native "max-scroll"覆盖?

javascript - 如何使用 JQuery 一次更新数组变量上的多个复选框值

java - 球是否接触到其中一个 Racket ?