Jump to content

Shooting projectiles not working.


manekin
 Share

Recommended Posts

Hello,

This is my first project with Phaser, i'm trying to implement a shooting mechanism using this tutorial http://labs.phaser.io/edit.html?src=src\pools\bullets.js

I am confused as to how it works. How do I create the Bullet class object? Does this.add.group do it for me? I know it's probably a stupid question, but this is really new to me. Especially since neither examples nor API documentation explain how to do it using a class that extends Phase.GameObjects.Sprite

This is my (rubbish) code:

class Bullet extends Phaser.GameObjects.Sprite {
    constructor(config) {
        super(config.scene, config.x, config.y, config.image);
        config.scene.add.existing(this);
        config.scene.physics.world.enable(this);
        this.body.maxVelocity.x = 200;
        this.body.maxVelocity.y = 500;
        this.speed = Phaser.Math.GetSpeed(400, 1);
    }

    fire(x, y) {
        this.setPosition(x, y - 50);

        this.setActive(true);
        this.setVisible(true);
    }

    update(delta) {
        this.y -= this.speed * delta;

        if (this.y < -50) {
            this.setActive(false);
            this.setVisible(false);
        }
    }
}

 

class GameScene extends Phaser.Scene {
    constructor() {
        super({
            key: "GameScene"
        });

        this.cursors = null;
        this.platforms = null;
        this.soldier = null;
        this.bullets - null;
    }

    preload() {
        this.load.image("background", "../assets/bg_jungle.png");
        this.load.image("ground", "../assets/platform.png");
        this.load.image("soldier", "../assets/deadpool.png");
        this.load.image("bullet", "../assets/red_projectile_4.png");

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

    create() {
        this.add.image(game.config.width / 2, game.config.height / 2, "background");
        this.platforms = this.physics.add.staticGroup();
        this.platforms.create(200, 650, 'ground');
        this.platforms.create(600, 650, 'ground')
        this.platforms.create(1000, 650, 'ground')

        this.soldier = new Soldier({
            scene: this,
            image: "soldier",
            x: 200,
            y: this.game.config.height / 2
        });

        this.bullets = this.add.group({
            classType: Bullet,
            maxSize: 10,
            runChildUpdate: false
        });

        this.physics.add.collider(this.soldier, this.platforms);
    }

    update(time, delta) {
        this.soldier.update(this.cursors);

        if (this.cursors.down.isDown) {
            var bullet = this.bullets.get();

            if (bullet){
                bullet.fire(this.soldier.x, this.soldier.y);
            }
        }
    }
}

 

Link to comment
Share on other sites

Thanks for the replay, but Bullet Class constructor takes config as its argument, how do I pass that? I am currently getting the following error:

Uncaught TypeError: Cannot read property 'queueDepthSort' of undefined
    at Bullet.GameObject [as constructor] (phaser.js:3413)
    at new Sprite (phaser.js:11310)
    at new Bullet (Bullet.js:3)
    at Group.create (phaser.js:16791)
    at Group.getHandler (phaser.js:17410)
    at Group.getFirst (phaser.js:17235)
    at Group.get (phaser.js:17439)
    at GameScene.update (GameScene.js:49)
    at Systems.step (phaser.js:40744)
    at SceneManager.update (phaser.js:81683)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...