본문 바로가기

[XNA] 키이벤트 받는법, 윈도우 WIDTH, HEIGHT 구하기

1.윈도우 크기 구하기
graphics.GraphicsDevice.Viewport.Width : 윈도우 가로 크기
graphics.GraphicsDevice.Viewport.Height : 윈도우 세로 크기


2.이미지 크기 구하기
Texture2D myTexture
myTexture.Width  : 이미지 가로 크기
myTexture.Height : 이미지 세로 크기



3.키이벤트
 KeyboardState currentState = Keyboard.GetState();

            // Get an array of all currently pressed keys
            Keys[] currentlyPressed = currentState.GetPressedKeys();

            // Loop through all keys that are currently being pressed
            foreach (Keys key in currentlyPressed)
            {
                // If no keys where pressed last frame...
                if (_previousKeysDown.Count == 0)
                {
                    HandleKey(key);
                }
                else
                {
                    // we need to loop through all the previously pressed keys to see if key state has changed
                    for (int i = 0; i < _previousKeysDown.Count; i++)
                    {
                        if (key == _previousKeysDown[i])
                        {
                            // this key was already pressed last frame, lets ignore it
                        }
                        else
                        {
                            HandleKey(key);
                        }
                    }
                }
            }
            _previousKeysDown.Clear();
            foreach (Keys key in currentlyPressed)
                _previousKeysDown.Add(key);


void HandleKey(Keys key)
        {
            // We are only processing each time any of the arrow keys are pressed down.
            // If they were already held down, they are ignored until they are released.
            switch (key)
            {
                case Keys.Left:
                    {
                        this.MoveSpriteLeft();
                        break;
                    }
                case Keys.Right:
                    {
                        this.MoveSpriteRight();
                        break;

                    }
                case Keys.Up:
                    {
                        this.MoveSpriteUp();
                        break;
                    }
                case Keys.Down:
                    {
                        this.MoveSpriteDown();
                        break;
                    }
            }

        }

        void MoveSpriteLeft()
        {
            if (_ballPosition.X > 0)
                _ballPosition.X -= 5;
        }

        void MoveSpriteRight()
        {
            if (_ballPosition.X < graphics.GraphicsDevice.Viewport.Width - _ballTexture.Width)
                _ballPosition.X += 5;
        }

        void MoveSpriteUp()
        {
            if (_ballPosition.Y > 0)
                _ballPosition.Y -= 5;
        }

        void MoveSpriteDown()
        {
            if (_ballPosition.Y < graphics.GraphicsDevice.Viewport.Height - _ballTexture.Height)
                _ballPosition.Y += 5;
        }


대략 이런듯 해보니 되긴 잘되군..