Jump to content

How to: 'flip sprite image horizontally' and reverse direction?


vmars316
 Share

Recommended Posts

Hello & Thanks , 

Well , I am making headway on my first Phaser game :

 http://liesandcowpies.com/testMe/vm-phaser/vm-vertical-collision-Web.html 

Is there a better way to to check if sprite is out of bounds , besides 

comparing sprite x position with game.world.setBounds ?

Also , when sprite is out of bounds , how can I cause it to :

do a 'flip sprite image horizontally' and reverse direction , from left-to-right to right-to-left ?

  Any examples do this ?

Thanks

 

 

 

Link to comment
Share on other sites

Have you tried something like this for checking for collision with the world bounds?

In the init():

I think you need to set the world bounds. And I believe you need to use arcade physics for this.

this.game.world.setBounds(0,0,360,700);

 

In create():

this.player.body.collideWorldBounds = true;

 

 

As far as flipping an image horizontally, 

Moving left:

this.player.body.velocity.x = -this.SPEED; <-- Note the negative in front of the speed.
this.player.scale.setTo(1, 1);
this.player.play('moving');
 

Moving right:

this.player.body.velocity.x = this.SPEED;
this.player.scale.setTo(-1, 1); <-- Note the negative in front of the first parameter.
this.player.play('moving');

Hope this helps,

-Chad   

Link to comment
Share on other sites

21 hours ago, cdelstad said:

Have you tried something like this for checking for collision with the world bounds?

In the init():

I think you need to set the world bounds. And I believe you need to use arcade physics for this.

this.game.world.setBounds(0,0,360,700);

 

In create():

this.player.body.collideWorldBounds = true;

 

 

As far as flipping an image horizontally, 

Moving left:

this.player.body.velocity.x = -this.SPEED; <-- Note the negative in front of the speed.
this.player.scale.setTo(1, 1);
this.player.play('moving');
 

Moving right:

this.player.body.velocity.x = this.SPEED;
this.player.scale.setTo(-1, 1); <-- Note the negative in front of the first parameter.
this.player.play('moving');

Hope this helps,

-Chad   

I am a bit confused , what does the code look like to get here:

? function update()  ?
this.player.body.velocity.x = -this.SPEED; <-- Note the negative in front of the speed.
this.player.scale.setTo(1, 1);
this.player.play('moving');  

Also , is it necessary to put this.player. in front of 
body.velocity.x 
could I just put truth02Sprite.body.velocity.x ?

Thanks 
 

Link to comment
Share on other sites

On 1/26/2016 at 10:22 PM, cdelstad said:

Have you tried something like this for checking for collision with the world bounds?

In the init():

I think you need to set the world bounds. And I believe you need to use arcade physics for this.

this.game.world.setBounds(0,0,360,700);

 

In create():

this.player.body.collideWorldBounds = true;

 

 

As far as flipping an image horizontally, 

Moving left:

this.player.body.velocity.x = -this.SPEED; <-- Note the negative in front of the speed.
this.player.scale.setTo(1, 1);
this.player.play('moving');
 

Moving right:

this.player.body.velocity.x = this.SPEED;
this.player.scale.setTo(-1, 1); <-- Note the negative in front of the first parameter.
this.player.play('moving');

Hope this helps,

-Chad   

I am a bit confused , what does the code look like to get here:

? function update()  ?
this.player.body.velocity.x = -this.SPEED; <-- Note the negative in front of the speed.
this.player.scale.setTo(1, 1);
this.player.play('moving');  

Also , is it necessary to put this.player. in front of 
body.velocity.x 
could I just put truth02Sprite.body.velocity.x ?

Thanks 
Ok , works great:

Quote

        if (truth02sprite.x > worldBoundsWidth) 
    {
        truth02sprite.scale.setTo(-1, 1); 
        truth02sprite.body.velocity.x = -100;
//        truth02sprite.body.x = 50;
        score ++;
        scoreText.text = 'Score : ' + score;
    }
        if (truth02sprite.x < 0) 
    {
        truth02sprite.scale.setTo(1, 1); 
        truth02sprite.body.velocity.x = +100;
//        truth02sprite.body.x = 50;
        score ++;
        scoreText.text = 'Score : ' + score;
    }

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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