timy created page: Movement

Tim Young 2017-10-03 01:59:22 +02:00
parent 610935a386
commit 221f8b6715

41
Movement.md Normal file

@ -0,0 +1,41 @@
# Movement
There are a number of ways to move a sprite, but some work a lot better than others. Because the sprite-controller (and C# in general) handles time in an inconsistent manner, it looks a lot better to use the sprite “SetDirection” and “MoveTo” functions. To get a sprite to move, however, you not only need to use one of those, but you also need to define the speed, and set “AutomaticallyMoves = true.” Here is an example of defining a sprite and starting it moving.
```c#
NewSprite.AutomaticallyMoves = true;
NewSprite.SetSpriteDirectionDegrees(180); //Direction in degrees. 0 = right, 180 = left
NewSprite.MovementSpeed = speed;
```
One issue with the sprite-controller is that it resets things every time you tell it to do the same thing again. So, it is better to check to see if the sprite is already moving the direction you want it to be moving, or check to see if it is already doing the animation you want, before telling it to move that direction or do that animation. Telling it to do it over and over usually results in it just sitting there.
```c#
If(NewSprite.AutomaticallyMoves == false)
{
NewSprite.AutomaticallyMoves = true;
NewSprite.SetSpriteDirectionDegrees(180); //Direction in degrees. 0 = right, 180 = left
NewSprite.MovementSpeed = speed;
}
```
## Direction
You can get and set the sprite direction. This direction is not precise. The sprite actually stores the direction it is moving in a “Vector”. So if you give it a direction, it computes a vector and moves in that general direction. If you set a direction of 45 and then ask the sprite what direction it is moving, it might return a direction of 45.230. If you do a comparison between that and 45, C# will say that it is not going at a 45-degree angle. In short, when comparing directions, allow a little bit of slop.
```c#
double degrees = NewSprite.GetSpriteDegrees();
if(degrees > 43 && degrees < 47)
{
//assume we are going about 45 degrees.
}
```
## Speed
Speed is how many pixels the sprite travels in a specific amount of time. This means that the relative speed changes with the size of your background. If your background image is 800x600, then a speed of 10 will look faster than if your background is 1024x768. You will need to figure out what speed is appropriate for your game, or whatever you are making.
## MoveTo
There are a number of MoveTo functions. You can move to one point, move to a list of points, or move to a sprite. They all work well in conjunction with the SpriteArrivedAtEndPoint event.
When you moveto one point, the sprite will move to that position and then stop moving. When it reaches the destination, it will fire off the SpriteArrivedAtEndPoint event, as well as set the SpriteReachedEndPoint variable to true.
When you MoveTo multiple points, the sprite will move from one point to the next, to the next. When it reaches each waypoint (each point along the way except for the ending point), the SpriteArrivedAtWaypoint event is triggered. When the Sprite reaches the end, the SpriteReachedEndPoint event is triggered, and the SpriteReachedEndPoint variable is set to true.
When you MoveTo a sprite, the target adjusts every time that the sprite moves. It tries to get the center of the moving sprite to hit the center of the target sprite. This is not “optimal.” Instead of moving to where the sprite will be, it moves to where it is now. So the sprite that is doing the MoveTo should be moving faster than the destination sprite if you want to eventually hit it. There are no “waypoints” involved, but the SpriteReachedEndPoint function is called when the sprite finally hits the target sprite. And, the SpriteReachedEndPoint is set to true.
If at any time you do another move-to, the old move-to information is replaced with the new. You cannot simply add another waypoint. You replace the entire move-to destination with a different one. And, in doing that, none of the SpriteReached.. events get triggered.
SpriteReachedEndPoint: This variable is somewhat useful, but has caused a fair bit of confusion. This value is basically “false” during the operation of a move-to. If you never tell a sprite to move, then it is always “true.” As soon as you tell it to move, the value remains false until it has stopped moving. This value starts “true”, but, if you are telling it to move somewhere, it is “false” until it gets to that spot (or the movement is canceled)