Jump to content
  • Advertisement
Sign in to follow this  
MrChrisnis

Sprite won't automove properly

This topic is 2395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to get a sprite to move around a path that is just a part of the background image automatically. I am using C++ and SDL, however when the sprite moves around 2 parts of the path, it hits a corner and then it will not automove from right to left. I have tried a few different ways of doing it (there is a part coded out in the code below where I tried it with the if statement outside of the other one, still same problem)

33mmwpt.png

The image shows where the sprite gets stuck.

if(E[0]->get_x() <= 205)
{
E[0]->set_velocities(1, 0);
E[0]->auto_move(1);
E[0]->move();
}
/* else if(E[0]->get_y() >= 380)
{
E[0]->set_velocities(-1, 0);
E[0]->auto_move(1);
E[0]->move();
}*/
else if(E[0]->get_x() > 204)
{
if(E[0]->get_y() < 380)
{
E[0]->set_velocities(0, 1);
E[0]->auto_move(1);
E[0]->move();
}
else if(E[0]->get_y() == 380)
{
E[0]->set_velocities(-1, 0);
E[0]->auto_move(-1);
E[0]->move();
}
}


Probably something really simple that I am just missing but I cannot for the life of me find it :(

Share this post


Link to post
Share on other sites
Advertisement
oh my god, i think it would be better that in the condition of 'if' statement the [color="#000000"]get_x[color="#666600"]() shoule be used with [color="#000000"]get_y[color="#666600"]() , or else it is easily mess up

Share this post


Link to post
Share on other sites
This is pretty simple to solve, and will help make your code cleaner. Lets say for example your sprite has 4 possible movement changes.

1. Top Left (X, Y) or Point A (X, Y)
2. Top Right (X, Y) or Point B (X, Y)
3. Bottom Right (X, Y) or Point C (X, Y)
4. Bottom Left (X, Y) or Point D (X, Y)

Start the sprite at Point A.

When your sprite hits Point B (250, 30) -> Moving Down.
When your sprite hits Point C (250, 130) -> Moving Left.
When your sprite hits Point D (0, 130) -> Moving Up
When your sprite hits Point A (0, 30) -> Moving Right

Rinse and repeat!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!