【问题标题】:Phaser.js Game objects collide as intended, but collision callback isn'tPhaser.js 游戏对象按预期碰撞,但碰撞回调不是
【发布时间】:2016-09-11 22:48:09
【问题描述】:

我正在制作一个简单的游戏,其中一个球物体与一个子弹物体撞击。对象正确碰撞,但回调function(addscore()),包含在对象的同一collides() 函数中,只被调用一次(很可能在对象第一次创建期间)。

这是create() 函数的代码sn-p。碰撞部分在底部:

create: function() { 

        this.cursors = this.game.input.keyboard.createCursorKeys();

        //background
        this.cloud = game.add.sprite(50, 100, 'cloud');
        this.cloud.scale.setTo(.4,.4);
        this.cloud1 = game.add.sprite(250, 200, 'cloud');
        this.cloud1.scale.setTo(.5,.5);
        this.grass = game.add.sprite(0, 470, 'grass');
        game.stage.backgroundColor = '#71c5cf';

        //Ball and cannon
        game.physics.startSystem(Phaser.Physics.P2JS);
        game.physics.p2.restitution = .9;
        this.ball = game.add.sprite(200, 245, 'ball');
        this.cannon = game.add.sprite(200, 490, 'cannon');
        this.ball.anchor.setTo(0.4, 0.4);
        this.cannon.anchor.setTo(0.5, 0.5);
        this.cannon.scale.setTo(.4,.4);
        this.ball.scale.setTo(.4,.4);
        game.physics.p2.enable(this.ball);
        this.ball.body.setCircle(29);
        this.game.debug.body(this.ball)
        //gravity and bounce, collision
        this.game.physics.p2.gravity.y = 1500; 

        this.ballCollisionGroup = this.game.physics.p2.createCollisionGroup();
        this.bulletCollisionGroup = this.game.physics.p2.createCollisionGroup();
        this.game.physics.p2.updateBoundsCollisionGroup();
        this.ball.body.setCollisionGroup(this.ballCollisionGroup);
        this.ball.body.collides([this.bulletCollisionGroup], this.addscore);

        this.bullet = game.add.group();

        this.bullet.createMultiple(20, 'bullet');

        this.bullet.callAll('events.onOutOfBounds.add', 'events.onOutOfBounds', this.resetbullet);
        this.bullet.callAll('anchor.setTo', 'anchor', 0.1, 0.1);
        this.bullet.callAll('scale.setTo', 'scale', .1, .1);
        this.bullet.setAll('checkWorldBounds', true);
        this.bullet.enableBody = true;
        this.bullet.physicsBodyType = Phaser.Physics.P2JS;
        game.physics.p2.enable(this.bullet);


        this.bullet.forEach(function(child){
        child.body.setCircle(7);
        child.body.setCollisionGroup(this.bulletCollisionGroup);
        child.body.collides([this.ballCollisionGroup]);
        child.body.collideWorldBounds=false;
    }, this);
    },

您可以在此处查看游戏:http://gabrnavarro.github.io/Volleyball.js。 查看源代码以查看整个代码。谢谢你的帮助。 :)

【问题讨论】:

    标签: javascript collision-detection phaser-framework


    【解决方案1】:

    查看您在此处发布的代码片段,乍一看,一切看起来都不错。但是,当我在 gh 上查看您的代码时,我发现您没有将 addScore 作为回调传递,而是直接调用它。

    更改 main.js 的第 67 行

    this.ball.body.collides([this.bulletCollisionGroup], this.addscore(), this);

    this.ball.body.collides([this.bulletCollisionGroup], this.addscore, this);

    应该做的伎俩。

    【讨论】:

    • 本帖中的第 67 行已更改为您建议的行。我忘了在 gh-pages 上推送更新的代码。对于那个很抱歉。无论如何,问题仍然存在,尽管 addcore 函数根本没有被调用。我现在在gh上上传了最新的代码,你现在可以再试一次。
    【解决方案2】:

    我对 P2 物理没有任何经验,所以这可能是完全错误的。使用街机物理时,组在创建函数中初始化,但在更新函数中检查碰撞。

    所以你要做的就是移动:

        this.ball.body.collides([this.bulletCollisionGroup], this.addscore, this);
    

    到您的更新函数(以及检查冲突的任何其他代码)。

    再说一次,我对 p2 物理系统没有任何经验,所以这可能是不正确的。

    抱歉,如果这没有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多