Jump to content

Ajust sprite with the ground


giturra
 Share

Recommended Posts

Hi everyone,

I'm trying to figure out how to 'reset/readjust' a sprites collider with the ground, because before set the collider bounds, I got this (first image) using the follow code:

 

var game  = new Phaser.Game(1024, 800, Phaser.AUTO, 'BreakOut Phaser',{preload: preload, create: create, update: update});
        var bats;

        var leftKey;
        var rightKey;

        function preload() {
            this.load.image('background', '{% static 'assets/Background/background.png' %}');
            this.load.image('ball', '{% static 'assets/Balls/ball_red_resize.png' %}');
            this.load.image('bats', '{% static 'assets/Bats/bat_black_256x256.png' %}');

        }
        
        function create() {

            this.physics.startSystem(Phaser.Physics.ARCADE);

            this.add.sprite(0, 0, 'background');
            this.add.sprite(480, 336, 'ball');
            bats = this.add.sprite(384, 660, 'bats');

            this.physics.enable(bats, Phaser.Physics.ARCADE);

            //bats.body.bounce.y = 0.2;
            //bats.body.gravity.y = 300;
            //bats.body.collideWorldBounds = true;


            leftKey = this.input.keyboard.addKey(Phaser.Keyboard.LEFT);
            rightKey = this.input.keyboard.addKey(Phaser.Keyboard.RIGHT);

            this.input.keyboard.addKeyCapture([Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT]);



        }



        
        function update() {
            bats.body.velocity.x = 0;

            if (leftKey.isDown) {
                bats.body.velocity.x -= 300;
            }

            if (rightKey.isDown) {
                bats.body.velocity.x += 300;
            }


        }

 

and in the seconds image I remove the Javascript comment, I got the seconds image, the bats is not in the position that was define, how can I fix it?:

bats.body.bounce.y = 0.2;
bats.body.gravity.y = 300;
bats.body.collideWorldBounds = true;

 

Screenshot from 2018-03-07 21-44-57.png

Screenshot from 2018-03-07 21-45-09.png

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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