2016-07-22 120 views
1

有人能解釋我的錯誤在哪裏,試圖讓我的球員與世界的界限相撞,而不僅僅是扔牆。我嘗試了自定義方法,但這不是我想要的效果。 當我添加「game.world.setBounds(0,0,x,y);」創建函數我的遊戲不啓動。我是相位器js的開始,所以也許我做錯了什麼。這裏是我的代碼:Phaser js與世界的界限碰撞

"use strict"; 
 

 
var game = new Phaser.Game(1000, 800, Phaser.CANVAS, "game_div"); 
 

 
var spaceField, 
 
    backgroundSpeed, 
 
    player, 
 
    cursors, 
 
    bullets, 
 
    bulletsTime = 0, 
 
    fireButton, 
 
    bullet, 
 
    bulletSound; 
 

 
var mainState = { 
 
    preload: function() { 
 
     //id 
 
     game.load.image("starfield", "images/space.png"); 
 
     game.load.image("player", "images/playerSmall.png"); 
 
     game.load.image("bullet", "images/fire.png"); 
 

 
     // audio 
 

 
     game.load.audio("bulletSound", "sounds/blaster.mp3"); 
 
    }, 
 

 
    create: function() { 
 
     // Full screen when clicking with the mouse on the screen 
 
     game.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT; 
 
     game.input.onDown.add(goFull, this); 
 
     // background 
 
     spaceField = game.add.tileSprite(0, 0, 1000, 800, "starfield"); 
 
     backgroundSpeed = 2; 
 
     game.physics.setBoundsToWorld(); 
 

 
     // player spaceship + adding physics + player movement 
 
     player = game.add.sprite(game.world.centerX, game.world.centerY + 300, "player"); 
 
     game.physics.enable(player, Phaser.Physics.ARCADE); 
 
     cursors = game.input.keyboard.createCursorKeys(); 
 

 
     // Fire bullets 
 
     bullets = game.add.group(); 
 
     bullets.enableBody = true; 
 
     bullets.physicsBodyType = Phaser.Physics.ARCADE; // Enabling physics for bullets 
 
     bullets.createMultiple(30, "bullet"); 
 
     bullets.setAll("anchor.x", 0.5); 
 
     bullets.setAll("anchor.y", 1); 
 
     bullets.setAll("outOfBoundsKill", true); // Checks if the bullet is off screen so we can reuse it 
 
     bullets.setAll("checkWorldBounds", true); 
 

 
     fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); 
 
     bulletSound = game.add.audio("bulletSound"); 
 
    }, 
 

 
    update: function() { 
 
     // Making scrolling background 
 
     spaceField.tilePosition.y += backgroundSpeed; 
 
     player.body.velocity.x = 0; // Everytime when key is not pressed the player does not move 
 
     player.body.velocity.y = 0; 
 

 
     // Checking which key is pressed 
 

 
     if (cursors.up.isDown) { 
 
      player.checkWorldBounds = true; 
 
      player.events.onOutOfBounds.add(playerOutOfBoundsTop, this); 
 
      player.body.velocity.y = -350; 
 
     } 
 

 
     if (cursors.down.isDown) { 
 
      player.checkWorldBounds = true; 
 
      // player.events.onOutOfBounds.add(playerOutOfBoundsBottom, this); 
 
      player.body.velocity.y = 350; 
 
     } 
 

 
     if (cursors.left.isDown) { 
 
      player.body.velocity.x = -350; 
 
     } 
 

 
     if (cursors.right.isDown) { 
 
      player.body.velocity.x = 350; 
 
     } 
 

 
     if (fireButton.isDown) { 
 
      fireBullet(); 
 
     } 
 
    } 
 
}; 
 

 
function fireBullet() { 
 
    if (game.time.now > bulletsTime) { 
 
     bullet = bullets.getFirstExists(false); 
 

 
     if (bullet) { 
 
      bullet.reset(player.x + 28, player.y); 
 
      bullet.bulletAngleOffset = 90; 
 
      bullet.bulletAngleVariance = 30; 
 
      bullet.body.velocity.y = -400; 
 
      bulletsTime = game.time.now + 200; 
 
      bulletSound.play(); 
 
     } 
 
    } 
 
} 
 

 
function playerOutOfBoundsTop(player) { 
 

 
    // Move the Spaceship to the top of the screen again 
 
    player.reset(player.x, 60); 
 

 
} 
 

 
/*function playerOutOfBoundsBottom(player) { 
 
// Move the spaceship to the bottom of the screen again 
 
player.reset(60, player.x); 
 
} 
 
*/ 
 

 
function goFull() { 
 

 
    if (game.scale.isFullScreen) { 
 
     game.scale.stopFullScreen(); 
 
    } else { 
 
     game.scale.startFullScreen(false); 
 
    } 
 

 
} 
 
//id 
 
game.state.add('mainState', mainState); 
 

 
game.state.start("mainState");

回答

2

如果您希望您的播放器不出去你可以設置該屬性collideWorldBounds到真正的遊戲界,在create功能:

player.body.collideWorldBounds=true; 

看看一個this移相器示例和docs

但是如果你想在播放器出界時做一些不同的事情,你可以在更新循環中添加一個函數(比如你按下UP鍵時的那個函數)。

例如,使用此代碼時,你的球員是出界,將在遊戲中的中間再次出現:

var game = new Phaser.Game(1000, 800, Phaser.CANVAS, "game_div"); 

var spaceField, 
    backgroundSpeed, 
    player, 
    cursors, 
    bullets, 
    bulletsTime = 0, 
    fireButton, 
    bullet, 
    bulletSound; 

var mainState = { 
    preload: function() { 
     //id 
     game.load.image("starfield", "images/space.png"); 
     game.load.image("player", "images/playerSmall.png"); 
     game.load.image("bullet", "images/fire.png"); 

     // audio 

     game.load.audio("bulletSound", "sounds/blaster.mp3"); 
    }, 

    create: function() { 
     // Full screen when clicking with the mouse on the screen 
     game.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT; 
     game.input.onDown.add(goFull, this); 
     // background 
     spaceField = game.add.tileSprite(0, 0, 1000, 800, "starfield"); 
     backgroundSpeed = 2; 
     game.physics.setBoundsToWorld(); 

     // player spaceship + adding physics + player movement 
     player = game.add.sprite(game.world.centerX, game.world.centerY + 300, "player"); 
     game.physics.enable(player, Phaser.Physics.ARCADE); 
     cursors = game.input.keyboard.createCursorKeys(); 

     //player.body.collideWorldBounds=true; 

     // Fire bullets 
     bullets = game.add.group(); 
     bullets.enableBody = true; 
     bullets.physicsBodyType = Phaser.Physics.ARCADE; // Enabling physics for bullets 
     bullets.createMultiple(30, "bullet"); 
     bullets.setAll("anchor.x", 0.5); 
     bullets.setAll("anchor.y", 1); 
     bullets.setAll("outOfBoundsKill", true); // Checks if the bullet is off screen so we can reuse it 
     bullets.setAll("checkWorldBounds", true); 

     fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); 
     bulletSound = game.add.audio("bulletSound"); 
    }, 

    update: function() { 
     // Making scrolling background 
     spaceField.tilePosition.y += backgroundSpeed; 
     player.body.velocity.x = 0; // Everytime when key is not pressed the player does not move 
     player.body.velocity.y = 0; 


     player.events.onOutOfBounds.add(playerOutOfBounds, this); 

     // Checking which key is pressed 
     if (cursors.up.isDown) { 
      player.checkWorldBounds = true; 
      player.body.velocity.y = -350; 
     } 

     if (cursors.down.isDown) { 
      player.checkWorldBounds = true; 
      // player.events.onOutOfBounds.add(playerOutOfBoundsBottom, this); 
      player.body.velocity.y = 350; 
     } 

     if (cursors.left.isDown) { 
      player.body.velocity.x = -350; 
     } 

     if (cursors.right.isDown) { 
      player.body.velocity.x = 350; 
     } 

     if (fireButton.isDown) { 
      fireBullet(); 
     } 
    } 
}; 

function fireBullet() { 
    if (game.time.now > bulletsTime) { 
     bullet = bullets.getFirstExists(false); 

     if (bullet) { 
      bullet.reset(player.x + 28, player.y); 
      bullet.bulletAngleOffset = 90; 
      bullet.bulletAngleVariance = 30; 
      bullet.body.velocity.y = -400; 
      bulletsTime = game.time.now + 200; 
      bulletSound.play(); 
     } 
    } 
} 

function playerOutOfBounds(player) { 

    // Move the Spaceship to the top of the screen again 
    player.reset(player.x, game.world.centerX); 
    player.reset(player.y, game.world.centerY); 

} 

/*function playerOutOfBoundsBottom(player) { 
// Move the spaceship to the bottom of the screen again 
player.reset(60, player.x); 
} 
*/ 

function goFull() { 

    if (game.scale.isFullScreen) { 
     game.scale.stopFullScreen(); 
    } else { 
     game.scale.startFullScreen(false); 
    } 

} 
//id 
game.state.add('mainState', mainState); 

game.state.start("mainState"); 
+0

謝謝你,我正在尋找有關的信息 - 我不知道我錯過了相位器文檔信息頁面:)。 – tabula