由于我的游戏中的某些原因,我可以检测到碰撞,但是身体实际上并没有碰撞它们通过。
这是我创建播放器的方式:
function createPlayer () {
player = game.add.sprite(200, 200, 'playerimg');
player.anchor.setTo(0.5,0.5);
game.physics.p2.enableBody(player, true);
player.body.data.shapes[0].sensor = true;
game.camera.follow(player);
player.body.onBeginContact.add(player_coll, this);
console.log(player.body)
}
player_coll函数在碰撞时可以很好地触发。
var tree_object = function (id, type, startx, starty, value) {
this.id = id;
this.posx = startx;
this.posy = starty;
this.tree = game.add.sprite(this.posx, this.posy, 'tree');
this.tree.type = 'tree';
this.tree.id = id;
game.physics.p2.enableBody(this.tree, true);
this.tree.body_size = 10;
this.tree.body.data.gravityScale = 0;
this.tree.body.data.shapes[0].sensor = true;
}
function player_coll(body,test){
if(body.sprite.type=="tree"){
console.log(body)
console.log("collided with tree")
}
}
我的预加载功能:
function preload(){
game.stage.disableVisibilityChange = true; // mouse browserdan çıkınca uyutma.2 ekran test için.
game.scale.scaleMode = Phaser.ScaleManager.RESIZE;
game.world.setBounds(0, 0, gameProperties.gameWidth,
gameProperties.gameHeight);
//Fizik için P2JS kullanıcaz.
game.physics.startSystem(Phaser.Physics.P2JS);
//fizik dünyasının sınırlarını yaratır.(duvarlar)
game.physics.p2.setBoundsToWorld(true, true, true, true, true)
//Y nin yerçekimini 0 a eşitle böylece yere düşmeyecek.
game.physics.p2.gravity.y = 0;
// Yerçekimini tamamen kapat.
game.physics.p2.applyGravity = false;
game.physics.p2.enableBody(game.physics.p2.walls, false);
game.load.image('playerimg', '/assets/player.png');
game.load.image('background', 'assets/lake.png');
game.load.image("wood", "/assets/wood.png");
game.load.image("tree", "/assets/tree.png");
// Collision(çarpma) detectionu aç.
game.physics.p2.setImpactEvents(true);
}
您在图片中看到为什么它们通过了?