问题
I'm working on XNA C# when I came upon this problem where I need to store easily editable animation data such that my game will be able to render and play it.
It's something like this:
I have a texture flying across the screen from (0,0) to (800,600) pixels. This will last 5s.
How do I represent it in data and also write it such that the game is able to interpret and do the necessary in the Draw
and Update
methods. It's OK if I need to do some extensive coding.
回答1:
A simple domain specific language could help; create an AnimationDirector class that would interpret the statements from the animation script and do the appropriate work during Draw
and Update
. The DSL itself could be as simple as
texture 0,0; 800,600; 5.0
which will create an object with texture texture
at (0,0) and move it to (800,600) over a 5 second period, and then destroy it.
If you want something a little more useful,
create x texture 0,0
over 5.0 move x 800,600
destroy x
which will let you get more creative, and expand the possibilities of animation (such as adding rotation, etc. if you desire such).
Now, when you need to run an animation, just pass the appropriate resource name to the director, and let it handle things from there. Alternatively, you could create some kind of AnimatedEntity which refers to a particular script, and calls the AnimationDirector itself for Draw
and Update
; depending on how you've done your engine so far, this might be more fitting to its design.
回答2:
Here's a quick hint...
The position of the texture can be defined by a mathematical formula, which would give you (0,0) at the start time, and (800,600) five seconds later...
来源:https://stackoverflow.com/questions/2212401/storing-vector-xna-animations