Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

bullet class phaser 3 fix


jasper1990
 Share

Recommended Posts

I'm developing a rail shooter and I'm trying to copy the bullet class from phaser 3 example here : https://labs.phaser.io/view.html?src=src\games\defenda\test.js&v=3.2.1.

I'm having issues with no bullets appearing *when I press spacebar*, there appears to be no errors in the console either. I'm not entirely sure how groups work, but could someone explain to me? I'm using phaser 3.2.1

railshooter.zip

Link to comment
Share on other sites

I added a zip file, but specifically I get an issue with the following :

    function create (){

        var Bullet = Phaser.Class({
            Extends: Phaser.GameObjects.Image,

            initialize:

            function Bullet(scene)
            {
                Phaser.GameObjects.Image.call(this, scene, 0, 0, 'bullet');

                this.speed = 0;
                this.born  = 0;
            },

            fire: function (argplayer)
            {
                this.setPosition(argplayer.x, argplayer.y);

                this.speed = 20;

                this.born = 0;
            },

            update: function (time, delta)
            {
                this.y += this.speed * delta;

                this.born += delta;

                if (this.born > 1000)
                {
                    this.setActive(false);
                    this.setVisible(false);
                }
            }
        });

        player = new Player(this, 100, 400);

        this.anims.create({
        key: 'middle',
        frames: this.anims.generateFrameNumbers(PLAYER_LABEL, { start: 0, end: 2 }),
        frameRate: 10,
        repeat: -1
        });

        this.anims.create({
        key: 'left',
        frames: this.anims.generateFrameNumbers(PLAYER_LABEL, { start: 3, end: 5 }),
        frameRate: 10,
        repeat: -1
        });

        this.anims.create({
        key: 'right',
        frames: this.anims.generateFrameNumbers(PLAYER_LABEL, { start: 6, end: 8 }),
        frameRate: 10,
        repeat: -1
        });

        this.bullets = this.add.group({ classType: Bullet, runChildUpdate: true });
        this.lastFired = 0;

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

        KeyZ = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z);
        KeyX = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.X);
        KeyC = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.C);
    }

    function update (time, delta){
        if (cursor.right.isDown)
        {
            player.moveRight();
            if(player.checkBoundary(256,512, 0))
            {
                player.moveLeft();
            }
        }
        else if(cursor.left.isDown)
        {
            player.moveLeft();
            if(player.checkBoundary(256,512, 0))
            {
                player.moveRight();
            }
        }
        else if(cursor.up.isDown)
        {
            player.moveUp();
            if(player.checkBoundary(256,512, 0))
            {
                player.moveDown();
            }
        }
        else if (cursor.down.isDown)
        {
            player.moveDown();
            if(player.checkBoundary(256,512, 0))
            {
                player.moveUp();
            }
        }

        if(KeyZ.isDown)
        {
            player.aimLeft();
        }

        if(KeyX.isDown)
        {
            player.aimMiddle();
        }

        if(KeyC.isDown)
        {
            player.aimRight();
        }

        if (cursor.space.isDown && time > this.lastFired)
        {
            var bullet = this.bullets.get();
            bullet.setActive(true);
            bullet.setVisible(true);

            if (bullet)
            {
                bullet.fire(player);

                this.lastFired = time + 100;
            }
        }

        player.renderAim();
    }

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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