2017-02-24 90 views
1

爲什麼我的Phaser.js的重疊有時候並沒有時間。 我已經爲我的角色和障礙啓用了街機。 這裏是代碼。Phaser重疊不起作用

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; 
 
}

THKS很多

回答

0

之前給你一個可能的解決問題的方法:

  • 沒有必要運用物理學中 創建的元素已經擁有物理學的團體。
  • 如果您需要知道兩個物體 是否重疊,但您不需要模擬完整的碰撞使用「重疊」。

試試這個:

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; 
} 
+0

它有很大幫助,THKS隊友! – AndresJay