Sign in to follow this  
Thomas Wiborg

The "Bounce" does not exist in current context

Recommended Posts

I currently have following code under my "Game1.cs"
 

 

 protected override void Update(GameTime gameTime)
        {
            
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            Bounce();                                                                        
                      
            base.Update(gameTime);
        }

 

and my "Bounce" method under "GameMechanics.cs"

 

namespace Cannon4
{
    class GameMechanics
    {
        const int SCREEN_WIDTH = 640;
        const int SCREEN_HEIGHT = 480;

        GameItem target;
   
        public void Bounce()
        {
            //Bouncer target vekk fra nordre og søndre vegg
            if (target.Position.Y <= 0)
            {
                Vector2 vecVar;
                Vector2 posVar;
                vecVar = target.Velocity;
                vecVar.Y *= -1;
                target.Velocity = vecVar;

                posVar = target.Position;
                posVar.Y = 0;
                target.Position = posVar;
            }
            else if (target.Position.Y >= SCREEN_HEIGHT)
            {
                Vector2 vecVar2;
                Vector2 posVar2;
                vecVar2 = target.Velocity;
                vecVar2.Y *= -1;
                target.Velocity = vecVar2;

                posVar2 = target.Position;
                posVar2.Y = SCREEN_HEIGHT - target.Origin.Y;
                target.Position = posVar2;
            }
        }
    }
}

 

Why do I get the error "The "Bounce" does not exist in current context"?

Share this post


Link to post
Share on other sites

At first glance I would say you are incorrectly calling Bounce(). It looks like you're trying to call Bounce() as if it were part of the main program, but Bounce() is inside the class GameMechanics and it is not part of the main class.

 

GameMechanics.Bounce()

 

maybe?

Edited by 0Circle0

Share this post


Link to post
Share on other sites
At first glance I would say you are incorrectly calling Bounce(). It looks like you're trying to call Bounce() as if it were part of the main program, but Bounce() is inside the class GameMechanics and it is not part of the main class.

 

GameMechanics.Bounce()

 

maybe?


no thats not the problem. Cause ive fixed it with removing my folder "Classes" which it lies under... So now its a part on main class.

 

 

namespace Cannon4

it WAS
 

namespace Cannon4.Classes

Share this post


Link to post
Share on other sites

It seems to me you are trying to separate the games event handling from the games content, but you are doing it in a way that convolutes your code in ways you don't want.

Its hard to say though without the complete source.

 

The first thing that strikes me is, why do you have the GameItem inside the GameMechanics class? It doesn't seem right.

 

Another thing that strikes me is that the way the GameMechanics class is now, I would consider making it a static class with static variables/functions.

Then I would put the GameItem somewhere else and pass it as an argument to Bounce.

 

[source]

 

class GameItem
{
}
 
static class GameMechanics
{
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;        
 
    public static void Bounce(GameItem target)
    {
        // ...
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        GameItem item = new GameItem();
        GameMechanics.Bounce(item);
    }
}

[/source]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this