It's taken me a lot longer than I had hoped to make something as simple as pong. Learning how to use Rectangles for the collision/Vector 2s to move the balls correctly was a bigger pain than it should've been.
At this point, I am trying to learn the most basic way to animate sprites/scroll backgrounds.
Maybe thats a bit of a jump from what I should be doing, idk. But I have seen a million tutorials on how to animate sprites, using Vector2s, Points, and even just regular ints + a SourceRect.
What is the most BASIC way to animate a sprite? Using just ints + a SourceRect seems pretty easy, but when it comes to thinking up the variables to use, I tend to not know what variables I need:
an int for the current frame
an int for the height/width of the image
ints for the X/Y position of the sprite
a float for the frame rate
a float for the elapsed time
those are the only variables I could probably come up with on my own.
The non obvious variables are the ones I wouldn't think of using:
ints for the X and Y offsets, in case we're using a sprite sheet.
But yeah, if someone could point me in the direction of the easiest way to animate a sprite, I would appreciate it. Having a million ways to do something is great, but can also be a headache for someone who is just learning.
On another note, I have a bit of a stupid question, what makes EFFICIENT code?
Learning to code is one thing, learning to write efficient code is another.
I finally managed to make pong with C#/XNA, moving onto more advanced stuff.
I understand your frustration with the million different ways people do things. I'm just getting into my first platformer, and it's a pain to try and find stuff out when nobody really does the same thing.
As to your questions:
Honestly, when I'm setting up my animation handler (one of the first things I do when starting a game) I standardize the frame rate and use that as a basis for my animations. I work in Java, so I don't know how it works in C#/XNA but basically you set a maximum frame time, and then subtract from that the amount of time it takes to update, buffer and render the game. The sleep time of the thread is then based off of how much time is left. This negates the need to keep a frame rate and time elapsed variable.
If you want to create an animation for all the possible frames that each object could have, rather than a separate one for each animation (ie running, jumping, walking etc), you would need a start frame and end frame variable that you would change based on what the object is doing.
As for efficient code, there are a lot of ways to define it, but most seem to revolve around minimizing the number of calculations the computer has to do. For example, if you have an array of enemies in your game, you only need the player to be checked for collisions against the ones that are actually on screen/active. By checking which enemies are 'active' you negate the need to check collisions, potentially update them at all, or draw them, which minimizes the number of things the computer is trying to do.
Hope this helps,
Peter
As to your questions:
Honestly, when I'm setting up my animation handler (one of the first things I do when starting a game) I standardize the frame rate and use that as a basis for my animations. I work in Java, so I don't know how it works in C#/XNA but basically you set a maximum frame time, and then subtract from that the amount of time it takes to update, buffer and render the game. The sleep time of the thread is then based off of how much time is left. This negates the need to keep a frame rate and time elapsed variable.
If you want to create an animation for all the possible frames that each object could have, rather than a separate one for each animation (ie running, jumping, walking etc), you would need a start frame and end frame variable that you would change based on what the object is doing.
As for efficient code, there are a lot of ways to define it, but most seem to revolve around minimizing the number of calculations the computer has to do. For example, if you have an array of enemies in your game, you only need the player to be checked for collisions against the ones that are actually on screen/active. By checking which enemies are 'active' you negate the need to check collisions, potentially update them at all, or draw them, which minimizes the number of things the computer is trying to do.
Hope this helps,
Peter
For what it's worth, I'll throw in my own 2 cents that I've gathered thus far.
As far as animation goes, I tend to focus my frame timing around whatever seems to be driving it. For example, I would maybe time a walking animation based on how far my character has walked. It's probably an idiotic way to do it, but it makes sense to me and I haven't had the need for a different method yet.
As far as thinking up of variables, you might want to just save that for a time that you actually need them, instead of trying to predict what you need before starting, at least member variables that you use within a class.
I couldn't say much about code efficiency as I didn't need to concern myself with it yet. Currently I'm working on a 2D sidescroller, and I'm pretty sure the code would give a severe heart attack to any efficiency nut ;] I tend to write really disorganized code, so I tend to put more focus on how to structure things such that I can keep track of them over efficiency.
As far as animation goes, I tend to focus my frame timing around whatever seems to be driving it. For example, I would maybe time a walking animation based on how far my character has walked. It's probably an idiotic way to do it, but it makes sense to me and I haven't had the need for a different method yet.
As far as thinking up of variables, you might want to just save that for a time that you actually need them, instead of trying to predict what you need before starting, at least member variables that you use within a class.
I couldn't say much about code efficiency as I didn't need to concern myself with it yet. Currently I'm working on a 2D sidescroller, and I'm pretty sure the code would give a severe heart attack to any efficiency nut ;] I tend to write really disorganized code, so I tend to put more focus on how to structure things such that I can keep track of them over efficiency.
I didn't debug this.. trying to type from memory.. but roughly you'll need a Sprite class with the following:
Hope this helps..
Vector2 location = Vector2.Zero;
double frameTime = 0;
double maxframeTime = 33; // ms
List<Rectangle> sourceRects = new List<Rectangle>();
int currentFrame = 0;
int frameWidth, frameHeight;
Texture2D spriteSheet; //<-- load this elsewhere, but it should contain all your animation frames
frameWidth = 50;
frameHeight = 60; // or whatever
// Add rectangles for each animation frame
sourceRects.Add(new Rectangle(0, 0, frameWidth, frameHeight)); // Insert your frame1 rectangle coords
sourceRects.Add(new Rectangle(frameWidth*1, 0, frameWidth, frameHeight)); // Insert your frame2 rectangle coords
// ... etc
Then in your update method you would need something like this
frameTime += gameTime.ElapsedGameTime.TotalMilliseconds;
if (frameTime >= maxframeTime)
{
currentFrame = (currentFrame + 1) % (sourceRects.Count);
frameTime = 0.0f;
}
// Lastly in your draw code you'd need a spriteBatch reference and this
spriteBatch.Draw(
spriteSheet,
location,
sourceRects[currentFrame],
Color.White,
0,
new Vector2(frameWidth / 2, frameHeight / 2),
1.0f,
SpriteEffects.None,
0.0f);
Hope this helps..
I'm currently reading the book "Learning XNA 4.0" and I think its great, definately for beginners. I'm only 6 chapters though.
I have covered topics such as so far...
- sprite sheet animations (easily change animation speed of certain sprites without altering the main game fps, this is introduced really early in the book)
- user input and collision detection
- learning object-oriented design concepts (including the use of GameComponents in XNA)
- using sound with XACT (its a program that comes with XNA and i love it)
Its really useful for beginners.
I'm about to read about basic artifical intelligence next chapter. But yes if you're a good reader this really speeds up learning.
It may not benefit people who are already aware of most game development coding stratergies/concepts (ive only read 100/400+ pages so dont take my word on this! lol)
I have covered topics such as so far...
- sprite sheet animations (easily change animation speed of certain sprites without altering the main game fps, this is introduced really early in the book)
- user input and collision detection
- learning object-oriented design concepts (including the use of GameComponents in XNA)
- using sound with XACT (its a program that comes with XNA and i love it)
Its really useful for beginners.
I'm about to read about basic artifical intelligence next chapter. But yes if you're a good reader this really speeds up learning.
It may not benefit people who are already aware of most game development coding stratergies/concepts (ive only read 100/400+ pages so dont take my word on this! lol)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement