Jump to content

Group objects rotation and origin setting


OlGF
 Share

Recommended Posts

Hello, 

I've just started learning Phaser 3. Playing with simple code and it seems my approach is not good.
Here is example what I'm trying to implement (moving and firing tank):

screencast-2018-11-08-022531.gif.e064a0d4aac61a3ebf7998612dee5dfd.gif

I used two separate sprites to animate tank and its shooting. Then I decided to complicate tank behavior during the shoot, so now it imitates recoil by rotating both of sprites (tank and shoot).
I found no way to rotate group of sprites at once, so I'm rotating it one by one. Also I had to use dirty workaround with changing sprite origins to have same angle at rotation.

I guess the better way to do the same would be a single sprite which includes both moving and firing frames. But probably code below may be improved. Any advice is highly appreciated:

    var config = {
        type: Phaser.AUTO,
        width: 1000,
        height: 600,
        physics: {
            default: 'arcade'
        },
        backgroundColor: "#007236",
        scene: {
            preload: preload,
            create: create,
            update: update
        }
    };

    var game = new Phaser.Game(config);
    var tankGroup, tank, cursors, fire;
    var shooting=0;

    function preload ()
    {

        
        this.load.spritesheet('tank', 'assets/tank.png',{ frameWidth: 200, frameHeight: 200 });
        this.load.spritesheet('shoot', 'assets/shootfire.png',{ startFrame: 8, frameWidth: 16, frameHeight: 16 });
    }

    function create ()
    {

        tankGroup = this.physics.add.group();
        tank = tankGroup.create(900, 100, 'tank');
        fire = tankGroup.create(908, 98, 'shoot',7);
        fire.setOrigin(6,1);
        tank.setOrigin(0.5,0.5);
        fireSound=this.sound.add('fire');

        
        tank.flipX=true;



        this.anims.create({
            key: 'shoot',
            frames: this.anims.generateFrameNumbers('shoot',{start: 0, end: 7}),
            framerate: 7,
            repeat: 0
        });

        this.anims.create({
            key: 'moving',
            frames: this.anims.generateFrameNumbers('tank',{start: 0, end: 2}),
            framerate: 1,
            repeat: -1
        });

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

    }

    function update() 
    {
        if (cursors.left.isDown){
            tankGroup.setVelocityX(-30);
            tank.anims.play('moving', true);
        } 
        else if (cursors.right.isDown){
            tankGroup.setVelocityX(30);
            tank.anims.playReverse('moving', true);
        } else {
            tankGroup.setVelocityX(0);
            tank.anims.stop();
        }
        
        if ( Phaser.Input.Keyboard.JustDown(cursors.space) ){
            shooting=1;

            fire.anims.setTimeScale(0.5);
            fire.anims.play('shoot');
        }
        
        tankRecoil();

    }

    function tankRecoil(){
        if (tank.angle<=10 && shooting==1){
            tank.angle+=1.8;
            fire.angle+=1.8;
        } 
        
        if (tank.angle>10)
            shooting=2;
        
        if (tank.angle>0 && shooting==2){
            tank.angle-=0.8;
            fire.angle-=0.8;
        } 
        
        if (tank.angle<=0 && shooting==2){
            shooting==0;
        } 
        
    }

 

 

 

 

 

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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