移相器重叠不起作用
Phaser overlap doesnt work
为什么我的 Phaser.js 的重叠有时有效,有时却无效。
我为我的角色和障碍启用了街机。
这是代码。
function create(){
...//other sprite
obs = game.add.group();
obs.enableBody = true;
obstacle = obs.create(800, game.world.height - 68, 'obstacle');
obstacle.scale.setTo(0.5,0.5);
game.physics.arcade.enable(obstacle);
obstacle.events.onOutOfBounds.add(function(obstacle) {
obstacle.kill();
}, this);
}
function update(){
...
game.physics.arcade.collide(character, obstacle, gameOver, null, this);
}
function generateObs(){//its where i generate new obstacle, and is my biggest suspect as well
obstacle = obs.create(800, game.world.height - 68, 'obstacle');
obstacle.scale.setTo(0.5,0.5);
game.physics.arcade.enable(obstacle);
obstacle.body.velocity.x = -300;
}
非常感谢
在为您提供可能的问题解决方案之前:
- 没有必要将物理学应用于在其中创建的元素
已经有物理学的小组。
- 如果你需要知道两具尸体
重叠但你不需要模拟完整的碰撞使用 'overlap'.
试试这个:
var obs;
function create(){
//Active Physics ARCADE
game.physics.startSystem(Phaser.Physics.ARCADE);
...//other sprite
obs = game.add.group();
obs.enableBody = true;
obs.physicsBodyType = Phaser.Physics.ARCADE;
var obstacle = obs.create(800, game.world.height - 68, 'obstacle');
obstacle.scale.setTo(0.5,0.5);
obstacle.events.onOutOfBounds.add(function(obstacle) {
obstacle.kill();
}, this);
}
function update(){
...
//Use the group, since the callback will automatically pass the group element that overlaps with the player. A group is used to handle multiple elements, otherwise use only one variable :)
game.physics.arcade.overlap(character, obs, gameOver, null, this);
/*If you want your obstacle to move in the game you must implement this in the Update function
You can access an element of the group, for this there are multiple methods one of them is to get the element from the element's exists attribute
var obstacle = obs.getFirstExists(true);
if (obstacle) { obstacle.body.velocity.x = -300; }
*/
}
function gameOver(character, obstacle) {
//From here you have access to the element that collides with the player (you can evaluate with a condition if it is the obstacle you want)
//if (obstacle.name == 'x') { console.log(obstacle); }
console.log('Game Over!');
}
function generateObs(){
var obstacle = obs.create(800, game.world.height - 68, 'obstacle');
obstacle.scale.setTo(0.5,0.5);
obstacle.body.velocity.x = -300;
}
为什么我的 Phaser.js 的重叠有时有效,有时却无效。 我为我的角色和障碍启用了街机。 这是代码。
function create(){
...//other sprite
obs = game.add.group();
obs.enableBody = true;
obstacle = obs.create(800, game.world.height - 68, 'obstacle');
obstacle.scale.setTo(0.5,0.5);
game.physics.arcade.enable(obstacle);
obstacle.events.onOutOfBounds.add(function(obstacle) {
obstacle.kill();
}, this);
}
function update(){
...
game.physics.arcade.collide(character, obstacle, gameOver, null, this);
}
function generateObs(){//its where i generate new obstacle, and is my biggest suspect as well
obstacle = obs.create(800, game.world.height - 68, 'obstacle');
obstacle.scale.setTo(0.5,0.5);
game.physics.arcade.enable(obstacle);
obstacle.body.velocity.x = -300;
}
非常感谢
在为您提供可能的问题解决方案之前:
- 没有必要将物理学应用于在其中创建的元素 已经有物理学的小组。
- 如果你需要知道两具尸体 重叠但你不需要模拟完整的碰撞使用 'overlap'.
试试这个:
var obs;
function create(){
//Active Physics ARCADE
game.physics.startSystem(Phaser.Physics.ARCADE);
...//other sprite
obs = game.add.group();
obs.enableBody = true;
obs.physicsBodyType = Phaser.Physics.ARCADE;
var obstacle = obs.create(800, game.world.height - 68, 'obstacle');
obstacle.scale.setTo(0.5,0.5);
obstacle.events.onOutOfBounds.add(function(obstacle) {
obstacle.kill();
}, this);
}
function update(){
...
//Use the group, since the callback will automatically pass the group element that overlaps with the player. A group is used to handle multiple elements, otherwise use only one variable :)
game.physics.arcade.overlap(character, obs, gameOver, null, this);
/*If you want your obstacle to move in the game you must implement this in the Update function
You can access an element of the group, for this there are multiple methods one of them is to get the element from the element's exists attribute
var obstacle = obs.getFirstExists(true);
if (obstacle) { obstacle.body.velocity.x = -300; }
*/
}
function gameOver(character, obstacle) {
//From here you have access to the element that collides with the player (you can evaluate with a condition if it is the obstacle you want)
//if (obstacle.name == 'x') { console.log(obstacle); }
console.log('Game Over!');
}
function generateObs(){
var obstacle = obs.create(800, game.world.height - 68, 'obstacle');
obstacle.scale.setTo(0.5,0.5);
obstacle.body.velocity.x = -300;
}