Sign in to follow this  
Renanfgs

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
    var hitWalls = game.physics.arcade.collide(player, walls);
    game.physics.arcade.collide(stars, 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.