Programming help (C# WITH XNA LIBRARY)
Posted: Thu Feb 19, 2009 3:53 pm
Im using xna 2.0 with visual c# 2008 express edition.
the problem is that every time I get my sprite to bounce off a wall the sound effect only plays on the very bottom and the very right ( I need it to play on the top bottom left and right ). look at the code and see if you can find any errors.
note: space1.Play is the command to make the space sound effect play.
entire code
screenshot of it running
the problem is that every time I get my sprite to bounce off a wall the sound effect only plays on the very bottom and the very right ( I need it to play on the top bottom left and right ). look at the code and see if you can find any errors.
note: space1.Play is the command to make the space sound effect play.
Code: Select all
int MaxX = graphics.GraphicsDevice.Viewport.Width - mSprite.Texture.Width ;
int MinX = 0;
int MaxY = graphics.GraphicsDevice.Viewport.Height - mSprite.Texture.Height ;
int MinY = 0;
if (mSprite.Position.X > MaxX)
{
mSprite.Speed.X *= -1;
space1.Play (mSprite.Position.X = MaxX);
}
else if (mSprite.Position.X < MinX)
{
mSprite.Speed.X *= -1;
space1.Play (mSprite.Position.X = MinX);
}
if (mSprite.Position.Y > MaxY)
{
mSprite.Speed.Y *= -1;
space1.Play (mSprite.Position.Y = MaxY);
}
else if (mSprite.Position.Y < MinY)
{
mSprite.Speed.Y *= -1;
space1.Play (mSprite.Position.Y = MinY);
}
Code: Select all
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;
//****************************************************************
// CISC4345 lab#1 - games example
// DB = (David Bronleewe), EFA = (Edwin Armstrong)
//****************************************************************
// Date Comment who
// ======== ============================================== ===
// 9/03/08 Typed in prog. from tutorial. efa
// 9/08/08 Added bounce code, updated Sprite.cs efa
// 9/08/08 Added UpdateSprite routine and sound. efa
//****************************************************************
namespace games1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
//variables to change the behavior of the game
const int NumPictures = 1;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch; //add a sprite/picture
private static SoundEffect hawk1;
private static SoundEffect space1;
//********** variables go here **************
Sprite mSprite; //added per tutorial part2
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <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
mSprite = new Sprite();
hawk1 = Content.Load<SoundEffect>(@"hawk"); //sound pre-load
space1 = Content.Load<SoundEffect>(@"space");
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()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Random rand = new Random();
mSprite.LoadContent(this.Content, "tux");
mSprite.Position = new Vector2(rand.Next(800), rand.Next(600));
mSprite.Speed = new Vector2(100f + rand.Next(100), 100f + rand.Next(100));
// Here is the sound prototype, as a guide to playback
//public SoundEffectInstance Play(float volume, float pitch, float pan, bool loop)
hawk1.Play(1.0f, 0.0f, 0.0f, false); //makes hawk sound play in begining of program
}
/// <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
// Create a new SpriteBatch, which can be used to draw textures.
}
/// <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();
// TODO: Add your update logic here
UpdateSprite(gameTime);
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
spriteBatch.Begin();
mSprite.Draw(this.spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
//*****************************************************************
// UpdateSprite:: Update the sprites position and speed, bounce
// sprite off walls. (DB) and (efa) last updated 9/08/08
//*****************************************************************
void UpdateSprite(GameTime gameTime)
{
mSprite.Position += mSprite.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
int MaxX = graphics.GraphicsDevice.Viewport.Width - mSprite.Texture.Width ;
int MinX = 0;
int MaxY = graphics.GraphicsDevice.Viewport.Height - mSprite.Texture.Height ;
int MinY = 0;
if (mSprite.Position.X > MaxX)
{
mSprite.Speed.X *= -1;
space1.Play (mSprite.Position.X = MaxX);
}
else if (mSprite.Position.X < MinX)
{
mSprite.Speed.X *= -1;
space1.Play (mSprite.Position.X = MinX);
}
if (mSprite.Position.Y > MaxY)
{
mSprite.Speed.Y *= -1;
space1.Play (mSprite.Position.Y = MaxY);
}
else if (mSprite.Position.Y < MinY)
{
mSprite.Speed.Y *= -1;
space1.Play (mSprite.Position.Y = MinY);
}
}
//*****************************************************************
}
}