animation - C# XNA - How to draw animated texture with 2 Rectangle variables? -
i have problem need resolve. making space invaders game in c# xna. i've been following tutorial invaders: https://www.youtube.com/watch?list=llhsf03qfdmdhbw02_8c6smg&v=wiw4s75izbi&feature=player_detailpage#t=397 have own code animating invader spritesheet. , here's i'm drawing:
void draw(spritebatch spritebatch){ (int r = 0; r < m_invaderrows; r++) { (int c = 0; c < m_invadercollumns; c++) { spritebatch.draw(m_botinvadertex, m_botinvaderpos, m_botinvaderhitbox, color.white, 0f, vector2.zero, 1.0f, spriteeffects.none, 0); // ??? spritebatch.draw(m_botinvadertex, m_botinvadersrect[r, c], color.white); } } }
so first draw()
call draws 1 animated sprite. second draws 50 non-animated sprites. m_botinvadersrect[r, c]
rectangle used multi-dimensional array. i'm looking way combine both draw
calls , have 50 animated sprites drawn on screen.
how can animate invaders using code bellow?
my code:
using system; using system.collections; using system.collections.generic; using system.linq; using microsoft.xna.framework; using microsoft.xna.framework.storage; using microsoft.xna.framework.audio; using microsoft.xna.framework.content; using microsoft.xna.framework.gamerservices; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; using microsoft.xna.framework.media; using microsoft.xna.framework.net; namespace spaceinvaders { class botinvader { public botinvader() { } public static texture2d g_botinvadertex; rectangle m_botinvaderhitbox; public static vector2 g_botinvaderpos = new vector2(0, 24); vector2 m_botinvaderorigin; int m_botinvadercurrentframe = 1; int m_botinvaderframewidth = 52; int m_botinvaderframeheight = 88; float m_timer = 0f; float m_interval = 100; rectangle[,] m_botinvadersrect; int m_invaderrows = 5; int m_invadercollumns = 10; string m_botinvadersdirection = "right"; public void initialize() { } public void loadcontent(contentmanager content) { g_botinvadertex = content.load<texture2d>(".\\gamegraphics\\gamesprites\\botinvaders\\normalinvaders\\spritesheet"); // invadership1 m_botinvadersrect = new rectangle[m_invaderrows, m_invadercollumns]; (int r = 0; r < m_invaderrows; r++) { (int c = 0; c < m_invadercollumns; c++) { m_botinvadersrect[r, c].width = g_botinvadertex.width; m_botinvadersrect[r, c].height = g_botinvadertex.height; m_botinvadersrect[r, c].x = 70 * c; m_botinvadersrect[r, c].y = 70 * r; } } } public void update(gametime gametime) { m_botinvaderhitbox = new rectangle(m_botinvadercurrentframe * m_botinvaderframewidth, 0, m_botinvaderframewidth, m_botinvaderframeheight); m_botinvaderorigin = new vector2(m_botinvaderhitbox.x / 2, m_botinvaderhitbox.y / 2); m_timer += (float)gametime.elapsedgametime.milliseconds; if (m_timer > m_interval) { m_botinvadercurrentframe++; m_timer = 0f; } if (m_botinvadercurrentframe == 2) { m_botinvadercurrentframe = 0; } m_botinvaderhitbox = new rectangle(m_botinvadercurrentframe * m_botinvaderframewidth, 0, m_botinvaderframewidth, m_botinvaderframeheight); m_botinvaderorigin = new vector2(m_botinvaderhitbox.width / 2, m_botinvaderhitbox.height / 2); int m_rightside = 800; int m_leftside = 0; (int r = 0; r < m_invaderrows; r++) { (int c = 0; c < m_invadercollumns; c++) { if (m_botinvadersdirection.equals ("right")) { m_botinvadersrect[r, c].x = m_botinvadersrect[r, c].x + 1; } if (m_botinvadersdirection.equals("left")) { m_botinvadersrect[r, c].x = m_botinvadersrect[r, c].x - 1; } } } string m_botinvaderschangedirection = "n"; (int r = 0; r < m_invaderrows; r++) { (int c = 0; c < m_invadercollumns; c++) { if (m_botinvadersrect[r, c].x + m_botinvadersrect[r, c].width > m_rightside) { m_botinvadersdirection = "left"; m_botinvaderschangedirection = "y"; } if (m_botinvadersrect[r, c].x < m_leftside) { m_botinvadersdirection = "right"; m_botinvaderschangedirection = "y"; } } if (m_botinvaderschangedirection.equals("y")) { (int c = 0; c < m_invadercollumns; c++) { m_botinvadersrect[r, c].y = m_botinvadersrect[r, c].y + 3; } } } } public void draw(spritebatch spritebatch) { (int r = 0; r < m_invaderrows; r++) { (int c = 0; c < m_invadercollumns; c++) { spritebatch.draw(g_botinvadertex, g_botinvaderpos, m_botinvaderhitbox, color.white, 0f, vector2.zero, 1.0f, spriteeffects.none, 0); spritebatch.draw(g_botinvadertex, m_botinvadersrect[r, c], color.white); } } } } }
you have create call creating sprite in tutorial --> enter link description here. in class yoo have put animation
m_botinvaderhitbox = new rectangle(m_botinvadercurrentframe * m_botinvaderframewidth, 0, m_botinvaderframewidth, m_botinvaderframeheight); m_botinvaderorigin = new vector2(m_botinvaderhitbox.x / 2, m_botinvaderhitbox.y / 2); m_timer += (float)gametime.elapsedgametime.milliseconds; if (m_timer > m_interval) { m_botinvadercurrentframe++; m_timer = 0f; } if (m_botinvadercurrentframe == 2) { m_botinvadercurrentframe = 0; } m_botinvaderhitbox = new rectangle(m_botinvadercurrentframe * m_botinvaderframewidth, 0, m_botinvaderframewidth, m_botinvaderframeheight); m_botinvaderorigin = new vector2(m_botinvaderhitbox.width / 2, m_botinvaderhitbox.height / 2); int m_rightside = 800; int m_leftside = 0; (int r = 0; r < m_invaderrows; r++) { (int c = 0; c < m_invadercollumns; c++) { if (m_botinvadersdirection.equals ("right")) { m_botinvadersrect[r, c].x = m_botinvadersrect[r, c].x + 1; } if (m_botinvadersdirection.equals("left")) { m_botinvadersrect[r, c].x = m_botinvadersrect[r, c].x - 1; } } } string m_botinvaderschangedirection = "n"; (int r = 0; r < m_invaderrows; r++) { (int c = 0; c < m_invadercollumns; c++) { if (m_botinvadersrect[r, c].x + m_botinvadersrect[r, c].width > m_rightside) { m_botinvadersdirection = "left"; m_botinvaderschangedirection = "y"; } if (m_botinvadersrect[r, c].x < m_leftside) { m_botinvadersdirection = "right"; m_botinvaderschangedirection = "y"; } } if (m_botinvaderschangedirection.equals("y")) { (int c = 0; c < m_invadercollumns; c++) { m_botinvadersrect[r, c].y = m_botinvadersrect[r, c].y + 3; } } }
and have follow tutorial , create list of object , in draw method draw each items of list.
Comments
Post a Comment