p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 (http://p2p.wrox.com/forumdisplay.php?f=325)
-   -   Chapter 2 pong menu problem (http://p2p.wrox.com/showthread.php?t=58078)

alexmbr May 18th, 2007 10:26 PM

Chapter 2 pong menu problem
 
I started reading the book and following the coding on my laptop, but on chapter 2, on the part that we are suppost to see the menu for the first time by using the TestMenuSprites() test, I don't see any menu, only the background and the code doesn't give me any erros or warnings. But the book says that I am suppost to be seeing the menu. If I run the code that I download from here, the game works and the menu appears.
Is the book right on that part?
Thanks.

phsarjk May 19th, 2007 06:23 AM

Hard to tell what the problem might be.

Have you implemented the Rendersprite methods?
Have you implemented the renderToSprite class, the Drawsprites() method and are you using that method in the pong game draw() routine?


Zulbaric May 19th, 2007 07:27 AM

Quote:

quote:Originally posted by phsarjk
 Hard to tell what the problem might be.

Have you implemented the Rendersprite methods?
Have you implemented the renderToSprite class, the Drawsprites() method and are you using that method in the pong game draw() routine?


Please can you elaborate on this abit more for the more dim (Me) people.

Maybe some source code


alexmbr May 19th, 2007 07:59 AM

Quote:

quote:Originally posted by phsarjk
 Hard to tell what the problem might be.

Have you implemented the Rendersprite methods?
Have you implemented the renderToSprite class, the Drawsprites() method and are you using that method in the pong game draw() routine?


Yes, I did.
Yes, I did.
And yes, the Drawsprites() are called inside the Draw() routine.

I think that the source code should be replaced by parts inside chapters. This way we could compare the code that we are writing against the code that the autor wrote. For example, on chapter 2, if I look at the code, it is different from the code on the book until the part that I am. I don't know if the code will change to become like that later.



Zulbaric May 19th, 2007 09:04 AM

Here are the replacements for the incorrect areas:

public static void TestMenuSprites()
        {
            StartTest(
                delegate
                {
                    testGame.RenderSprite(testGame.menuTexture,
                        new Rectangle(512-XnaPongLogoRect.Width/2, 150,
                        XnaPongLogoRect.Width, XnaPongLogoRect.Height),
                        XnaPongLogoRect);
                    testGame.RenderSprite(testGame.menuTexture,
                        new Rectangle(512-MenuSingleplayerRect.Width/2, 300,
                        MenuSingleplayerRect.Width, MenuSingleplayerRect.Height),
                        MenuSingleplayerRect);
                    testGame.RenderSprite(testGame.menuTexture,
                        new Rectangle(512-MenuMultiplayerRect.Width/2, 350,
                        MenuMultiplayerRect.Width, MenuMultiplayerRect.Height),
                        MenuMultiplayerRect, Color.Orange);
                    testGame.RenderSprite(testGame.menuTexture,
                        new Rectangle(512-MenuExitRect.Width/2, 400,
                        MenuExitRect.Width, MenuExitRect.Height),
                        MenuExitRect);
                });
        } // TestMenuSprites()




        public void RenderSprite(Texture2D texture, Rectangle rect, Rectangle? sourceRect,
            Color color)
        {
            sprites.Add(new SpriteToRender(texture, rect, sourceRect, color));
        } // RenderSprite(texture, rect, sourceRect, color)

        public void RenderSprite(Texture2D texture, Rectangle rect, Rectangle? sourceRect)
        {
            RenderSprite(texture, rect, sourceRect, Color.White);
        } // RenderSprite(texture, rect, sourceRect)

        public void RenderSprite(Texture2D texture, int x, int y, Rectangle? sourceRect,
            Color color)
        {
            RenderSprite(texture,
                new Rectangle(x, y, sourceRect.Value.Width, sourceRect.Value.Height),
                sourceRect, color);
        } // RenderSprite(texture, rect, sourceRect)

        public void RenderSprite(Texture2D texture, int x, int y, Rectangle? sourceRect)
        {
            RenderSprite(texture,
                new Rectangle(x, y, sourceRect.Value.Width, sourceRect.Value.Height),
                sourceRect, Color.White);
        } // RenderSprite(texture, rect, sourceRect)

        public void RenderSprite(Texture2D texture, Rectangle rect, Color color)
        {
            RenderSprite(texture, rect, null, color);
        } // RenderSprite(texture, rect, color)

        public void RenderSprite(Texture2D texture, Rectangle rect)
        {
            RenderSprite(texture, rect, null, Color.White);
        } // RenderSprite(texture, rect)

        public void RenderSprite(Texture2D texture)
        {
            RenderSprite(texture, new Rectangle(0, 0, 1024, 768), null, Color.White);
        } // RenderSprite(texture)


phsarjk May 19th, 2007 12:00 PM

Hmmm...

If you have implemented all the methods you should be able to see the menu.
At least, if you are running the test, instead of the main game.

like so:
Code:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {
        //comment out main game for testing of menu

        //using (PongGame game = new PongGame())
        //{
        //    game.Run();
        //}

        PongGame.TestMenuSprites();
    }
}


abi.exdream.com May 24th, 2007 09:51 PM

If you have a problem like this try to zip up your project and post it here so we can take a look at it and figure out why nothing is on the screen.

Setting breakpoints at the place you expect the rendering to happen is also useful to make sure the code is executed. Other than that there are often cases in which sprites (or other content) is not rendered at all because the sprites are not flushed on the screen with RenderAllSprites or you could do everything right, but then overpaint the screen or even clear it, which will also result in an empty screen.

http://abi.exdream.com

dparsons May 25th, 2007 06:40 AM

Setting a breakpoint would be your best bet. The thing about this book is that it is not a "Step by Step" type of book in that alot of coding is implied. (For example, the book doesn't explicitly tell you to set up all of the RenderSprites methods to reference the RenderSprite method with the signature: text, rect, sourceRect, color)

In any case, in your Program.cs file, set a breakpoint on PongGame.TestMenuSprites() this will allow you to trace the program flow of the game from the very begining. TestMenuSprites will call this overload of RenderSprite:

       public void RenderSprite(Texture2D texture, Rectangle rect, Rectangle? sourceRect)
        {
            RenderSprite(texture, rect, sourceRect, Color.White); //<--This call is important as it adds the texture to the list, without this reference, you list will be empty and DrawSprites() will exit as soon as you call it.
        }
Which, in turn, calls this overload of RenderSprite
        public void RenderSprite(Texture2D texture, Rectangle rect, Rectangle? sourceRect,
            Color color)
        {
            sprites.Add(new SpriteToRender(texture, rect, sourceRect, color));
        }
If I had to guess, I would think that you are not correctly calling the above RenderSprite that adds your texture to the list hence the call to DrawSprite() exits as soon as its entered since your List is empty.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========

alexmbr May 25th, 2007 10:37 PM

Thank you all. The code from Zulbaric worked. It was the RenderSprites codes.



All times are GMT -4. The time now is 11:36 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.