2016-12-03 53 views
1

我對JavaScript有點新,並且最近一直在和Phaser一起玩。所以我正在建造一個無限的側面卷軸,除了我的玩家不會與牆壁碰撞,一切正常。這兩個精靈都啓用了物理功能,我嘗試了多種解決方案,但其中沒有一個能夠工作。你能幫我解決嗎?Sprite vs Group Collider不能在移相器中使用enableBody設置爲true

function bloxo() 
 
{ 
 
\t var game = new Phaser.Game(1200, 600, Phaser.CANVAS, 'gameStage', { preload: preload, create: create, update: update }); 
 
\t var prevHole = 3; 
 

 
\t function preload() { 
 
\t \t game.load.image('bloxoDown','../bloxo/assets/images/bloxoDown.png'); 
 
\t \t game.load.image('bloxoUp','../bloxo/assets/images/bloxoUp.png'); 
 
\t \t game.load.image('wall','../bloxo/assets/images/platform.png',400,200); 
 

 
\t \t var space; 
 
\t \t var esc; 
 
\t \t var player; 
 
\t \t var walls; 
 
\t \t var score; 
 
\t } 
 

 
\t function create() { 
 

 
\t \t //Canvas With a White Bacground and Physics is Created 
 
\t \t game.stage.backgroundColor = "#ffffff"; 
 
\t \t game.physics.startSystem(Phaser.Physics.ARCADE); 
 

 

 
\t \t //Sets the initial Score. 
 
\t \t score = 0; \t 
 

 
\t \t //Sets how fast the tiles move 
 
\t \t tileSpeed = -300; 
 

 
\t \t tileWidth = game.cache.getImage('wall').width; 
 
\t \t tileHeight = game.cache.getImage('wall').height;; 
 

 
\t \t //Keys for User Input are created 
 
\t \t space = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); 
 
\t \t esc = game.input.keyboard.addKey(Phaser.Keyboard.ESC); 
 

 
\t \t //Adds Bloxo to the game as a sprite. 
 
\t \t player = game.add.sprite(200,200,'bloxoDown'); 
 
\t \t player.scale.setTo(0.6, 0.6); 
 
\t \t game.physics.enable(player, Phaser.Physics.ARCADE); 
 
\t \t player.body.collideWorldBounds = true; 
 
\t \t player.body.immovable = true; 
 

 
\t \t //Walls Group is created 
 
\t \t walls = game.add.physicsGroup(); 
 
\t \t walls.createMultiple(50, 'wall'); 
 
\t \t walls.enableBody = true; 
 

 

 
\t \t game.physics.arcade.overlap(player, walls,null,this) 
 
\t \t 
 
\t \t game.physics.arcade.collide(player,walls,gameOver); 
 

 
\t \t // Stop the following keys from propagating up to the browser 
 
\t \t game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR, Phaser.Keyboard.ESC,]); 
 

 
\t \t //Unpausing Function 
 
\t \t window.onkeydown = function(event) 
 
\t  { 
 
\t   if (esc.onDown && (esc.timeDown > 2000)) 
 
\t   { \t 
 
\t   \t if(game.paused) 
 
\t   \t { 
 
\t    \t game.paused = !game.paused; 
 
\t    \t pauseLbl.destroy(); 
 
\t    \t } 
 
\t   } 
 
\t  } 
 

 
\t  //Add an initial platform 
 
\t \t addWall(); 
 
    
 
\t \t //Add a platform every 3 seconds 
 
\t \t var timerWorld = game.time.events.loop(500, addWall); 
 
\t } 
 

 
\t function update() { 
 

 
\t \t if (space.isDown) 
 
    \t { 
 
     \t player.body.y -=5; 
 
     \t bloxoUp(); 
 
    \t } 
 
    \t else 
 
    \t { 
 
    \t \t player.body.y +=5; 
 
    \t \t bloxoDown(); 
 
    \t } 
 

 
    \t if(esc.isDown) 
 
    \t { 
 
    \t \t pauseGame(); \t 
 
    \t } 
 
\t } 
 

 
\t function bloxoUp() 
 
\t { 
 
\t \t player.loadTexture('bloxoUp'); 
 
\t } 
 

 
\t function bloxoDown() 
 
\t { 
 
\t \t player.loadTexture('bloxoDown'); 
 
\t } 
 

 
\t function pauseGame() 
 
\t { 
 
\t \t game.paused = true; 
 
\t \t pauseLbl = game.add.text(500, 300, 'Game Paused', { font: '30px Roboto', fill: '#aaaaaa' }); 
 
\t } 
 

 
\t function addTile(x,y) 
 
\t { 
 
\t  //Get a tile that is not currently on screen 
 
\t  var tile = walls.getFirstDead(); 
 
\t 
 
\t  //Reset it to the specified coordinates 
 
\t  tile.reset(x,y); 
 
\t  tile.body.velocity.x = tileSpeed; 
 
\t  tile.body.immovable = true; 
 
\t 
 
\t  //When the tile leaves the screen, kill it 
 
\t  tile.checkWorldBounds = true; 
 
\t  tile.outOfBoundsKill = true;  
 
\t } 
 

 
\t function addWall() 
 
\t { 
 
\t  //Speed up the game to make it harder 
 
\t  tileSpeed -= 1; 
 
\t  score += 1; 
 
\t 
 
\t  //Work out how many tiles we need to fit across the whole screen 
 
\t  var tilesNeeded = Math.ceil(game.world.height/tileHeight); 
 

 
\t  //Add a hole randomly somewhere 
 
\t  do 
 
\t  { 
 
\t  \t var hole = Math.floor(Math.random() * (tilesNeeded - 2)) + 1; 
 
\t \t }while((hole > (prevHole + 2)) && (hole < (prevHole - 2))); 
 

 
\t \t prevHole = hole; 
 

 
\t  //Keep creating tiles next to each other until we have an entire row 
 
\t  //Don't add tiles where the random hole is 
 
\t  for (var i = 0; i < tilesNeeded; i++){ 
 
\t   if (i != hole && (i != hole+1 && i != hole-1) && (i != hole+2 && i != hole-2)){ 
 
\t    addTile(game.world.width, i * tileHeight); 
 
\t   }  
 
\t  } 
 
\t } 
 

 
\t function gameOver() 
 
\t { 
 
\t \t console.log("player hit"); 
 
\t \t player.kill(); 
 
\t \t game.state.start(game.state.current); 
 
\t } 
 
}

回答

1

您剛纔對collide呼叫轉移到你的更新方法:

game.physics.arcade.collide(player, walls, gameOver); 

看看下面(我已經調整的畫布預覽可運行的代碼片斷,對不起)或Fiddle

var game = new Phaser.Game(450, 150, Phaser.CANVAS, 'gameStage', { 
 
    preload: preload, 
 
    create: create, 
 
    update: update 
 
}); 
 
var prevHole = 3; 
 

 
function preload() { 
 
    game.load.image('bloxoDown', '../bloxo/assets/images/bloxoDown.png'); 
 
    game.load.image('bloxoUp', '../bloxo/assets/images/bloxoUp.png'); 
 
    game.load.image('wall', '../bloxo/assets/images/platform.png', 400, 100); 
 

 
    var space; 
 
    var esc; 
 
    var player; 
 
    var walls; 
 
    var score; 
 
} 
 

 
function create() { 
 

 
    //Canvas With a White Bacground and Physics is Created 
 
    game.stage.backgroundColor = "#ffffff"; 
 
    game.physics.startSystem(Phaser.Physics.ARCADE); 
 

 

 
    //Sets the initial Score. 
 
    score = 0; 
 

 
    //Sets how fast the tiles move 
 
    tileSpeed = -300; 
 

 
    tileWidth = game.cache.getImage('wall').width; 
 
    tileHeight = game.cache.getImage('wall').height;; 
 

 
    //Keys for User Input are created 
 
    space = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); 
 
    esc = game.input.keyboard.addKey(Phaser.Keyboard.ESC); 
 

 
    //Adds Bloxo to the game as a sprite. 
 
    player = game.add.sprite(200, 200, 'bloxoDown'); 
 
    player.scale.setTo(0.6, 0.6); 
 
    game.physics.enable(player, Phaser.Physics.ARCADE); 
 
    player.body.collideWorldBounds = true; 
 
    player.body.immovable = true; 
 

 
    //Walls Group is created 
 
    walls = game.add.physicsGroup(); 
 
    walls.createMultiple(50, 'wall'); 
 
    walls.enableBody = true; 
 

 

 
    game.physics.arcade.overlap(player, walls, null, this) 
 
    // remove your call to collide 
 

 
    // Stop the following keys from propagating up to the browser 
 
    game.input.keyboard.addKeyCapture([Phaser.Keyboard.SPACEBAR, Phaser.Keyboard.ESC, ]); 
 

 
    //Unpausing Function 
 
    window.onkeydown = function(event) { 
 
    if (esc.onDown && (esc.timeDown > 2000)) { 
 
     if (game.paused) { 
 
     game.paused = !game.paused; 
 
     pauseLbl.destroy(); 
 
     } 
 
    } 
 
    } 
 

 
    //Add an initial platform 
 
    addWall(); 
 

 
    //Add a platform every 3 seconds 
 
    var timerWorld = game.time.events.loop(500, addWall); 
 
} 
 

 
function update() { 
 

 
    if (space.isDown) { 
 
    player.body.y -= 5; 
 
    bloxoUp(); 
 
    } else { 
 
    player.body.y += 5; 
 
    bloxoDown(); 
 
    } 
 
             
 
    // move your collide call here 
 
    game.physics.arcade.collide(player, walls, gameOver); 
 

 
    if (esc.isDown) { 
 
    pauseGame(); 
 
    } 
 
} 
 
             
 
function bloxoUp() { 
 
    player.loadTexture('bloxoUp'); 
 
} 
 

 
function bloxoDown() { 
 
    player.loadTexture('bloxoDown'); 
 
} 
 

 
function pauseGame() { 
 
    game.paused = true; 
 
    pauseLbl = game.add.text(500, 300, 'Game Paused', { 
 
    font: '30px Roboto', 
 
    fill: '#aaaaaa' 
 
    }); 
 
} 
 

 
function addTile(x, y) { 
 
    //Get a tile that is not currently on screen 
 
    var tile = walls.getFirstDead(); 
 

 

 
    //Reset it to the specified coordinates 
 
    if (tile) { 
 
    tile.reset(x, y); 
 
    tile.body.velocity.x = tileSpeed; 
 
    tile.body.immovable = true; 
 

 
    //When the tile leaves the screen, kill it 
 
    tile.checkWorldBounds = true; 
 
    tile.outOfBoundsKill = true; 
 
    } 
 

 
} 
 

 
function addWall() { 
 
    //Speed up the game to make it harder 
 
    tileSpeed -= 1; 
 
    score += 1; 
 

 
    //Work out how many tiles we need to fit across the whole screen 
 
    var tilesNeeded = Math.ceil(game.world.height/tileHeight); 
 
    var prevHole; 
 
    //Add a hole randomly somewhere 
 
    do { 
 
    var hole = Math.floor(Math.random() * (tilesNeeded - 2)) + 1; 
 
    } while ((hole > (prevHole + 2)) && (hole < (prevHole - 2))); 
 

 
    prevHole = hole; 
 

 
    //Keep creating tiles next to each other until we have an entire row 
 
    //Don't add tiles where the random hole is 
 
    for (var i = 0; i < tilesNeeded; i++) { 
 
    if (i != hole && (i != hole + 1 && i != hole - 1) && (i != hole + 2 && i != hole - 2)) { 
 
     addTile(game.world.width, i * tileHeight); 
 
    } 
 
    } 
 
} 
 

 
function gameOver() { 
 
    console.log("player hit"); 
 
    player.kill(); 
 
    game.state.start(game.state.current); 
 
}
canvas{ 
 
    border: 5px solid #333; 
 
    margin-left:25px; 
 
    margin-top:25px; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.6.2/phaser.min.js"></script>

+0

我會吻你,如果我可以。非常感謝,我不明白爲什麼它不能工作幾個小時,因爲我正在研究的教程說碰撞是在創建函數中。再次非常感謝! –

相關問題