Nodoka

Sorry newbie question here! ^_^'

I started the tutorial in Help and I got my sprite to appear on the screen but it is in the top left corner. How do I make my sprite to appear in the center instead of the top left Any help would be much appreciated.

Thanks, Nodoka

------

New question. I got my sprite to appear in the center of the screen. How do I get the sprite to move in a random direction when I compile and run it Here is my code currently:

#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 MyFirstXNAGame

{

public class Game1 : Microsoft.Xna.Framework.Game

{

GraphicsDeviceManager graphics;

ContentManager content;

public Game1()

{

graphics = new GraphicsDeviceManager(this);

content = new ContentManager(Services);

}

protected override void Initialize()

{

// TODO: Add your initialization logic here

base.Initialize();

}

//this is a texture we can render

Texture2D myTexture;

//coordinates to draw the sprite at

Vector2 spritePosition = Vector2.Zero;

//this is the object that will draw the sprites

SpriteBatch spriteBatch;

protected override void LoadGraphicsContent(bool loadAllContent)

{

if (loadAllContent)

{

myTexture = content.Load<Texture2D>("Blue Ball");

spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

}

// TODO: Load any ResourceManagementMode.Manual content

}

protected override void UnloadGraphicsContent(bool unloadAllContent)

{

if (unloadAllContent == true)

{

content.Unload();

}

}

//store some info about the sprite's motion

Vector2 spriteSpeed = new Vector2(100.0f, 100.0f);

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();

//move the sprite around

UpdateSprite(gameTime);

base.Update(gameTime);

}

void UpdateSprite(GameTime gameTime)

{

//move the sprite by speed, scaled by elapsed time

spritePosition += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

spritePosition.X = (graphics.GraphicsDevice.Viewport.Width - myTexture.Width) / 2;

spritePosition.Y = (graphics.GraphicsDevice.Viewport.Height - myTexture.Height) /2;

}

protected override void Draw(GameTime gameTime)

{

graphics.GraphicsDevice.Clear(Color.Black);

//draw our sprite

spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

spriteBatch.Draw(myTexture, spritePosition, Color.White);

spriteBatch.End();

base.Draw(gameTime);

}

}

}



Re: XNA Framework Position Sprite in the Center of Screen using XNA

xissburg

Just set its position this way:

posx = (windowWidth - spriteWidth)/2;
posy = (windowHeight - spriteHeight)/2;

Hope this helps. . .




Re: XNA Framework Position Sprite in the Center of Screen using XNA

Nodoka

alright I'll try that thanks!

new question above.





Re: XNA Framework Position Sprite in the Center of Screen using XNA

musicalglass

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion
  // Tutorials in plain English by Dillinger - http://musicalglass.spaces.live.com 
  /// <summary> This is the main hype for your game </summary>
  public class Game1 : Microsoft.Xna.Framework.Game
  {
  /// <summary> Declare Instances of assorted game stuff. </summary>
  GraphicsDeviceManager graphicDeviceMon; // Da Mon who speaks to da graphics card.
  ContentManager contentMon; // Da Mon dat edits all the useful information out of your newspaper.
  SpriteBatch spriteBatch; // A clean, well lighted place for Sprites.
  Texture2D spriteTexture; // Da vision, Mon.
  // Declare a 2D space Float Vector for storing Sprite Vertical and Horizontal position ( X & Y ) 
  Vector2 spritePosition = new Vector2(0f, 0f);
  // Declare a 2 space Float variable for storing Sprite's Center, or point around which it will rotate.
  Vector2 spriteOrigin = new Vector2(0f, 0f); 
 public Game1()
  {
  // Call a high level meeting of upper management.
  graphicDeviceMon = new GraphicsDeviceManager(this); // You'll be the liason with the Hardware guys.
  contentMon = new ContentManager(Services); // You'll be in charge of the Graphics Dept.
  }
  /// <summary> Allows the game to perform any pre game initialization. </summary>
  protected override void Initialize()
  {
  // TODO: Add your initialization logic here
  base.Initialize();
  }
 
  /// <summary> Send all the Graphics Stuff to the Graphics Card. </summary>
  protected override void LoadGraphicsContent(bool loadAllContent)
  {
  if (loadAllContent)
  {
  // Load Sprite inventory
  spriteBatch = new SpriteBatch(this.graphicDeviceMon.GraphicsDevice);
  spriteTexture = contentMon.Load<Texture2D>(@"FolderName\ImageName");  
  }
 // TODO: Load any ResourceManagementMode.Manual  content
  }
  /// <summary> Dump de Stuff, mon. </summary>
  protected override void UnloadGraphicsContent(bool unloadAllContent)
  {
  if (unloadAllContent)
  {
  contentMon.Unload();
  }
  }

  /// <summary> Upgrade Stuff </summary>
  protected override void Update(GameTime gameTime)
  {
  // Update Sprite Origin from from Top,Left of Texture ( 0,0 ) to center of Sprite
  spriteOrigin.X = spriteTexture .Width / 2;
  spriteOrigin.Y = spriteTexture .Height / 2;
  
  // Update Sprite Position to center of screen
  spritePosition.X = graphicDeviceMon.GraphicsDevice.Viewport.Width / 2;
  spritePosition.Y = graphicDeviceMon.GraphicsDevice.Viewport.Height / 2;
 base.Update(gameTime);
  }
 
  /// <summary> Clear Field & Draw New Stuff </summary>
  protected override void Draw(GameTime gameTime)
  {
  graphicDeviceMon.GraphicsDevice.Clear(Color.Black);
 spriteBatch.Begin();
  // Notice instead of passing 3 Sprite Parameters ( Texture, Position, Color )
  // Draw allows a number of options for Passing Parameters. This time we use
  // Draw Sprite ( Texture, Position, TextureSegmentCoords, Color, Rotation, Origin, Scale, SpriteEffects, Layer )
  spriteBatch.Draw( spriteVisuals, spritePosition, null, Color.White, 0, spriteOrigin, 1, SpriteEffects.None, 0.0f );
 spriteBatch.End();
 base.Draw(gameTime);
  }
  }
  





Re: XNA Framework Position Sprite in the Center of Screen using XNA

Quantumdude

Uh Nodoka, you might need to change some of that code; move the statements that set the sprite to the center of the screen from the update method to the loadGraphicsContent method. From what I'm seeing, I don't think the sprite will move at all - you set it's position, then relocate it right back to the middle of the screen.