How to use Window.Clientbouds inside a class in XNA

Asked

Viewed 93 times

1

I’m having a hard time delimiting the character’s movement to the edge of the screen. In class Game1 which is my main I can use Window.ClientBounds, but when I use this function within the class Player1 makes the mistake:

'The name 'Window' does not exist in the Current context'`.

Code snippet of class Player1:

public void Update(GameTime gameTime)
{
    position += velocity;

    if (Keyboard.GetState().IsKeyDown(Keys.Up))
    {
        velocity.Y = -3f;
        if (position.Y + texture.Height > (Game as Game1).Window.ClientBounds.Height)
            velocity.Y += 3f;
    }
    ...
}

When I use (Game as Game1) can use Window.ClientBounds, but he makes a mistake that says:

'Microsoft.Xna.Framework.Game' is a 'type' but is used like a 'variable'

  • 1

    And present code? Or something that you can analyze...

  • Ajuda ae Cesarmiguel!

  • I’m not really in those classes, but it seems to me that the mistake 'The name 'Window' does not exist in the current context' means that the Window you are using is not part of your context. Check if you have

2 answers

3

There is a lack of information for a complete analysis of the situation, but from what it was possible to understand the question, the method Update shown belongs to the class Player1.

In turn, this class Player1 does not have access to class members Game1.

Thus, it would be enough to pass a class instance Game1 for Player1, during its creation, so that Player1 can access everything related to Game1.

For example:

The class Player1 would look about like this:

public class Player1 ... {
    private Game1 myGame;

    //não sei como está o construtor da sua classe Player1, mas agora ele deve possuir
    //um parâmetro a mais: game
    public Player1 (Game1 game) {
        myGame = game;
        ...
    }

    ...

    public void Update(...) {
        ...
        //agora você pode acessar os membros da classe Game1, a partir de Player1
        if (position.Y + texture.Height > myGame.Window.ClientBounds.Height)
        ...
    }
}

And in class Game1, where the Player1 is instantiated, you must do something like this:

...
Player1 p = new Player1(this);
...

1

That Method is in the game class for use-lo or its class You will have to inherit the game class so that you can overload or receive Window.Clientbounds as parameter in your method here I implemented in a class that I already had it is inherited from my Sprite class that Inherits The game class.

public class SpriteAnimated : Sprite
{
    Point   FrameSize;
    Point   CurrentFrame;
    Point   SheetSize;
    Vector2 Position;

    int TimeSizeLastFrame = 0;

    private Texture2D SpriteSheet;        

    public SpriteAnimated(Texture2D Textura, Point Size)
    {          
        SpriteSheet = Textura;
        SheetSize = Size;

        int FlameX = Convert.ToInt16(Textura.Width / Size.X);
        int FlameY = Convert.ToInt16(Textura.Height / Size.Y);

        FrameSize.X = FlameX;
        FrameSize.Y = FlameY;

        Position = new Vector2(
                                 (Window.ClientBounds.Width - FrameSize.X) / 2,
                                 (Window.ClientBounds.Height - FrameSize.Y) / 2
                              );

    }

    public void Update(GameTime gameTime, KeyboardState KeyPress, GameWindow Window)
    {
        TimeSizeLastFrame += gameTime.ElapsedGameTime.Milliseconds;

        if (Position.Y <= Window.ClientBounds.Height)
        { 
            // Aqui Pode Ir seu codigo
        }

        if (KeyPress.IsKeyDown(Keys.Up))
        {
            Position.Y -= 5;
            SpriteSheet = null;
        }
        else if (KeyPress.IsKeyDown(Keys.Down))
        {
            Position.Y += 5;
        }
        else if (KeyPress.IsKeyDown(Keys.Left))
        {
            Position.X -= 5;
        }
        else if (KeyPress.IsKeyDown(Keys.Right))
        {
            Position.X += 5;
        }

        if (TimeSizeLastFrame > 60)
        {
            TimeSizeLastFrame -= 60;
            CurrentFrame.X++;

            if (CurrentFrame.X >= SheetSize.X)
            {
                CurrentFrame.X = 0;
                CurrentFrame.Y++;

                if (CurrentFrame.Y >= SheetSize.Y)
                    CurrentFrame.Y = 0;
            }
        }
    }

    public void Draw(GameTime gametime, SpriteBatch spriteBacth)
    {           
        spriteBacth.Draw(  SpriteSheet, 
                           Position, 
                           new Rectangle(CurrentFrame.X * FrameSize.X, CurrentFrame.Y * FrameSize.Y, FrameSize.X, FrameSize.Y), 
                           Color.White, 
                           0, 
                           Vector2.Zero, 
                           1, 
                           SpriteEffects.None, 
                           0
                        );
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.