Danh mục

Building XNA 2.0 Games- P5

Số trang: 30      Loại file: pdf      Dung lượng: 1.04 MB      Lượt xem: 10      Lượt tải: 0    
Hoai.2512

Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Building XNA 2.0 Games- P5: I would like to acknowledge John Sedlak, who saved this book from certain doom, as well asall of the great guys in the XNA community and Microsoft XNA team, who helped me with allof my stupid programming questions. (That is actually the term used—“stupid programmingquestion”—and it is a question that one should not have to ask if one has been approached towrite a book about the subject.)
Nội dung trích xuất từ tài liệu:
Building XNA 2.0 Games- P5 CHAPTER 5 ■ THE CHARACTER EDITOR 107 So far, we’re iterating through all images for each texture, getting the source and destina-tion rectangles so that we can draw them in a neat row on the bottom of the screen. Of course,the special case with weapons is coming right up: if (l == 3) { sRect.X = (i % 4) * 80; sRect.Y = (i / 4) * 64; sRect.Width = 80; if (i < 15) { dRect.X = i * 30; dRect.Width = 30; } } With the correct source and destination rectangles, we draw the image. But since we havethe destination rectangle, we might as well check if the mouse location is within the rectangleand clicking: spriteBatch.Draw(texture, dRect, sRect, Color.White); if (dRect.Contains(mouseState.X, mouseState.Y)) { if (mouseClick) { charDef.Frames[selFrame].Parts[selPart].Index = i + 64 * l; } } } }}spriteBatch.End(); Assuming we add a call to DrawPalette() and DrawCursor() at the end of Game1.Draw()somewhere, we’ll be treated to the result shown in Figure 5-6. Also, be sure to set mouseClick tofalse at the end of the Draw() method.108 CHAPTER 5 ■ THE CHARACTER EDITOR Figure 5-6. Icon palette The Parts List We’ll use the icon palette to specify which image index each part uses. The parts list will allow us to manipulate our composited character in a way similar to a layer-heavy image-editing approach. We’ll be able to select a part to manipulate, move parts up and down the list (like Send to Bottom and Bring to Top in layer ordering), and delete parts. We do this in a method called Game1.DrawPartsList(), as follows: for (int i = 0; i < charDef.Frames[selFrame].Parts.Length; i++) { int y = 5 + i * 15; text.Size = 0.75f; string line = ; int index = charDef.Frames[selFrame].Parts[i].Index; if (index < 0) line = ; CHAPTER 5 ■ THE CHARACTER EDITOR 109 else if (index < 64) line = head + index.ToString(); else if (index < 74) line = torso + index.ToString(); else if (index < 128) line = arms + index.ToString(); else if (index < 192) line = legs + index.ToString(); else line = weapon + index.ToString(); if (selPart == i) { text.Color = Color.Lime; text.DrawText(600, y, i.ToString() + : + line); We’ll put in two buttons to swap the current part with the one on the previous or nextlayer, using a function named Game1.SwapParts(): if (DrawButton(700, y, 1, mouseState.X, mouseState.Y, mouseClick)) { SwapParts(selPart, selPart - 1); if (selPart > 0) selPart--; } if (DrawButton(720, 5 y, 2, mouseState.X, mouseState.Y, mouseClick)) { SwapParts(selPart, selPart + 1); if (selPart < charDef.Frames[selFrame].Parts.Length - 1) selPart++; } We’ll put some makeshift buttons next to the swap buttons to modify the parts. One of these isto mirror parts. For the mirror button, we’ll use an (n) for normal and an (m) for mirrored. Part part = charDef.Frames[selFrame].Parts[selPart]; if (text.DrawClickText(740, y, (part.Flip == 0 ? (n) : (m)), mouseState.X, mouseState.Y, mouseClick)) { part.Flip = 1 - part.Flip; } Because scaling leaves all sorts of openings for things to go terribly wrong in artistic consis-tency, we’ll put in a button next to the selected part to reset the scale, denoted with an (r). We’llalso add a part delete button, marked with an (x).110 CHAPTER 5 ■ THE CHARACTER EDITOR if (text.DrawClickText(762, y, (r), mouseState.X, mouseState.Y, mouseClick)) { part.Scaling = new Vector2(1.0f, 1.0f); } if (text.DrawClickText(780, y, (x), mouseState.X, mouseState.Y, mouseClick)) { part.Index = -1; } } else { if (text.DrawClickText(600, y, i.ToString() + : + line, mouseState.X, mouseState.Y, mouseClick)) { selPart = i; } } } ...

Tài liệu được xem nhiều: