Sign in to follow this  

Make Bullet shoot in desired direction

Recommended Posts

I am trying to make a 'bullet' from the var weapon shoot in a certain direction, this bullet is actually a pokemon ball as I am just making a practice game.

I cannot seem to make the 'bullet' go in the direction that I would like it to, I entered: weapon.body.velocity.x = -100; under the the: if (cursors.left.isDown) but this did not work, when I pressed any key the screen would just freeze.

Please help me make the 'bullet' go in the direction I want.


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');'spin', 10, true);

function itemHandler(player, item) {


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 =, 200, 'player');
        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, weapon, Phaser.Physics.ARCADE);
        game.physics.p2.enable(player, weapon);

        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;


        //  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:
        weapon.bulletAngleOffset = 90;

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


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

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

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


  function update() {

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

        // player presses left key
        if (cursors.left.isDown)
            player.body.velocity.x = -100;

        // player presses right key
        else if (cursors.right.isDown)
            player.body.velocity.x = 100;
        // player presses up key
        else if (cursors.up.isDown)
            player.body.velocity.y = -100;
        // player presses down key
        else if (cursors.down.isDown)
            player.body.velocity.y = 100;
        // player does not press anything

        if (fireButton.isDown) {



  function render() {





Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.