Phaser.js 游戏对象按预期发生碰撞,但碰撞回调并非如此
Phaser.js Game objects collide as intended, but collision callback isn't
我正在制作一个简单的游戏,球对象与子弹对象相撞。对象正确碰撞,但包含在对象的相同 collides()
函数中的回调 function(addscore())
仅被调用一次(很可能在对象的第一次创建期间)。
这里是 create()
函数的代码片段。碰撞部分在底部:
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。
查看源代码以查看整个代码。谢谢你的帮助。 :)
查看您在此处发布的代码片段,乍一看一切都很好。但是,当我在 gh 上查看您的代码时,我发现您没有将 addScore 作为回调传递,而是直接调用它。
从
更改main.js第67行
this.ball.body.collides([this.bulletCollisionGroup], this.addscore(), this);
至
this.ball.body.collides([this.bulletCollisionGroup], this.addscore, this);
应该可以解决问题。
我对 P2 物理没有任何经验,所以这可能是完全错误的。使用街机物理时,组在创建函数中初始化,但在更新函数中检查碰撞。
所以你要做的就是移动:
this.ball.body.collides([this.bulletCollisionGroup], this.addscore, this);
到您的更新函数(以及检查碰撞的任何其他代码)。
再说一次,我对p2物理系统没有任何经验,所以这可能是不正确的。
抱歉,如果这没有帮助。
我正在制作一个简单的游戏,球对象与子弹对象相撞。对象正确碰撞,但包含在对象的相同 collides()
函数中的回调 function(addscore())
仅被调用一次(很可能在对象的第一次创建期间)。
这里是 create()
函数的代码片段。碰撞部分在底部:
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。 查看源代码以查看整个代码。谢谢你的帮助。 :)
查看您在此处发布的代码片段,乍一看一切都很好。但是,当我在 gh 上查看您的代码时,我发现您没有将 addScore 作为回调传递,而是直接调用它。
从
更改main.js第67行this.ball.body.collides([this.bulletCollisionGroup], this.addscore(), this);
至
this.ball.body.collides([this.bulletCollisionGroup], this.addscore, this);
应该可以解决问题。
我对 P2 物理没有任何经验,所以这可能是完全错误的。使用街机物理时,组在创建函数中初始化,但在更新函数中检查碰撞。
所以你要做的就是移动:
this.ball.body.collides([this.bulletCollisionGroup], this.addscore, this);
到您的更新函数(以及检查碰撞的任何其他代码)。
再说一次,我对p2物理系统没有任何经验,所以这可能是不正确的。
抱歉,如果这没有帮助。