Top Rope Suplex

Learning Life & Programming

Aaron’s Head is now Animated

I’m learning that programming as a novice is a whole lot of trial and error, with an akward learning curve.

Aaron's Head Open to the RightToday I managed to get my moving sprite animated; if it’s moving right it looks right. If it’s moving left, it looks left. I also got it open and close the mouth from a space bar press! The path to getting there however, was a long one…

I started out trying to switch the filename associated with a Texture2D object. This, simply put, did not work. My theory was to simply change out the file name based on keyboard presses, hoping this would inpact the Draw class. It didn’t.

So… I began a treck of many trials and errors, and many Google searches. I found a tutorial called “How to Animate a Sprite” in the XNA help documents. Excited, I tried and tried for about 2 hours to understand what was happening in the AnimateSprite class they created, so I could pull out and modify the neccessary code. What I couldn’t wrap my head around was the class was asking for some frames arguments, and then dividing the width by the number of frames.

I tried to find any references to frames in the C# documents (in Java they’re called the JavcaDoc; what are they in C#?). But couldn’t figure it out… so I went on in faith. I decided they must have meant use an animated GIF, so I did. And worked, and thought, and finally, started from scratch.

At which point I finally read the opening sentances of the XNA Tutorial – they expect you to be using one image that is a strip of frames, all of equal size. Now it all made sense… but I didn’t want to do it that way.

So, I ended up loading 4 images (i.e. myTextureF1 = Texture2D.FromFile(graphics.GraphicsDevice, “aaronRightOpen”)) and the setting up an int Frame. I then used the keyboard states to set what number should be assigned to the Frame. Finally, in the Draw class, I determined what sprite should be drawn by using if/else statements based on the frame number.

THe thing I’m learning about programming is that sometimes things work. I know, looking at my code, that it’s not clean and not the best way of doing it. But now that I know one way to do it, I can try to find some design patterns that make better sense and more effective use of code.

Never-the-less, I feel accomplished. I had a goal and I’ve gotten there, step by step, and I know how I did it.

September 4, 2006 - Posted by Aaron | C#, Programming & Gaming, XNA, XNA Tutorials | | 3 Comments

3 Comments »

  1. I’m proud of you! Although I’ve seen code on -how- to do this sort of thing, I can’t -understand- it as I would like to. There’s no way a person can be happy with what they’ve written unless they have spent hours (or if you can, not so long!) through trial and error, and getting something that -you- made and learn to do. Only then will things make sense.

    Comment by Adam | February 10, 2007 | Reply

  2. Hi, i’m having a problem coding a moving animated sprite, i have managed to seperatly code a static animated sprite, and a non animated sprite controlled by the keyboard…i just can’t seem to figure out how to combine the two.

    Reading this post of yours it seems you have managed to succeed were i have failed ha, could you please e-mail me if you have any ideas on how to help, or post your example code so i can try and get my head around it, cheers.

    Comment by Ricky | June 30, 2008 | Reply

  3. Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

    Comment by sandrar | September 10, 2009 | Reply


Leave a comment