|
|
#1 | ||||||||
|
Junior Member
|
Using XNA, ive made a background and other stuff that moves when pressing the right of left button. This means that it gives the illusion of the hero moving left because everything else is moving right. and vice versa. But, ive made it so that the backgrounds disappear when off-screen, and appear further on, so you get a series of backgrounds. However, Due to this whenever you move left, the backgrounds end, and there is just the standerd blue screen. Im going to make just one picture for the whole thing soon, and the same thing would happen for that too. How would i make it so that the hero cant move so that the blue bit appears? Heres my code:
Game1.cs Code:
using 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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Ball_Adventures
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//AI Units
AI mAI;
AI mBackground;
AI mBackground2;
//Hero
Hero mHero;
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()
{
//Change the resolution to 800x600
graphics.PreferredBackBufferWidth = 1200;
graphics.PreferredBackBufferHeight = 900;
graphics.ApplyChanges();
// TODO: Add your initialization logic here
mAI = new AI();
mBackground = new AI();
mBackground.Scale = 1.5f;
mBackground2 = new AI();
mBackground2.Scale = 1.5f;
mHero = new Hero();
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
//Hero
mHero.LoadContent(this.Content, "Hero2");
mHero.Position = new Vector2(350, 250);
//Background
mBackground.LoadContent(this.Content, "Background1");
mBackground2.LoadContent(this.Content, "Background1");
mBackground2.Position = new Vector2(mBackground.Position.X + mBackground.Size.Width, 0);
}
/// <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();
//Background Movement
if (mBackground.Position.X < -mBackground.Size.Width)
{
mBackground.Position.X = mBackground2.Position.X + mBackground2.Size.Width;
}
if (mBackground2.Position.X < -mBackground2.Size.Width)
{
mBackground2.Position.X = mBackground.Position.X + mBackground.Size.Width;
}
// TODO: Add your update logic here
mHero.Update(gameTime);
mBackground.Update(gameTime);
mBackground2.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);
// TODO: Add your drawing code here
spriteBatch.Begin();
mBackground.Draw(this.spriteBatch);
mBackground2.Draw(this.spriteBatch);
mHero.Draw(this.spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace Ball_Adventures
{
class AI : Sprite
{
const int WIZARD_SPEED = 160;
const int MOVE_LEFT = -1;
const int MOVE_RIGHT = 1;
enum State
{
Walking
}
State mCurrentState = State.Walking;
Vector2 mDirection = Vector2.Zero;
Vector2 mSpeed = Vector2.Zero;
KeyboardState mPreviousKeyboardState;
public void Update(GameTime theGameTime)
{
KeyboardState aCurrentKeyboardState = Keyboard.GetState();
UpdateMovement(aCurrentKeyboardState);
mPreviousKeyboardState = aCurrentKeyboardState;
base.Update(theGameTime, mSpeed, mDirection);
}
private void UpdateMovement(KeyboardState aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
mSpeed = Vector2.Zero;
mDirection = Vector2.Zero;
if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
mSpeed.X = WIZARD_SPEED;
mDirection.X = MOVE_LEFT;
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
mSpeed.X = WIZARD_SPEED;
mDirection.X = MOVE_RIGHT;
}
}
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace Ball_Adventures
{
class Hero : Sprite
{
//mStartingPosition
Vector2 mStartingPosition = Vector2.Zero;
//Assets
const string WIZARD_ASSETNAME = "WizardSquare";
const int START_POSITION_X = 125;
const int START_POSITION_Y = 245;
const int WIZARD_SPEED = 160;
const int MOVE_UP = -1;
const int MOVE_DOWN = 1;
const int MOVE_LEFT = -1;
const int MOVE_RIGHT = 1;
enum State
{
Walking,
Jumping
}
State mCurrentState = State.Walking;
Vector2 mDirection = Vector2.Zero;
Vector2 mSpeed = Vector2.Zero;
KeyboardState mPreviousKeyboardState;
public void Update(GameTime theGameTime)
{
KeyboardState aCurrentKeyboardState = Keyboard.GetState();
UpdateJump(aCurrentKeyboardState);
mPreviousKeyboardState = aCurrentKeyboardState;
base.Update(theGameTime, mSpeed, mDirection);
}
private void UpdateJump(KeyboardState aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
if (aCurrentKeyboardState.IsKeyDown(Keys.Space) == true && mPreviousKeyboardState.IsKeyDown(Keys.Space) == false)
{
Jump();
}
}
if (mCurrentState == State.Jumping)
{
if (mStartingPosition.Y - Position.Y > 150)
{
mDirection.Y = MOVE_DOWN;
}
if (Position.Y > mStartingPosition.Y)
{
Position.Y = mStartingPosition.Y;
mCurrentState = State.Walking;
mDirection = Vector2.Zero;
}
}
}
private void Jump()
{
if (mCurrentState != State.Jumping)
{
mCurrentState = State.Jumping;
mStartingPosition = Position;
mDirection.Y = MOVE_UP;
mSpeed = new Vector2(WIZARD_SPEED, WIZARD_SPEED);
}
}
}
}
__________________
If Oathbreaker == Clueless { Post on Forum } (this will happen a lot) Wanting to learn C++ or whatever suits me. |
||||||||
|
|
|
![]() |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 05:29 AM.















Linear Mode

