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.
Edited by mistervirtue, 28 September 2012 - 11:13 AM.






