Results 1 to 6 of 6

Thread: Game loop problem with XNA

  1. #1
    Join Date
    Jul 2010

    Game loop problem with XNA

    The classic file Program.cs is, as in any application. NET, the entry point of the application, and in the case of applications XNA, instantiates the class Game1 defined in the file Game1.cs and call the method on the Run. The game, in the case of this simple example, where we do not make use of architectural virtuosity, will be fully defined in the file Game1.cs.
    public class game1: Microsoft.Xna.Framework.Game
       GraphicsDeviceManager graphics;
       SpriteBatch SpriteBatch;
       public game1 ()
         graphics = new GraphicsDeviceManager (this);
         Content.RootDirectory = "Content";
         / / Frame rate is 30 fps by default for Windows Phone.
         TargetElapsedTime TimeSpan.FromTicks = (333,333);
    In the constructor of the class we find some important information: first GraphicsDeviceManager object is created that will manage the graphical portion of the device compared to our game. Then property is set RootDirectory content to the directory "content" of the project, where we added the picture just now. The third line of code opens our minds to the world XNA: XNA interface in a game is not based on form or pages in which to place controls that trigger events such as Silverlight, WPF, Windows Forms or additional Visual Basic, but on the concept loop runs at intervals, regular default, in which our code will draw on the screen. For comparison a bit 'forced, but that should better explain the concept to those coming from a more traditional programming, is how to create a blank form and a Timer control that triggers an event at regular intervals of time in which to manage our code.

  2. #2
    Join Date
    May 2008

    Re: Game loop problem with XNA

    The XNA runtime is based on this philosophy: a regular (or irregular willing) time, the runtime calls the method Update is intended to contain the game logic and then invoke the method Draw , aimed at drawing on the screen. Both methods are already in place by the template of Visual Studio and, in a moment, we'll fill them with the code of our simple animation. TargetElapsedTime property indicates the time interval after which the runtime will invoke the two methods and therefore The framerate of our application (ie the number of times the game update our image to the screen). By default, Windows Phone 7, the frame rate is 30 fps (frames per second), while Windows is normally 60 fps. Before you can wire the code in the methods Update and Draw must inform the runtime that we want to use the image above in addition to our project "Content".

  3. #3
    Join Date
    Feb 2010

    Re: Game loop problem with XNA

    You should also know that by default, Windows Phone 7, the frame rate is 30 fps (frames per second), while Windows is normally 60 fps. Before you can wire the code in the methods Update and Draw must inform the runtime that we want to use the image above in addition to our project "Content". Since RootDirectory has already been set, you simply define a variable to hold the Texture and assign it into the method LoadContent : This method is called only once at the start of the game and gives us a central place to load resources. You use the generic method Load<T> Item Content that receives the Asset Name in the game to load the resource. In our case, T must be set to Texture2D in when the image included in the project Content is a two-dimensional texture.

  4. #4
    Join Date
    Feb 2010

    Re: Game loop problem with XNA

    To do this you need to define a variable in the class and value in the method game1 LoadContent as seen in the following code, the default method that has created the template for the project, to which we added a few lines:
    Texture2D logoTexture;
             / / / 
             / / / LoadContent will be called ounces per game and is the position to load
             / / / All of your content. 
             / / / 
             protected override void LoadContent ()
                 / / Create a new SpriteBatch, Which can be used to draw textures.
                 SpriteBatch = new SpriteBatch (GraphicsDevice);
                 // TODO: use this.Content to load your game content
    			 / / [Addition] 
                 logoTexture this.Content.Load <Texture2D> = ("logo");
    The primary step, parting aside for now the logical algorithms, is to illustrate the image, the interface design is done from the Draw method.

  5. #5
    Join Date
    Feb 2010

    Re: Game loop problem with XNA

    With the proposed code from Visual Studio, which automatically initializes the object SpriteBatch, class of XNA that is committed to draw the sprites on the screen, we can directly use this instance to draw the image. The method Draw class SpriteBatch must always be between a call to Begin and a call to the End : This technique allows you to group several operations into one transaction to design video. The code to draw our image on the screen, as Texture2D loaded in the Load method is as follows:
    spriteBatch.Begin ();
                 spriteBatch.Draw (logoTexture, new Vector2 (0, 0), Color.White);
                 spriteBatch.End ();
    Running our application with the classic F5 , you will get the emulator that will present our image in the upper left, as we indicated in the preceding code using the class Vector2 , a two-dimensional vector, which contains the values for 'X and Y axis (by default, the location of image is calculated by taking as a reference point in the upper left corner of the image).

  6. #6
    Join Date
    Feb 2010

    Re: Game loop problem with XNA

    The coordinates XNA on the phone, by default, based on the position Left Landscape, or with the device oriented horizontally to the left starting from the vertical position. Then rotate the emulator using the appropriate button to check out. Enrich the simple example of trying to move the image diagonally down: for it will be necessary that the method Draw has two coordinate variables in each cycle. So we define a variable within the class Game1 , type Vector2 that every call in the loop of the method Update is increased by 1 position. You must define a variable outside the method, since, as we now understand, there are two methods that work in the loop: Update to the application logic and Draw for the design of the interface.

Similar Threads

  1. loop Pedals in Rocksmith Game
    By Doshti in forum Video Games
    Replies: 6
    Last Post: 24-10-2011, 04:06 PM
  2. Stuck in Case failed loop in L.A. Noire Game
    By Cherilyn in forum Video Games
    Replies: 9
    Last Post: 03-10-2011, 07:25 PM
  3. How to check Bingo game numbers in loop
    By Afznotermi in forum Software Development
    Replies: 3
    Last Post: 29-10-2009, 03:19 PM
  4. Problem of infinite loop in C
    By Firon in forum Software Development
    Replies: 4
    Last Post: 11-05-2009, 07:12 PM
  5. Problem with a foreach loop in PHP
    By JiJi in forum Software Development
    Replies: 2
    Last Post: 20-11-2008, 05:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts