Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

bullet mechanism in phaser

function create ()
{
    var Bullet = new Phaser.Class({

        Extends: Phaser.GameObjects.Image,

        initialize:

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

            this.speed = Phaser.Math.GetSpeed(400, 1);
        },

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

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

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

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

    });

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

    ship = this.add.sprite(400, 500, 'ship').setDepth(1);

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

    speed = Phaser.Math.GetSpeed(300, 1);
}

function update (time, delta)
{
    if (cursors.left.isDown)
    {
        ship.x -= speed * delta;
    }
    else if (cursors.right.isDown)
    {
        ship.x += speed * delta;
    }

    if (cursors.up.isDown && time > lastFired)
    {
        var bullet = bullets.get();

        if (bullet)
        {
            bullet.fire(ship.x, ship.y);

            lastFired = time + 50;
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: phaser matrix rotate 
Javascript :: socket io inside route express not working 
Javascript :: phaser wrap sprite 
Javascript :: controllare che ci sia un file in javascript 
Javascript :: jquery show loader 
Javascript :: Creating getLastBlock Object for blockchain 
Javascript :: javascript cookies all together 
Javascript :: open close menu javascript 
Javascript :: A Node Module For ReactJS 
Javascript :: change button text dynamically angular 6 
Javascript :: empty or remove div span class 
Javascript :: react_devtools_backend.js:4026 Warning: Cannot update a component (`BrowserRouter`) while rendering a different component (`Login`). 
Javascript :: how to get creator of inetarceton discordjs 
Javascript :: JavaScript Using es6(ES2015) Destructuring assignment 
Javascript :: A Method In Class That Accesses A Property 
Javascript :: path.join javascript one folder above 
Javascript :: ONDC node 
Javascript :: problem with Next.js and @material-ui. 
Javascript :: mongodb instructions 
Javascript :: how to get on hnage input before clicking off 
Javascript :: How do I target and change the style of a different element when I click a button in react 
Javascript :: create number format excel react native 
Javascript :: set to array js 
Javascript :: electron npm start not working 
Javascript :: table to excel javascript 
Javascript :: dictionnary js 
Javascript :: Remove uploaded file in jquery 
Javascript :: noise expression after effects 
Javascript :: rem api rest 
Javascript :: Convert to String Explicitly 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =