My player's sprite wont draw

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 4043 Views - Last Post: 02 November 2010 - 11:56 PM

#1 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

My player's sprite wont draw

Posted 21 September 2010 - 01:10 PM

Sorry to burden with so much code. But this is obviously a technical issue i cant comprehend or something. I have 2 sprites, with my main game1 class and my Ship class which holds the controls and properties of my player's sprite. The background draws fine, so it might be something going on in the Draw() method in the game component not sure. Thank you for the help and instant rep for everyone who comments and helps in some way...

Here is the Game1 class:
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace New_Rock_Rain
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch = null;

        Texture2D backgroundTexture;
        private Texture2D meteorTexture;
        private Ship player;

        private void Start()
        {
            if (player == null)
            {
                player = new Ship(this, ref meteorTexture);
                Components.Add(player);
            }
            player.PutInStartPosition();
        }
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            backgroundTexture = Content.Load<Texture2D>("starfield");
            meteorTexture = Content.Load<Texture2D>("SquareGuy");
            Services.AddService(typeof(SpriteBatch), spriteBatch);
        }
        protected override void UnloadContent()
        {
        }
        protected override void Update(GameTime gameTime)
        {
            if (player == null)
                Start();

            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(
                backgroundTexture, new Rectangle(0,0, 
                graphics.GraphicsDevice.DisplayMode.Width,
                graphics.GraphicsDevice.DisplayMode.Height),
                Color.White
                );
            spriteBatch.End();
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            // Draw the game components (sprites included)
            base.Draw(gameTime);
            // End rendering sprites
            spriteBatch.End();
        }
    }
}



And here is the Ship.cs Gamne Component:
#region Using Statments
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace New_Rock_Rain
{
   
    public class Ship : Microsoft.Xna.Framework.DrawableGameComponent
    {

        protected Texture2D texture;
        protected Rectangle spriteRectangle;
        protected Vector2 position;

        protected const int SHIPWIDTH = 30;
        protected const int SHIPHEIGHT = 30;

        protected Rectangle screenBounds;


        public Ship(Game game, ref Texture2D theTexture)
            : base(game)
        {
            texture = theTexture;
            position = new Vector2();

            spriteRectangle = new Rectangle(31, 83, SHIPWIDTH, SHIPHEIGHT);
        }

        public void PutInStartPosition()
        {
            position.X = screenBounds.Width / 2;
            position.Y = screenBounds.Height - SHIPHEIGHT;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            //This is so the user can move
            KeyboardState keyboard = new KeyboardState();
            if (keyboard.IsKeyDown(Keys.Up))
                position.Y -= 3;
            if (keyboard.IsKeyDown(Keys.Down))
                position.Y += 3;
            if (keyboard.IsKeyDown(Keys.Left))
                position.X -= 3;
            if (keyboard.IsKeyDown(Keys.Right))
                position.X += 3;


            //This keeps the ship inside the screen we have made
            if (position.X < screenBounds.Left)
                position.X = screenBounds.Left;
            if (position.X > screenBounds.Right - SHIPWIDTH)
                position.X = screenBounds.Right - SHIPWIDTH;
            if (position.Y < screenBounds.Top)
                position.Y = screenBounds.Top;
            if (position.Y > screenBounds.Bottom - SHIPHEIGHT)
                position.Y = screenBounds.Bottom - SHIPHEIGHT;



            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {

            SpriteBatch sBatch =
                (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            sBatch.Draw(texture, position, spriteRectangle, Color.White);

            base.Draw(gameTime);
        }
        public Rectangle GetBounds()
        {
            return new Rectangle((int)position.X, (int)position.Y, SHIPWIDTH, SHIPHEIGHT);
        }



    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: My player's sprite wont draw

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 01:14 PM

You to call the Draw method of the playerShip class to draw the player to the screen. I haven't looked at that Rock Rain tutorial in quite a while, so I can't remember off hand what the call was for that particular game, but try player.Draw() inside the Draw method of the game1 class. This call will call the draw method inside the playerShip class, and as long as you provide the proper parameters, it should work.
Was This Post Helpful? 1
  • +
  • -

#3 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 01:23 PM

Something that I spotted quite quickly after consulting my full RockRain version. I moved the Spritebatch initialization to the constructor instead of the draw() method.
        public Ship(Game game, ref Texture2D theTexture)
            : base(game)
        {
            texture = theTexture;
            position = new Vector2();

            SpriteBatch sBatch =
                (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            spriteRectangle = new Rectangle(31, 83, SHIPWIDTH, SHIPHEIGHT);
        }

        public override void Draw(GameTime gameTime)
        {
            sBatch.Draw(texture, position, spriteRectangle, Color.White);

            base.Draw(gameTime);
        }
        public Rectangle GetBounds()
        {
            return new Rectangle((int)position.X, (int)position.Y, SHIPWIDTH, SHIPHEIGHT);
        }



Oh and move the Start() method in Game1 to after the Game1() constructor and before the loadcontent(), update() and draw() methods.

This post has been edited by ShadowsEdge19: 21 September 2010 - 01:20 PM

Was This Post Helpful? 1
  • +
  • -

#4 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 01:40 PM

Shadow when i moved the SpriteBatch initializer the sBatch variable gets and error and says it doesnt exist in this context
Was This Post Helpful? 0
  • +
  • -

#5 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 01:53 PM

View PostDev1462, on 21 September 2010 - 12:40 PM, said:

Shadow when i moved the SpriteBatch initializer the sBatch variable gets and error and says it doesnt exist in this context


#region Using Statments
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace New_Rock_Rain
{
   
    public class Ship : Microsoft.Xna.Framework.DrawableGameComponent
    {

        protected Texture2D texture;
        protected Rectangle spriteRectangle;
        protected Vector2 position;

        protected const int SHIPWIDTH = 30;
        protected const int SHIPHEIGHT = 30;
        SpriteBatch sBatch;

        protected Rectangle screenBounds;


        public Ship(Game game, ref Texture2D theTexture)
            : base(game)
        {
            texture = theTexture;
            position = new Vector2();

            sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            spriteRectangle = new Rectangle(31, 83, SHIPWIDTH, SHIPHEIGHT);
        }

        public void PutInStartPosition()
        {
            position.X = screenBounds.Width / 2;
            position.Y = screenBounds.Height - SHIPHEIGHT;
        }

        public override void Update(GameTime gameTime)
        {
            //This is so the user can move
            KeyboardState keyboard = new KeyboardState();
            if (keyboard.IsKeyDown(Keys.Up))
                position.Y -= 3;
            if (keyboard.IsKeyDown(Keys.Down))
                position.Y += 3;
            if (keyboard.IsKeyDown(Keys.Left))
                position.X -= 3;
            if (keyboard.IsKeyDown(Keys.Right))
                position.X += 3;


            //This keeps the ship inside the screen we have made
            if (position.X < screenBounds.Left)
                position.X = screenBounds.Left;
            if (position.X > screenBounds.Right - SHIPWIDTH)
                position.X = screenBounds.Right - SHIPWIDTH;
            if (position.Y < screenBounds.Top)
                position.Y = screenBounds.Top;
            if (position.Y > screenBounds.Bottom - SHIPHEIGHT)
                position.Y = screenBounds.Bottom - SHIPHEIGHT;

            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            sBatch.Draw(texture, position, spriteRectangle, Color.White);

            base.Draw(gameTime);
        }
        public Rectangle GetBounds()
        {
            return new Rectangle((int)position.X, (int)position.Y, SHIPWIDTH, SHIPHEIGHT);
        }
    }
}



Now I only added the sBatch to the properties list at the top and then initialized at the contructor. However if that still doesn't work I suggest you try reapplying the sprite content to the project that you found yourself, and also tell us any error messages that pop up.
Was This Post Helpful? 1
  • +
  • -

#6 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 02:05 PM

Shadow your way works great but sadly the sprite still doesn't show, to test the sprite i simple created it the beginners way using spriteBatch.Draw(); and it was there, so this isn't an issue with that, something isn't communicating between the Game1 class and the Ship class.
Was This Post Helpful? 0
  • +
  • -

#7 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 02:34 PM

I've compared the game1 to my own working one and I suggest you replace it with this, no major changes so I'm not sure if it will actually change things for the better. If it still doesn't work then it's something to do with the content itself, did you put the images into the folder in windows explorer or did you drop it onto the content folder in the Visual Studio folder list?

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace New_Rock_Rain
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        //Graphical stuff
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private Texture2D backgroundTexture, meteorTexture;
        
        //Game Stuff
        private Ship player;

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

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Services.AddService(typeof(SpriteBatch), spriteBatch);
            
            backgroundTexture = Content.Load<Texture2D>("starfield");
            meteorTexture = Content.Load<Texture2D>("SquareGuy");
            
        }
        protected override void UnloadContent()
        {
        }
        
        protected override void Update(GameTime gameTime)
        {
            if (player == null)
                Start();

            base.Update(gameTime);
        }
        
         
        private void Start()
        {
            if (player == null)
            {
                player = new Ship(this, ref meteorTexture);
                Components.Add(player);
            }
            player.PutInStartPosition();
        }       
        
        
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(
                backgroundTexture, new Rectangle(0,0, 
                graphics.GraphicsDevice.DisplayMode.Width,
                graphics.GraphicsDevice.DisplayMode.Height),
                Color.White
                );
            spriteBatch.End();
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            // Draw the game components (sprites included)
            base.Draw(gameTime);
            // End rendering sprites
            spriteBatch.End();
        }
    }
}



This post has been edited by ShadowsEdge19: 21 September 2010 - 02:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 02:46 PM

I know what's wrong, you don't have the RockRain.png image that contains the ship and meteor images.

Here's the basic version:
Posted Image

and here's the final Rock Rain Enhanced version:
Posted Image

Change the load content for the meteorTexture to look for RockRain instead of the current one and use the image provided in the content folder.
Was This Post Helpful? 0
  • +
  • -

#9 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 03:12 PM

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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace New_Rock_Rain
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch = null;

        private Texture2D backgroundTexture, meteorTexture;
        private Ship player;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Services.AddService(typeof(SpriteBatch), spriteBatch);
            backgroundTexture = Content.Load<Texture2D>("starfield");
            meteorTexture = Content.Load<Texture2D>("RockRain");
        }
        protected override void UnloadContent()
        {
        }
        protected override void Update(GameTime gameTime)
        {
            if (player == null)
                Start();

            base.Update(gameTime);
        }
        private void Start()
        {
            if (player == null)
            {
                player = new Ship(this, ref meteorTexture);
                Components.Add(player);
            }
            player.PutInStartPosition();
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(
                backgroundTexture, new Rectangle(0,0, 
                graphics.GraphicsDevice.DisplayMode.Width,
                graphics.GraphicsDevice.DisplayMode.Height),
                Color.White
                );
            spriteBatch.End();
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            base.Draw(gameTime);
            spriteBatch.End();
        }
    }
}



I changed it, but you only moved the method. Secondly what i did was right-click content and added an existing item, also idk how to use a tile set...only single images...could you maybe upload your version? i wish this wasnt so fustrating. I think the Game1 isnt the problem i think its something in the Ship Game component

This post has been edited by Dev1462: 21 September 2010 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

#10 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 03:17 PM

View PostDev1462, on 21 September 2010 - 02:12 PM, said:

Secondly what i did was right-click content and added an existing item, also idk how to use a tile set...only single images...could you maybe upload your version? i wish this wasnt so fustrating


But then what is gained by the tutorial if you skip to the end? btw from your code it's already established how it will read the tile set for the ship and the meteors, you just need to use it and it'll work.

The line it finds the ship in the tile set is in ship() :
spriteRectangle = new Rectangle(31, 83, SHIPWIDTH, SHIPHEIGHT);

This post has been edited by ShadowsEdge19: 21 September 2010 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

#11 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 03:38 PM

View PostShadowsEdge19, on 21 September 2010 - 02:17 PM, said:

View PostDev1462, on 21 September 2010 - 02:12 PM, said:

Secondly what i did was right-click content and added an existing item, also idk how to use a tile set...only single images...could you maybe upload your version? i wish this wasnt so fustrating


But then what is gained by the tutorial if you skip to the end? btw from your code it's already established how it will read the tile set for the ship and the meteors, you just need to use it and it'll work.

The line it finds the ship in the tile set is in ship() :
spriteRectangle = new Rectangle(31, 83, SHIPWIDTH, SHIPHEIGHT);


ahhh, i see, it would have been really cool is he told me that instead of you know just showing me the code and making me figure that out. w.e and you're right i got to figure out wtf is going wrong, ill copy and past your code and see what might be wrong

edit: NOPE did work either, it HAS to be the Ship class at this point it just has to be, do you have that class too? I also commented out the background and left it with the blue to see if maybe it was effecting anything, and no it still doesn't show, its definitely that other component

This post has been edited by Dev1462: 21 September 2010 - 03:48 PM

Was This Post Helpful? 0
  • +
  • -

#12 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 03:59 PM

I've checked my old project now that I'm at home, and your Ship class is perfectly fine. Do me a favor and paste the code that you have now, after the changes and let me take a look through all of it with a fine toothed comb.
Was This Post Helpful? 0
  • +
  • -

#13 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 04:09 PM

Thank you man, i know there has to be a problem, this is what i have

Game1.cs:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace New_Rock_Rain
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        //Graphical stuff
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private Texture2D backgroundTexture, meteorTexture;

        //Game Stuff
        private Ship player;

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

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Services.AddService(typeof(SpriteBatch), spriteBatch);

            backgroundTexture = Content.Load<Texture2D>("starfield");
            meteorTexture = Content.Load<Texture2D>("RockRain");

        }
        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (player == null)
                Start();

            base.Update(gameTime);
        }


        private void Start()
        {
            if (player == null)
            {
                player = new Ship(this, ref meteorTexture);
                Components.Add(player);
            }
            player.PutInStartPosition();
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(
                backgroundTexture, new Rectangle(0, 0,
                graphics.GraphicsDevice.DisplayMode.Width,
                graphics.GraphicsDevice.DisplayMode.Height),
                Color.LightGray
                );
            spriteBatch.End();
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            // Draw the game components (sprites included)
            base.Draw(gameTime);
            // End rendering sprites
            spriteBatch.End();
        }
    }
}


Now here is the Ship.cs:
#region Using Statments
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace New_Rock_Rain
{
   
    public class Ship : Microsoft.Xna.Framework.DrawableGameComponent
    {

        protected Texture2D texture;
        protected Rectangle spriteRectangle;
        protected Vector2 position;

        protected const int SHIPWIDTH = 30;
        protected const int SHIPHEIGHT = 30;
        //SpriteBatch sBatch;

        protected Rectangle screenBounds;


        public Ship(Game game, ref Texture2D theTexture)
            : base(game)
        {
            texture = theTexture;
            position = new Vector2();

            //sBatch =
            //    (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            spriteRectangle = new Rectangle(31, 83, SHIPWIDTH, SHIPHEIGHT);

#if XBOX360
// On the 360, we need to be careful about the TV's "safe" area.
screenBounds = new Rectangle(
(int)(Game.window.ClientBounds.Width * 0.03f),
(int)(Game.window.ClientBounds.Height * 0.03f),
Game.window.ClientBounds.Width -
(int)(Game.window.ClientBounds.Width * 0.03f),
Game.window.ClientBounds.Height -
(int)(Game.window.ClientBounds.Height * 0.03f));#else
screenBounds = new Rectangle(0,0,
Game.window.ClientBounds.Width,
Game.window.ClientBounds.Height);
#endif
        }

        public void PutInStartPosition()
        {
            position.X = screenBounds.Width / 2;
            position.Y = screenBounds.Height - SHIPHEIGHT;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            //This is so the user can move
            KeyboardState keyboard = new KeyboardState();
            if (keyboard.IsKeyDown(Keys.Up))
                position.Y -= 3;
            if (keyboard.IsKeyDown(Keys.Down))
                position.Y += 3;
            if (keyboard.IsKeyDown(Keys.Left))
                position.X -= 3;
            if (keyboard.IsKeyDown(Keys.Right))
                position.X += 3;


            //This keeps the ship inside the screen we have made
            if (position.X < screenBounds.Left)
                position.X = screenBounds.Left;
            if (position.X > screenBounds.Right - SHIPWIDTH)
                position.X = screenBounds.Right - SHIPWIDTH;
            if (position.Y < screenBounds.Top)
                position.Y = screenBounds.Top;
            if (position.Y > screenBounds.Bottom - SHIPHEIGHT)
                position.Y = screenBounds.Bottom - SHIPHEIGHT;



            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {

            SpriteBatch sBatch =
                sBatch =
                (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            sBatch.Draw(texture, position, spriteRectangle, Color.White);


            base.Draw(gameTime);
        }
        public Rectangle GetBounds()
        {
            return new Rectangle((int)position.X, (int)position.Y, SHIPWIDTH, SHIPHEIGHT);
        }



    }
}



Posted Image

This post has been edited by Dev1462: 21 September 2010 - 04:13 PM

Was This Post Helpful? 0
  • +
  • -

#14 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 04:18 PM

EDIT: Finally found it! (int)(Game.window.ClientBounds.Height * 0.03f));#else is the problem line. That #else needs to be on a new line.

This post has been edited by Kilorn: 21 September 2010 - 04:22 PM

Was This Post Helpful? 1
  • +
  • -

#15 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: My player's sprite wont draw

Posted 21 September 2010 - 04:21 PM

Here is my full ship.cs, but it has things that will be added further on in the tutorial so you will need to remove them to get it to work.

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace RockRain
{
    /// <summary>
    /// This is a game component that implements the player ship.
    /// </summary>
    public class Ship : Microsoft.Xna.Framework.DrawableGameComponent
    {
        protected Texture2D texture;
        protected Rectangle spriteRectangle;
        protected Vector2 position;
        protected SpriteBatch sBatch;

        // Width and Heigh of sprite in texture
        protected const int SHIPWIDTH = 30;
        protected const int SHIPHEIGHT = 30;

        // Screen Area
        protected Rectangle screenBounds;

        public Ship(Game game, ref Texture2D theTexture): base(game)
        {
            texture = theTexture;
            position = new Vector2();
            // Get the current spritebatch
            sBatch =
                (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            // Create the source rectangle.
            // This represents where is the sprite picture in surface
            spriteRectangle = new Rectangle(31, 83, SHIPWIDTH, SHIPHEIGHT);

            #if XBOX360
            // On the 360, we need take care about the tv "safe" area.
            screenBounds = new Rectangle(
                                (int)(Game.window.ClientBounds.Width * 0.03f),
                                (int)(Game.window.ClientBounds.Height * 0.03f),
                                Game.window.ClientBounds.Width - 
                                (int)(Game.window.ClientBounds.Width * 0.03f),
                                Game.window.ClientBounds.Height - 
                                (int)(Game.window.ClientBounds.Height * 0.03f));            
            #else
            screenBounds = new Rectangle(0,0,
                Game.window.ClientBounds.Width,
                Game.window.ClientBounds.Height);
            #endif
        }

        /// <summary>
        /// Put the ship in your start position in screen
        /// </summary>
        public void PutinStartPosition()
        {
            position.X = screenBounds.Width / 2;
            position.Y = screenBounds.Height - SHIPHEIGHT;
        }

        /// <summary>
        /// Update the ship position 
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            // Move the ship with xbox controller
            GamePadState gamepadstatus = GamePad.GetState(PlayerIndex.One);
            position.Y += (int)((gamepadstatus.ThumbSticks.Left.Y * 3) * -2);
            position.X += (int)((gamepadstatus.ThumbSticks.Left.X * 3) * 2);

            // Move the ship with keyboard
            KeyboardState keyboard = Keyboard.GetState();
            if (keyboard.IsKeyDown(Keys.Up))
            {
                position.Y -= 3;
            }
            if (keyboard.IsKeyDown(Keys.Down))
            {
                position.Y += 3;
            }
            if (keyboard.IsKeyDown(Keys.Left))
            {
                position.X -= 3;
            }
            if (keyboard.IsKeyDown(Keys.Right))
            {
                position.X += 3;
            }

            // Keep the ship inside the screen
            if (position.X < screenBounds.Left)
            {
                position.X = screenBounds.Left;
            }
            if (position.X > screenBounds.Width - SHIPWIDTH)
            {
                position.X = screenBounds.Width - SHIPWIDTH;
            }
            if (position.Y < screenBounds.Top)
            {
                position.Y = screenBounds.Top;
            }
            if (position.Y > screenBounds.Height - SHIPHEIGHT)
            {
                position.Y = screenBounds.Height - SHIPHEIGHT;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// Draw the ship sprite
        /// </summary>
        public override void Draw(GameTime gameTime)
        {            
            // Draw the ship
            sBatch.Draw(texture, position, spriteRectangle, Color.White);

            base.Draw(gameTime);
        }

        /// <summary>
        /// Get the bound rectangle of ship position in screen
        /// </summary>
        public Rectangle GetBounds()
        {
            return new Rectangle((int)position.X, (int)position.Y, 
                SHIPWIDTH, SHIPHEIGHT);
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3