This code should work, but it doesn't and I cannot see why. It pops up with about 4 invalid token errors where it says "spriteBatch.Draw(tex, position, Color.White)" in the Paddle class.
Main 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;
namespace XNA_Pong
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texBall;
Paddle P1 = new Paddle();
Paddle P2 = new Paddle();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = false;
graphics.PreferredBackBufferHeight=500;
graphics.PreferredBackBufferWidth = 500;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
P1.tex = Content.Load<texture2d>("Player1");
P2.tex = Content.Load<texture2d>("Player2");
texBall = Content.Load<texture2d>("Pong Ball");
P1.position.X = 10;
P1.position.Y = graphics.GraphicsDevice.Viewport.Height / 2 - (P1.height / 2);
P2.position.X = graphics.GraphicsDevice.Viewport.Width - 20 - (P2.width);
P2.position.Y = graphics.GraphicsDevice.Viewport.Height / 2 - (P2.height / 2);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(texBall, new Vector2(250f, 250f), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Paddle Class (NOT WORKING)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 XNA_Pong{
class Paddle : Microsoft.Xna.Framework.Game
{
public Texture2D tex;
public Vector2 position;
public PlayerIndex Pnumber;
public int width, height, speed;
public Paddle()
{
tex = null;
position = Vector2.Zero;
Pnumber= PlayerIndex.One;
width = 10;
height = 50;
speed = 20;
}
public void Update()
{
}
public void Draw(SpriteBatch spriteBatch);
spriteBatch.Draw(tex, position, Color.White);
}
}
Please help ASAP.
Thanks