How to use Window.ClientBouds inside a class in XNA

1

I'm having trouble limiting 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 inside class Player1 it gives the error:

  

'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) I can use Window.ClientBounds , but it gives an error that says:

  

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

    
asked by anonymous 20.05.2014 / 16:37

2 answers

3

There is no information for a complete analysis of the situation, but as far as we can see from the question, the Update method is in the Player1 class.

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

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

For example:

The class Player1 would look 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 the Game1 class, where Player1 is instantiated, you should do something like this:

...
Player1 p = new Player1(this);
...
    
20.05.2014 / 19:07
1

This method is in the game class to use it or its class You will have to inherit the game class so you can do an overload or receive Window.ClientBounds as a parameter in your method here Implemented in a class I already had it is inherited from my Sprite class that inherits A class game.

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
                        );
    }
}
    
28.07.2014 / 14:22