1 Replies - 620 Views - Last Post: 20 January 2012 - 04:02 PM

#1 Tyreece  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-January 12

New to C# / XNA 4.0 and I'm having trouble with sprite sheets.

Posted 19 January 2012 - 09:43 PM

I've been working on a programming assignment that wants me to take a sprite sheet consisting of 15 different tiles and place them randomly into the client window. When I run my code, the tiles randomly appear/disappear in a diagonal line from the origin down to the bottom right corner of the window. Could some one look at my code to see what I could be doing wrong?

Link to tile sheet if needed - http://i.imgur.com/GFmgX.png

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
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;

namespace Tiles
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        
        SpriteBatch spriteBatch;
        
        Texture2D texPete;
        Texture2D texTiles;

        Vector2 pos = Vector2.Zero;

        

        private int random(int min, int max)
        {
            Random rand = new Random();
            return rand.Next(min, max);
        }

        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            texPete = Content.Load<Texture2D>(@"Images/Pete");
            texTiles = Content.Load<Texture2D>(@"Images/Tilesheet");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            float speed = random(-5, 5); 

            pos.X += speed;
            if (pos.X > window.ClientBounds.Width - texTiles.Width ||
                pos.X < 0)
                speed *= -1;
            if (pos.Y > window.ClientBounds.Height - texTiles.Height ||
                pos.Y < 0)
                speed *= -1;

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            // TODO: Add your drawing code here

            spriteBatch.Begin(SpriteSortMode.FrontToBack,BlendState.AlphaBlend);
            
            spriteBatch.Draw(texPete,
                new Vector2(
                    (window.ClientBounds.Width / 2) - (texPete.Width / 2),
                    (window.ClientBounds.Height / 2) - (texPete.Height / 2 )),
                    null,
                    Color.White,
                    0,
                    Vector2.Zero,
                    1,
                    SpriteEffects.None,
                    0);
         
            for(int x = 0; x < 5; x++)
            {
                for(int y = 0; y < 3; y++)
                {
                    //Calculate tiles position
                    
                    Rectangle rTile = new Rectangle(x * 77, y * 77, 75, 75);
                    
                    //Calculate position on sheet

                    int pos = y * 5 + x;
                    int min = 0;
                    int maxX = window.ClientBounds.Width - pos;
                    int maxY = window.ClientBounds.Height - pos;

                    Rectangle rDraw = new Rectangle((random(min,maxX) * 75), (random(min,maxY) * 75), 75, 75);

                    spriteBatch.Draw(texTiles, rDraw, rTile, Color.White);
                }
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: New to C# / XNA 4.0 and I'm having trouble with sprite sheets.

#2 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: New to C# / XNA 4.0 and I'm having trouble with sprite sheets.

Posted 20 January 2012 - 04:02 PM

because draw is run like 60 times per second and you have some "random" there. so its different every 1/60 of second.

Sprite Sheets are best handled with separate class and you should create some function like GetFrame(int x) where x will represent index of tile.

this is code from my far from perfect class which hold texture (sprite sheet, AnimSources[] is array of rectangles;FramesHorizontally,FramesVertically are ints telling how much frames given ss have )
Spoiler

This post has been edited by Nerfpl: 20 January 2012 - 04:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1