c# - How to animate rectangle frames? -


so have image 2 frames , want put rectangle.this image animate it's frames , loop them - 1st frame, 2nd frame.

here's code:

//create image texture2d image; //load, draw , update image in rectangle //load contnet  protected override void loadcontent() {      // todo: use this.content load game content here      image = content.load<texture2d>("image"); } protected override void update(gametime gametime) {      //don't know write here, should make frames loop } protected override void draw(gametime gametime) {      //begin drawing image      spritebatch.begin();      spritebatch.draw(image, new rectangle(244, 536, 32, 32), color.white)      spritebatch.end();     } 

can tell me how fill in update method?

added these variables above public game1():

    //sprite texture     texture2d texture;     //a timer variable     float timer = 0f;     //the interval (300 milliseconds)     float interval = 300f;     //current frame holder (start @ 1)     int currentframe = 1;     //width of single sprite image, not whole sprite sheet     int spritewidth = 32;     //height of single sprite image, not whole sprite sheet     int spriteheight = 32;     //a rectangle store 'frame' being shown     rectangle sourcerect;     //the centre of current 'frame'     vector2 origin; 

next, in loadcontent() method:

        //texture load         texture = content.load<texture2d>("gamegraphics\\enemies\\sprite"); 

the update() method:

        //increase timer number of milliseconds since update last called         timer += (float)gametime.elapsedgametime.totalmilliseconds;         //check timer more chosen interval         if (timer > interval)         {             //show next frame             currentframe++;             //reset timer             timer = 0f;         }         //if on last frame, reset 1 before first frame (because currentframe++ called next next frame 1!)         currentframe = currentframe % 2;         sourcerect = new rectangle(currentframe * spritewidth, 0, spritewidth, spriteheight);         origin = new vector2(sourcerect.width / 2, sourcerect.height / 2); 

and finally, draw() method:

        //texture draw         spritebatch.draw(texture, new vector2(263, 554), sourcerect,color.white, 0f, origin, 1.0f, spriteeffects.none, 0); 

note: that's solution works me... if finds answer useful, should know spritesheet must have it's frames aligned left right or else animation won't work.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -