The "Bounce" does not exist in current context

Started by
5 comments, last by pulpfist 11 years, 3 months ago

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

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?

Sprite Creator 3 VX & XP

WARNING: I edit my posts constantly.

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

If you tried it and it didn't work then I don't know. If you didn't try it then you should.

Sprite Creator 3 VX & XP

WARNING: I edit my posts constantly.

Yeah, I tried it :S

//Thomas Wiborg

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>

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]

This topic is closed to new replies.

Advertisement