Game Design Music and Art

why – gaurdianAQ

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
why does this code not work. the image won't appear on the screen I added the image to the file I am using XNA.
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace CreatingA2DSprite
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;

//The 2D textured to be rendered
Texture2D mTexture;

//The texture start position for the rectangle
int mTextureStartX = 0;

//The object to draw the sprite
SpriteBatch mSpriteBatch;

//The X and Y coordinates to position the Sprite
int mSpriteX = 50;
int mSpriteY = 50;

//The horizontal and vertical movement speeds for the sprite
int mHorizontalSpeed = 3;
int mVerticalSpeed = 3;


public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}


/// <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>
/// Load your graphics content. If loadAllContent is true, you should
/// load content from both ResourceManagementMode pools. Otherwise, just
/// load ResourceManagementMode.Manual content.
/// </summary>
/// <param name="loadAllContent">Which type of content to load.</param>
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
// TODO: Load any ResourceManagementMode.Automatic content

//Initialze the sprite batch object. This will be used to draw the sprite
mSpriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);

//Load the image into the texture object
ContentManager aLoader = new ContentManager(Services);
mTexture = aLoader.Load<Texture2D>("JumpingJelly") as Texture2D;
}

// TODO: Load any ResourceManagementMode.Manual content
}

/// <summary>
/// Unload your graphics content. If unloadAllContent is true, you should
/// unload content from both ResourceManagementMode pools. Otherwise, just
/// unload ResourceManagementMode.Manual content. Manual content will get
/// Disposed by the GraphicsDevice during a Reset.
/// </summary>
/// <param name="unloadAllContent">Which type of content to unload.</param>
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}


/// <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 default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

//Check for keyboard input
CheckKeyboardInput();

//Update the Sprite
UpdateSprite();

base.Update(gameTime);
}


//Update the Sprite
void UpdateSprite()
{
//Auto-move the sprite by speed
mSpriteX += mHorizontalSpeed;
mSpriteY += mVerticalSpeed;

//Check if the sprite has collided with a boundary
if (CheckBoundaryCollision() == true)
{
//Move to the next frame of the sprite when a collision occurs
ChangeSpriteFrame();
}
}

//Check for keyboard input
protected void CheckKeyboardInput()
{
//Get the current state of the keyboard
KeyboardState aKeyboard = Keyboard.GetState();

//Get the current keys being pressed
Keys[] aCurrentKeys = aKeyboard.GetPressedKeys();

//Cycle through all of the keys being pressed and move the sprite accordingly
for (int aCurrentKey = 0; aCurrentKey < aCurrentKeys.Length; aCurrentKey++)
{
//Change the sprite's direction to the left and move it left
if (aCurrentKeys[aCurrentKey] == Keys.Left)
{
if (mHorizontalSpeed > 0)
{
mHorizontalSpeed *= -1;
}
mSpriteX += mHorizontalSpeed;
}

//Change the sprite's direction to the right and move it right
if (aCurrentKeys[aCurrentKey] == Keys.Right)
{
if (mHorizontalSpeed < 0)
{
mHorizontalSpeed *= -1;
}
mSpriteX += mHorizontalSpeed;
}

//Change the sprite's direction to up and move it up
if (aCurrentKeys[aCurrentKey] == Keys.Up)
{
if (mVerticalSpeed > 0)
{
mVerticalSpeed *= -1;
}
mSpriteY += mVerticalSpeed;
}

//Change the sprite's direction to down and move it down
if (aCurrentKeys[aCurrentKey] == Keys.Down)
{
if (mVerticalSpeed < 0)
{
mVerticalSpeed *= -1;
}
mSpriteY += mVerticalSpeed;
}

//Exit the game if the Escape key has been pressed
if (aCurrentKeys[aCurrentKey] == Keys.Escape)
{
this.Exit();
}

}
}

//Check to see if the Sprite has collided with a boundary
protected Boolean CheckBoundaryCollision()
{
//Determine if a boundary collision occurred
Boolean aCollision = false;

//Set the max and min for the X boundaries of the area the sprite can move within
int aMaxX = Window.ClientBounds.Width - 50;
int aMinX = 0;

//Set the max and min for the Y boundaries of the area the sprite can move within
int aMaxY = Window.ClientBounds.Height - 50;
int aMinY = 0;

//Check to see if the sprite has moved out of the allowed area
if (mSpriteX > aMaxX)
{
mHorizontalSpeed *= -1;
mSpriteX = aMaxX;
aCollision = true;
}
else if (mSpriteX < aMinX)
{
mHorizontalSpeed *= -1;
mSpriteX = aMinX;
aCollision = true;
}

if (mSpriteY > aMaxY)
{
mVerticalSpeed *= -1;
mSpriteY = aMaxY;
aCollision = true;
}
else if (mSpriteY < aMinY)
{
mVerticalSpeed *= -1;
mSpriteY = aMinY;
aCollision = true;
}

return aCollision;
}

//Move the sprite to the next frame
protected void ChangeSpriteFrame()
{
if (mTextureStartX == 0)
{
mTextureStartX = 256;
}
else
{
mTextureStartX = 0;
}
}


/// <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)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here
//Begin drawing the scene. Using AlphaBlend as the blend mode will use the transparencies of the images drawn.
mSpriteBatch.Begin(SpriteBlendMode.AlphaBlend);

//Draw the sprite
mSpriteBatch.Draw(mTexture, new Rectangle(mSpriteX, mSpriteY, 50, 50), new Rectangle(mTextureStartX, 0, 256, 256), Color.White);
mSpriteBatch.End();

base.Draw(gameTime);
}
}
}

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
wait that is not it that is the wrong one this is it
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace test2
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
//The 2D textured to be rendered
Texture2D mTexture;

//The texture start position for the rectangle
int mTextureStartX = 0;

//The object to draw the sprite
SpriteBatch mSpriteBatch;

//The X and Y coordinates to position the Sprite
int mSpriteX = 200;
int mSpriteY = 200;

//The horizontal and vertical movement speeds for the sprite
int mHorizontalSpeed = 3;
int mVerticalSpeed = 3;

GraphicsDeviceManager graphics;
ContentManager content;


public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}


/// <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>
/// Load your graphics content. If loadAllContent is true, you should
/// load content from both ResourceManagementMode pools. Otherwise, just
/// load ResourceManagementMode.Manual content.
/// </summary>
/// <param name="loadAllContent">Which type of content to load.</param>
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
// TODO: Load any ResourceManagementMode.Automatic content

//Initialze the sprite batch object. This will be used to draw the sprite
mSpriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);

//Load the image into the texture object
ContentManager aLoader = new ContentManager(Services);
mTexture = aLoader.Load<Texture2D>("spaceship") as Texture2D;

}

// TODO: Load any ResourceManagementMode.Manual content
}


/// <summary>
/// Unload your graphics content. If unloadAllContent is true, you should
/// unload content from both ResourceManagementMode pools. Otherwise, just
/// unload ResourceManagementMode.Manual content. Manual content will get
/// Disposed by the GraphicsDevice during a Reset.
/// </summary>
/// <param name="unloadAllContent">Which type of content to unload.</param>
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}


/// <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 default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

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)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here
//Begin drawing the scene. Using AlphaBlend as the blend mode will use the transparencies of the images drawn.
mSpriteBatch.Begin(SpriteBlendMode.AlphaBlend);

//Draw the sprite
mSpriteBatch.Draw(mTexture, new Rectangle(mSpriteX, mSpriteY, 50, 50), new Rectangle(mTextureStartX, 0, 256, 256), Color.White);
mSpriteBatch.End();


base.Draw(gameTime);
}
}
}

spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
did you try adding a filename extensiont to your filename in your code?

and i don't see your main() function(or WinMain).

------------------
Matthew(22:36-40)"Teacher, which is the greatest commandment in the Law?" Jesus replied: " 'Love the Lord your God with all your heart and with all your soul and with all your mind. This is the first and greatest commandment. And the second is like it: 'Love your neighbor as yourself.All the Law and the Prophets hang on these two commandments."
Whose Son Is the Christ

SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
GuardianAQ, have you figured out the problem yet? I have only used XNA Beta 1 and the new updates are quite a bit different from what I used. Looking at sample code from XNA Resources, it looks like you are doing everything correctly.

Have you actually added the spaceship texture to your game's content?

I recommend heading to XNA Resources and reading their first Tutorial. Download the sample and see if it will work. I think it was written for Beta 2, but I believe that may be close enough to work with XNA Final.

If you figure it out, let us know. I am interested in knowing, so I don't fall into the same problem when I eventually install XNA Final.