Jump to content

Phaser 3 - Tweens and Particles


Devika Mathur
 Share

Recommended Posts

//Using tweens and particles to simulate the smokey emissions of a rocket in Phaser 3.

var config = {
    width: 1280,
    height: 720,
    type: Phaser.AUTO,
    autoCenter: Phaser.Scale.CENTER_BOTH,
    physics: {
        default: 'arcade',
    },  

//We us the default arcade physics game engine. 
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var player;
var cursors;
var smoke;

var game = new Phaser.Game(config);

function preload ()
{
    this.load.image('bg', 'assets/space.jpg');
    this.load.image('player', 'assets/ship.png');
    this.load.image('smoke', 'assets/smoke.png');
}

function create ()
{
    bg = this.add.image(0, 0, 'bg');
    bg.setScale(1.35);
    cursors = this.input.keyboard.createCursorKeys();

    player = this.physics.add.image(400, 300, 'player');
    player.setScale(0.2);

    player.setCollideWorldBounds(true);

    smoke = this.add.particles('smoke');
    
    var emitter = smoke.createEmitter({
        speed: 100,
        scale: { start: 0, end: 1 },
        blendMode: 'ADD'
    });

    emitter.startFollow(player, 0, 80);
    emitter.setScale(0.1);
    emitter.setGravityY(20000);
    emitter.flow(1, 5);

   //The smoke effect is created using the emitter

    var tween = this.tweens.add({

            targets: emitter,
            loop:-1,
        
    });

    tween.play();

}

//The rocket can fly up,down,left and right using arrow keys.

function update ()
{
    player.setVelocity(0);

    if (cursors.left.isDown)
    {
        player.setVelocityX(-500);
    }
    else if (cursors.right.isDown)
    {
        player.setVelocityX(500);
    }

    if (cursors.up.isDown)
    {
        player.setVelocityY(-500);

    }
    else if (cursors.down.isDown)
    {
        player.setVelocityY(500);
    }
}

 

ship.png

smoke.png

space.jpg

image.png

Link to comment
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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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