# When hit the walls turn direction

## Recommended Posts

Hi guys,

I'm a new student and I knew Phaser since 5 days and it's fantastic. So I'm a beginner.

I'm trying to create one simple platform game, I have one issue but I don't know how to solve it. I'll appreciate your help.

I need that when the animation hit the wall he changes the direction, right or left and keep running.

Here's the part of the code that I think is necessary for analyses:

``````   //  Collide the player with the walls

//My animation starts running to right and I want it
player.body.velocity.x = 200;
player.animations.play('right',15,true);

if (hitWalls)  ????
{
//Here I tried: player.body.velocity.x = -150; but the animation is crazy now
}``````

Thanks and have a good day!

##### Share on other sites

I know the topic is about 2 weeks old but maybe still relevant (and sorry for digging up that other post ^^).

If you only move on the x-axis, you can simply invert the velocity for by using this:

player.body.velocity.x = player.body.velocity.x * -1;

To flip the sprite animation when moving in the other direction, you can either have an individual animation frame for this or invert the sprite by setting the scale for its x-axis to -1.

// Set Anchor to the center of your sprite

yourSprite.anchor.setTo(.5,.5);

// Invert scale.x to flip left/right

yourSprite.scale.x *= -1;

This however also inverts the width and might cause other problems (you might want to use absolute values for width and height then).

See this post for examples and more insight into sprite flipping:

##### Share on other sites

I need your help. I'm a student of The University of Illinois at Chicago. I got the task to create a presentation "Interrelation of Higher Mathematics and Informatics".
I need to show the students that the modern stage of science is characterized by the interconnection of mathematics, physics, informatics and other branches of knowledge. With the search for the text for my report, there were no problems, I turned to the specialists of the Essayshark.com company. But I have a problem with in finding the right images for my presentation. Do you know some good resources? Thank you!

##### Share on other sites
On 15/06/2017 at 11:40 AM, TammySmith said:

I need your help. I'm a student of The University of Illinois at Chicago. I got the task to create a presentation "Interrelation of Higher Mathematics and Informatics".
I need to show the students that the modern stage of science is characterized by the interconnection of mathematics, physics, informatics and other branches of knowledge. With the search for the text for my report, there were no problems, I turned to the specialists of the Essayshark.com company. But I have a problem with in finding the right images for my presentation. Do you know some good resources? Thank you!

is this some really weird bot?

who is a bot?

##### Share on other sites
On 29/06/2017 at 10:21 AM, TammySmith said:

who is a bot?

you?

no!

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.