Weapon problems

#1

Hello! I have copied a weapon fire ship from the phaser github and paste it on my file editor. I have edited the images so that they are stored in my computer but when I open up the file on localhost I get the message (Uncaught TypeError: Cannot read property ‘weapon’ of undefined) but I have initialized the variable weapon I do not see where the problem is. Can you take a look and help me. Thank you for helping me.
Here is me code:

Page Title
*{
    background-color: blueviolet;
    margin: 0;
    text-align: center;
}
<script>
    
    
var config = {
    type: Phaser.AUTO,
    width: 700,
    height: 600,
    scene: {
        preload: preload,
        create: create,
        update: update
    },
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y : 100},
            debug : false
        }
    }
};

var game = new Phaser.Game(config);

function preload() {

this.load.image('bullet', '../assets/bomb.png');
this.load.spritesheet('ship', '../assets/dude.png', { frameWidth: 32, frameHeight: 48 });

}

var sprite;
var weapon;
var cursors;
var fireButton;

function create() {
this.add.image(100, 100, ‘ship’);
// Creates 30 bullets, using the ‘bullet’ graphic
weapon = game.add.weapon(30, ‘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;

//  Speed-up the rate of fire, allowing them to shoot 1 bullet every 60ms
weapon.fireRate = 60;

sprite = this.add.sprite(320, 500, 'ship');

game.physics.arcade.enable(sprite);

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

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

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

}

function update() {

sprite.body.velocity.x = 0;

if (cursors.left.isDown)
{
    sprite.body.velocity.x = -200;
}
else if (cursors.right.isDown)
{
    sprite.body.velocity.x = 200;
}

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

}

function render() {

weapon.debug();

}

</script>
0 Likes

#2

Hi @mohkan,
As far as I know, in phaser 3 there isn’t weapon class, unless there is a plugin but in which case you haven’t included it in your config.
In the examples page of phaser 2 for weapon there is one almost like this, but it uses a different constructor. That’s why I think you’re mixing Phaser 3 with Phaser 2, which have incompatible codes.
You can see the phaser 3 examples here: http://labs.phaser.io/.
Good luck!!

0 Likes