lander of game, not landing on the landing pad but passing through - c#

i want to get the lander of this xna game to successfully touchdown on the landing pad, and then play the sound, however, it passes right through and the sound still plays. the complete code for this is:
namespace MoonLander
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class BasicLunarLander : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SoundEffectInstance EndGameSoundInstance;
Texture2D BackgroundImage;
Texture2D LandingImage;
SoundEffect EndGameSound;
int LanderPosX;
int LanderPosY;
Texture2D LandingPadImage;
public BasicLunarLander()
{
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
LanderPosX = 100;
LanderPosY = 0;
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);
// TODO: use this.Content to load your game content here
BackgroundImage = Content.Load<Texture2D>("background");
LandingImage = Content.Load<Texture2D>("lander");
LandingPadImage = Content.Load<Texture2D>("landingpad");
EndGameSound = Content.Load<SoundEffect>("crowdcheer");
EndGameSoundInstance = EndGameSound.CreateInstance();
}
/// <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();
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
LanderPosY = LanderPosY + 2;
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
LanderPosY = LanderPosY - 2;
}
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
LanderPosX = LanderPosX - 2;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
LanderPosX = LanderPosX + 2;
}
// TODO: Add your update logic here
Rectangle rectLander = new Rectangle(LanderPosX, LanderPosY, LandingImage.Width, LandingImage.Height);
Rectangle rectLandingPad = new Rectangle(350, 500, LandingPadImage.Width, LandingPadImage.Height);
if (rectLander.Intersects(rectLandingPad))
{
EndGameSoundInstance.Play();
}
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)
{
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(BackgroundImage, Vector2.Zero, Color.White);
spriteBatch.Draw(LandingImage, new Vector2(LanderPosX, LanderPosY), Color.White);
spriteBatch.Draw(LandingPadImage, new Vector2(350,500), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Hope you can help me with this issue, thank you all for your time.

Its because you're not doing anything about preventing movement after the lander has landed (the condition has been met). The sound plays of course, because that is what you did code.
Add a variable detecting if it has landed and declare it inside your class
bool _hasLanded = false;
then wrap the key detection in your update method (for up, down, left and right keys) inside and if:
if(!_hasLanded)
{
//keypresses go here
}
and finally, inside your if where you check to see if the rectangles intersect, add
_hasLanded = true;
That should prevent you from being able to move the lander once it has touched the landing pad.

Related

xna sound effect failed to load

I am having trouble getting XNA sound effect to load. I have tried selecting the file through cwd as well as a full static directory, but when I try to build, the sound effect still reads as null. I have triple checked that the file is in the correct place, as well as that it is included in the project, but it still goes unrecognized.
Here is the code I'm working with:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
namespace Prototype0217
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Color clearColor = Color.CornflowerBlue;
SoundEffect noise;
string cwd = System.IO.Directory.GetCurrentDirectory();
string ngnl1 = "A:\\DrawingBoard\\Prototype0217\\Prototype0217\\Content\\ngnl1.mp3";
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
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);
noise = Content.Load<SoundEffect>("Content\\ngnl.mp3");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific 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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
clearColor = Color.Green;
if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
clearColor = Color.Red;
if (GamePad.GetState(PlayerIndex.One).Buttons.X == ButtonState.Pressed)
clearColor = Color.Blue;
if (GamePad.GetState(PlayerIndex.One).Buttons.Y == ButtonState.Pressed)
clearColor = Color.Orange;
if (GamePad.GetState(PlayerIndex.One).Buttons.RightShoulder == ButtonState.Pressed)
noise.Play();
// 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(clearColor);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}

Monogame shader doesn't draw anything

I'm brand new to working with shaders and I'm trying to learn how to use them with Monogame. I've been reading a few tutorials online about them and I'm already running into problems. It may be that the tutorials are outdated? When I apply the effect I just get a blank, cornflowerblue, screen.
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
texture ScreenTexture;
sampler TextureSampler = sampler_state {
Texture = <ScreenTexture>;
};
float4 PixelShaderFunction(float2 TextureCoordinate : TEXCOORD0) : COLOR0{
float4 color = tex2D(TextureSampler, TextureCoordinate);
return color;
}
technique Plain {
pass Pass1 {
PixelShader = compile PS_SHADERMODEL PixelShaderFunction();
}
};
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace ShaderTest {
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game {
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Effect shader1;
Texture2D peach;
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
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);
shader1 = Content.Load<Effect>("shader1");
peach = Content.Load<Texture2D>("peach");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific 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) {
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
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) {
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
shader1.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(peach, new Vector2(0, 0), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
without shader:
with shader:

Sprite not appearing in monogame?

I am currently getting back in to MonoGame but for some reason I am having an issue drawing a simple sprite to the screen.
I am using VS2015 community, created a new multiplatform MonoGame project, and then right-clicked on the Content folder and added existing item to load my 'player.png' image in to the project.
I then wrote the following code to load the image and draw to the screen:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace BulletHell
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D playerImage;
Player player;
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()
{
player = new Player(100, 100, playerImage);
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);
// TODO: use this.Content to load your game content here
playerImage = Content.Load<Texture2D>("player");
player.Image = playerImage;
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific 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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
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)
{
GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(playerImage, new Vector2(0, 0), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Any idea why the sprite is not being displayed?
Thanks
Upon looking at your code again I noticed that base.LoadContent(); is missing in your LoadContent method. If you re-add this it should draw it

C# Monogame snake game

Well first i must say i am a complete noob, I just started with 2d graphics and i experiment on a snake game, in the Update method i call snakeUpdate which updates the position according the keystate the thing is it never updates the position, someone explain a bit please.
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Utilities;
namespace SnakeGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Texture2D background;
private Texture2D shuttle;
private Texture2D earth;
KeyboardState newState = new KeyboardState();
KeyboardState oldState = new KeyboardState();
private float angle = 0;
public Vector2 position= new Vector2(480,240);
public Vector2 velocity;
public Game1()
: base()
{
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
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);
// TODO: use this.Content to load your game content here
background = Content.Load<Texture2D>("stars.jpg"); // change these names to the names of your images
shuttle = Content.Load<Texture2D>("shuttle.png"); // if you are using your own images.
earth = Content.Load<Texture2D>("earth.png");
}
/// <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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
listener();
snakeUp();
// TODO: Add your update logic here
//angle += 0.01f;
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);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
spriteBatch.Draw(earth, new Vector2(400, 240), Color.WhiteSmoke);
Vector2 origin = new Vector2(0, 0);
spriteBatch.Draw(shuttle, position, new Rectangle(0,0, shuttle.Width, shuttle.Height), Color.White, angle, origin, 0.5f, SpriteEffects.None, 1);
spriteBatch.End();
base.Draw(gameTime);
}
public void listener()
{
if (newState.IsKeyDown(Keys.Right) && (oldState.IsKeyUp(Keys.Right)))
velocity = new Vector2(3, 0);
if (newState.IsKeyDown(Keys.Left) && (oldState.IsKeyUp(Keys.Left)))
velocity = new Vector2(-3, 0);
if (newState.IsKeyDown(Keys.Down) && (oldState.IsKeyUp(Keys.Down)))
velocity = new Vector2(0, 3);
if (newState.IsKeyDown(Keys.Up) && (oldState.IsKeyUp(Keys.Up)))
velocity = new Vector2(0, -3);
}
public void snakeUp() {
position += velocity;
}
}
}
You're not assigning oldState and newState correctly: you're initializing those variables (incorrectly) only one time when Game1 is instantiated. You'll need something like this:
public class Game1 : Game {
KeyboardState oldState = Keyboard.GetState();
KeyboardState newState = Keyboard.GetState();
// ...
protected override void Update(GameTime gameTime) {
oldState = newState;
newState = Keyboard.GetState();
// ...
}
}
You are not updating your keyboard state. Write something like this inside your Update method (before listener):
oldState = newState;
newState = Keyboard.GetState();

spriteBatch is Null

I'm creating a game and for the moment I have 3 classes, greenpaddle, ball, and Game1.
When i run my game, the debugger hops down to my spriteBatch.Begin();
and says NullReferenceException Unhandled.
This is my Game1.cs:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Ball ball;
GreenPaddle gPaddle;
Texture2D BackGround;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 500;
}
protected override void Initialize()
{
gPaddle = new GreenPaddle();
ball = new Ball(gPaddle);
}
/// <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);
BackGround = Content.Load<Texture2D>("pongBG");
gPaddle.LoadContent(Content);
ball.LoadContent(Content);
}
/// <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();
gPaddle.Update(gameTime);//Error Line
ball.Update(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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();//Error Line
spriteBatch.Draw(BackGround, new Vector2(0f, 0f), Color.White);
gPaddle.Draw(spriteBatch);
ball.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
Don't know what's wrong, this have never happened to me.
Because you initialized spritebatch...
spriteBatch = new SpriteBatch(GraphicsDevice);
...It shouldn't be null unless one of your other classes is changing it.
Things you can try:
-Put a breakpoint on it in load content, I don't know why it wouldn't be called, but just check in case, make sure that LoadContent() is being called.
-Rebuild you project and make sure your changes are being saved.
...As I was writing this answer and testing the code on my machine I finally found the error. I'll leave the advice above in case anyone else has one of those issues.
You are not calling base.Initialize in your Initialize() method. This method calls the internal XNA stuff, which results in your LoadContent() being called.
It is also a good idea to call base.LoadContent in the LoadContent() method, you should always call the base method on any overridden method.

Categories