Jump to content
  • Advertisement
Sign in to follow this  
Thomas Wiborg

The "Bounce" does not exist in current context

This topic is 2150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!