Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

The "Bounce" does not exist in current context

Started by Thomas Wiborg Jan 22, 2013 at 7:59 PM 5 replies 1.7k views
Original Post
Thomas Wiborg
Thomas Wiborg

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"?

//Thomas Wiborg
BrianJensen
BrianJensen

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?

Thomas Wiborg
Thomas Wiborg
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
//Thomas Wiborg
Thomas Wiborg
Thomas Wiborg

Yeah, I tried it :S

//Thomas Wiborg
Endurion
Endurion

Unless your main class (Game1?) is not derived from GameMechanics you can't call Bounce like that.

You need a GameMechanics instance somewhere. C# doesn't have class-less functions.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
pulpfist
pulpfist

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]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.