Jump to content

  • Log In with Google      Sign In   
  • Create Account


Sprite won't automove properly


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 MrChrisnis   Members   -  Reputation: 340

Like
0Likes
Like

Posted 28 March 2012 - 03:46 PM

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)

Posted Image

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 :(

Sponsor:

#2 www.gamegugu.com   Members   -  Reputation: 73

Like
0Likes
Like

Posted 28 March 2012 - 08:53 PM

oh my god, i think it would be better that in the condition of 'if' statement the get_x() shoule be used with get_y() , or else it is easily mess up

#3 Black-Rook   Members   -  Reputation: 1563

Like
0Likes
Like

Posted 29 March 2012 - 12:31 AM

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!
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

#4 MrChrisnis   Members   -  Reputation: 340

Like
0Likes
Like

Posted 29 March 2012 - 11:30 AM

Ahhh, that makes sense lol. Thanks :)

As I said, easy but I couldn't spot it :P




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS