2017-08-10 53 views
0

我想從變種武器拍一個'子彈'朝某個方向射擊,這個子彈實際上是一個口袋妖怪的球,因爲我只是在做一個練習遊戲。如何讓武器'子彈'走向某個方向

我似乎無法讓'子彈'走向我想要的方向,我輸入了:weapon.body.velocity.x = -100;在:if(cursors.left.isDown) 但這不起作用,當我按任何鍵時,屏幕將凍結。

請幫我讓'子彈'走向我想要的方向。

var items; 
 
var game; 
 
var player; 
 
var weapon; 
 
var cursors; 
 
var fireButton; 
 

 

 
function addItems() { 
 
    items = game.add.physicsGroup(); 
 
    createItem(100, 400, 'coin'); 
 
} 
 

 
function createItem(left, top, image) { 
 
    var item = items.create(left, top, image); 
 
    item.animations.add('spin'); 
 
    item.animations.play('spin', 10, true); 
 
} 
 

 
function itemHandler(player, item) { 
 
    item.kill(); 
 

 
} 
 

 

 
window.onload = function() { 
 
    game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); 
 

 
    function preload() { 
 

 
    game.stage.backgroundColor = ('#424242'); 
 

 
    game.load.spritesheet('coin', 'coin.png', 36, 44); 
 
    game.load.spritesheet('player', 'hero.png', 64, 64); 
 
    game.load.spritesheet('bullet', 'Pokeball.png'); 
 

 

 
    } 
 

 

 
    function create() { 
 

 
     player = this.game.add.sprite(100, 200, 'player'); 
 
    // ANIMATION FOR PLAYER CONTROLS 
 
     down = player.animations.add('down', [0,1,2,3], 10, true); 
 
     left = player.animations.add('left', [4,5,6,7], 10, true); 
 
     right = player.animations.add('right', [8,9,10,11], 10, true); 
 
     up = player.animations.add('up', [12,13,14,15], 10, true); 
 

 
     // enable physics in the game (can't go through walls, gravity, etc.) 
 

 
     game.physics.enable(player, Phaser.Physics.ARCADE); 
 
     game.physics.startSystem(Phaser.Physics.P2JS); 
 
     game.physics.startSystem(Phaser.Physics.ARCADE); 
 
     game.physics.p2.enable(player); 
 

 

 
     player.body.setSize(30, 45, 16, 12); 
 
     player.body.immovable = false; 
 
     // enable keyboard arrows for controls 
 
     cursors = game.input.keyboard.createCursorKeys(); 
 
     // camera will follow the character 
 
     game.camera.follow(player); 
 

 
     addItems(); 
 

 

 
     // Creates 1 single bullet, using the 'bullet' graphic 
 
     weapon = game.add.weapon(1, 'bullet'); 
 

 
     // The bullet will be automatically killed when it leaves the world bounds 
 
     weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; 
 

 
     // Because our bullet is drawn facing up, we need to offset its rotation: 
 

 

 
     // The speed at which the bullet is fired 
 
     weapon.bulletSpeed = 400; 
 

 

 

 
     game.physics.arcade.enable(player); 
 

 
     // Tell the Weapon to track the 'player' Sprite, offset by 14px horizontally, 0 vertically 
 
     weapon.trackSprite(player, 30, 0); 
 

 
     cursors = this.input.keyboard.createCursorKeys(); 
 

 
     fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR); 
 

 
    } 
 

 
    function update() { 
 

 
     game.physics.arcade.overlap(player, items, itemHandler); 
 

 
    // PLAYER CONTROLS 
 
     player.body.velocity.set(0); 
 
     // player presses left key 
 
     if (cursors.left.isDown) 
 
     { 
 
      player.body.velocity.x = -100; 
 
      player.play('left'); 
 
     } 
 
     // player presses right key 
 
     else if (cursors.right.isDown) 
 
     { 
 
      player.body.velocity.x = 100; 
 
      player.play('right'); 
 
     } 
 
     // player presses up key 
 
     else if (cursors.up.isDown) 
 
     { 
 
      player.body.velocity.y = -100; 
 
      player.play('up'); 
 
     } 
 
     // player presses down key 
 
     else if (cursors.down.isDown) 
 
     { 
 
      player.body.velocity.y = 100; 
 
      player.play('down'); 
 
     } 
 
     // player does not press anything 
 
     else 
 
     { 
 
      player.animations.stop(); 
 
     } 
 

 
     if (fireButton.isDown) 
 
     { 
 
      weapon.fire(); 
 
     } 
 

 
    } 
 

 
    function render() { 
 

 
    weapon.debug(); 
 

 
    } 
 

 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 
\t <head> 
 
\t \t <meta charset="utf-8"> 
 
\t \t <title>Simple Canvas Game</title> 
 
    <style> 
 
     html { 
 
     background: black 
 
     } 
 
     canvas { 
 
     margin: auto; 
 
     } 
 
    </style> 
 
\t </head> 
 
\t <body> 
 
    <script src="phaser.js"></script> 
 
\t \t <script src="game.js"></script> 
 
\t </body> 
 
</html>

+0

試試這個; weapon.body.velocity.x - = 100; –

+0

@KrisJ。當我點擊任何鍵時它仍然凍結。 – hannacreed

+0

你沒有得到任何錯誤? –

回答

0

嘗試改變

weapon.trackSprite(player, 30, 0); 

weapon.trackSprite(player, 30, 0, true); 

移相器的文件說,如果你傳遞true作爲第四個參數,那麼它會跟隨精靈的旋轉。

編輯:您目前沒有做任何旋轉你的精靈,那麼改變你在哪裏檢查球員改變精靈的方向的角度:

if (cursors.left.isDown) 
    { 
     player.angle = 180; 
    } 
    else if (cursors.right.isDown) 
    { 
     player.angle = -90; 
    } 
    else if (cursors.up.isDown) 
    { 
     player.angle = 90; 
    } 
    // player presses down key 
    else if (cursors.down.isDown) 
    { 
     player.angle = 0; 
    } 

這也將旋轉你的玩家精靈,所以也許這不是你想要的。

+0

現在它只是拍攝左大聲笑@VasilyKushakov – hannacreed

+0

我編輯了我的答案來解決這個問題。我認爲子彈跟蹤你的精靈的旋轉,所以如果你在玩家按下箭頭鍵的時候旋轉精靈,它應該可以工作,或者至少讓你朝着正確的方向前進。 –

+0

這是有效的,除了我不希望我的播放器翻轉和周圍哈哈,有沒有一種方法可以旋轉精靈的邊界,而不是圖像本身?這樣子彈就可以聽到正在旋轉的精靈邊界的旋轉,但圖像會保持不變? @VasilyKushakov – hannacreed

相關問題