Code Help - My First Roadblock

Started by
6 comments, last by mistervirtue 11 years, 6 months ago
Hey gamedev'ers,

I am have a lot of problems on my XNA 4.0 project. I am trying to have my texture flip horizontally when I the directional value changes. Can any one give me a hint with out directly telling me what to do. I know it's simple but I can't seem to figure it out. I think it has something to do with the update. Any advice?

-This is My SirHero Class

[source lang="csharp"]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ProjectGasllightKnights
{
class SirHero
{
Vector2 position;
Vector2 direction;
float heroSpeed = 10f;

KeyboardState keyboardState;
GamePadState gamePadState;

Texture2D texture;
Rectangle screenBounds;

public SirHero(Texture2D texture, Rectangle screenBounds)
{
this.texture = texture;
this.screenBounds = screenBounds;
SetInStartPosition();
}
public void Update()
{
direction = Vector2.Zero;

keyboardState = Keyboard.GetState();
gamePadState = GamePad.GetState(PlayerIndex.One);

//If player Press A on keyboard or Left game pad, set their movemnt negative direction
if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left)
|| gamePadState.IsButtonDown(Buttons.DPadLeft) || gamePadState.IsButtonDown(Buttons.LeftThumbstickLeft))
{
direction.X = -1;
}
//If player press D on keyboard or right on gamepad set their movement in the postive direction
if (keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right)
|| gamePadState.IsButtonDown(Buttons.DPadRight) || gamePadState.IsButtonDown(Buttons.RightThumbstickRight))
{
direction.X = 1;
}

//Movement Direction
direction.X = heroSpeed * direction.X;

// Actually Move the Texture
position += direction;

//Check to see if the texture has gone out of bounds
LockHero();
}
private void LockHero()
{
//Don't Let Sir Hero run off the screen
if (position.X < 0)
{
position.X = 0;
}
if (position.X + texture.Width > screenBounds.Width)
{
position.X = screenBounds.Width - texture.Width;
}
}
public void SetInStartPosition()
{
position.X = (screenBounds.Width - texture.Width) / 2;
position.Y = screenBounds.Height - texture.Height;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
public Rectangle GetBounds()
{
return new Rectangle(
(int)position.X, (int)position.Y, (texture.Width), (texture.Height));
}
}
}[/source]


This is my Game1 class.
[source lang="csharp"]sing 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 ProjectGasllightKnights
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Rectangle screenRectangle;
SirHero playerCharcater;

Texture2D mTexture;



public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 600;
graphics.PreferredBackBufferWidth = 750;
screenRectangle = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
}

/// <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()
{

spriteBatch = new SpriteBatch(GraphicsDevice);

Texture2D tempTexture = Content.Load<Texture2D>("SirHero");
playerCharcater = new SirHero(tempTexture, screenRectangle);

StartGame();

}
private void StartGame()
{
playerCharcater.SetInStartPosition();
}

/// <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();
playerCharcater.Update();
// 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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
playerCharcater.Draw(spriteBatch);
spriteBatch.End();
// TODO: Add your drawing code here

base.Draw(gameTime);
}
}
}
[/source]


Yeah, I am pretty sure it has to do something with update or maybe draw. Any advice would be appreciated. Like i said I don't want the answer to my problem, just some pointers or hints in the right direction.
Advertisement
SpriteBatch.Draw has a parameter, SpriteEffects, that can be inserted into one of the overloads to handle simple sprite flipping. It also has a rotation value if you need it. Bear in mind rotation is typically done in Radians, not Degrees.
So I got this far
[source lang="csharp"]SpriteEffects myFlip = SpriteEffects.None


// Code and Stuff //


//If player Press A on keyboard or Left game pad, set their movemnt negative direction
if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left)
|| gamePadState.IsButtonDown(Buttons.DPadLeft) || gamePadState.IsButtonDown(Buttons.LeftThumbstickLeft))
{
direction.X = -1;
myFlip = SpriteEffects.FlipVertically;
}
else
{
myFlip = SpriteEffects.None;
}
[/source]
But still can't get SirHero to turn in my game. I don't know how to implement myFlip in to the draw,

But still can't get SirHero to turn in my game. I don't know how to implement myFlip in to the draw,


myFlip would be supplied as a argument to SpriteBatch.Draw()
Check the MSDN entry for SpriteBatch.Draw and look for one of the overloads that uses SpriteEffects as a parameter - There's a few, and you'll need to figure them out to pick the one that's most relevant to what you need to do.

Where you have:
[source lang="csharp"]spriteBatch.Draw(texture, position, Color.White);[/source]
You will likely need something like:
[source lang="csharp"]spriteBatch.Draw(texture, position, null, Color.White, 0f, Vector2.Zero, 1f, myFlip, 0f);[/source]

You may notice there's a lot more information there. Refer back to the SpriteBatch.Draw page for a good understanding of what each one does. You'll end up needing to use them sooner or later!
Well I haven't really changed my drawing code it's still the same. That is my problem I don't know how to pass myFlip in to my draw
[source lang="csharp"] public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}[/source]
Mistervirtue were you able to get it working using Haps advice?
ALL RIGHT! WE ARE BACK IN ACTION Gamedev'ers! Thanks for all your help! It totally worked. I had never seen some like Haps stuff. But I get my research on and get better! LET'S DO THIS!

This topic is closed to new replies.

Advertisement