Click here to Skip to main content
15,867,756 members

Why doesn't this code work?

Member 9813019 asked:

Open original thread
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
C#
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
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    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;
        }

        /// <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.
            //Assigning Paddle Textures
            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);
            // TODO: use this.Content to load your game content here
        }

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

            // 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(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
                      spriteBatch.Draw(texBall, new Vector2(250f, 250f), Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

Paddle Class (NOT WORKING)
C#
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
Tags: C#, XNA4.0, Drawing

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900