Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 5-Feb-13 5:15am
v2
Comments
ZurdoDev 5-Feb-13 11:16am    
What doesn't work? Errors? Examples?
Member 9813019 5-Feb-13 11:18am    
It pops up with:
Invalid token '(' in class, struct, or interface member declaration
Invalid token ',' in class, struct, or interface member declaration
Invalid token ',' in class, struct, or interface member declaration
Invalid token ')' in class, struct, or interface member declaration
Sergey Alexandrovich Kryukov 5-Feb-13 12:17pm    
Incorrect question. "Doesn't work" is a notion which may make some sense only if it is defined what "it" should do. Alsways explain it when asking questions.
—SA

1 solution

The syntax error you are receiving is because of the semi-colon at the end of your function declaration which causes the spriteBatch variable on the next line to be out of scope.
public void Draw(SpriteBatch spriteBatch); <= This colon.
spriteBatch.Draw(text, position, Color.White); <= Out of scope because of the colon on the previous line ends the function scope.

Change the code to this and things will work.
C#
public void Draw(SpriteBatch spriteBatch)
{
   spriteBatch.Draw(tex, position, Color.White);
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Feb-13 12:21pm    
If cannot fix anything. This is just an extra ';', perfectly allowed by syntax, called "empty statement". You can easily check that it won't even cause warning. I don't know why OP accepted it.
(I did not vote this time.)

[EDIT:]
Voted 5, but will you please improve it a bit?

—SA
fjdiewornncalwe 5-Feb-13 12:55pm    
It would cause a compile exception because the spriteBatch variable is defined in the method signature and will attempt to execute out of scope on the next line. :)
Sergey Alexandrovich Kryukov 5-Feb-13 13:27pm    
Why? The code with this colon is strictly equivalent to the code without. Look, it's too easy to check up.
—SA
fjdiewornncalwe 5-Feb-13 14:23pm    
In my fix, yes. But the OP's original code didn't have any braces there.
Sergey Alexandrovich Kryukov 5-Feb-13 14:30pm    
I see. Than your code is just not quite clear...
Just say: "replace ';' with '{'" or just show to fragments of code: "your code: ...", "should be: ...".
Honestly, right now it's misleading. Even though I misread part of it, but I would understand it correctly, if you expressed it more accurately.

Will you improve it? Now I'm voting my 5, but in advance... :-)

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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