如何停止鼠标光标随相机移动

时间:2019-01-02 17:57:52

标签: c# monogame

当我用鼠标单击屏幕时,我的精灵会移动到这个位置,但是当相机移动时,鼠标光标会跟着她移动,而精灵会移动到错误的位置。 如何在相机移动鼠标时将其固定在正确的位置? 我试图相应地更改鼠标的位置并更改相机的位置,但是我似乎还是无法执行

谢谢

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using XELibrary;
using Comora;


namespace gameTest
{

public class PlayingState
{
    public SpriteBatch SpriteBatch { get; private set; }
    public Vector2 playerPosition = new Vector2(100, 100);
    private Vector2 velocity = new Vector2(200.0f, 0.0f);
    private DirectionType direction = DirectionType.Right;
    public Rectangle spriteRectangle;
    private int health;
    private int radius;
    private string playerKey;


    MouseState mState;
    MouseState oldmState;
    Vector2 current_mouse_pos = new Vector2(0, 0);
    Vector2 cursor_mouse_pos;
    private Camera camera;

    // BACKGROUND
    Texture2D backgroundtexture;
    Vector2 backgroundPosition;

    Texture2D mouse_Sprite;

    SpriteFont gameFont;

    IScrollingBackgroundManager scrollingBackgroundManager;
    ICelAnimationManager celAnimationManager;
    ISoundManager soundManager;

    // ---------- GETTERS AND SETTERS ----------
    public float HealthTimer { get { return healthTimer; } set { 
    healthTimer = value; } }
    public int Radius { get { return radius; } }
    public int Health { get { return health; } set { health = value; } }
    public Vector2 PlayerPosition { get { return playerPosition; } set { 
    playerPosition = value; } }
    public void SetX(float newX) { playerPosition.X = newX; }
    public void SetY(float newY) { playerPosition.Y = newY; }

    // ---------- C O N S T R U C T O R ----------
    public PlayingState(Game game) : base(game)
    {
        game.Services.AddService(typeof(IPlayingState), this);

        scrollingBackgroundManager = (IScrollingBackgroundManager)game.Services.GetService(typeof(IScrollingBackgroundManager));
        celAnimationManager = (ICelAnimationManager)game.Services.GetService(typeof(ICelAnimationManager));
        soundManager = (ISoundManager)game.Services.GetService(typeof(ISoundManager));

        playerKey = "playerPauseDown";
    }

    // ---------- I N I T I A L I Z E ----------
    public override void Initialize()
    {
        this.camera = new Camera(this.GraphicsDevice);
        base.Initialize();
    }

    // ---------- L O A D - C O N T E N T ----------
    protected override void LoadContent()
    {
        **here i load my animation sprites** 
    }

    // ---------- U P D A T E ----------
    public override void Update(GameTime gameTime)
    {
        KeyboardState kState = Keyboard.GetState();
        float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

        if (healthTimer > 0) { HealthTimer -= dt; }

        foreach (Enemy en in Enemy.enemies)
        {
            en.Update(gameTime, PlayerPosition);
        }
        Enemy.enemies.RemoveAll(e => e.Health <= 0);

        // handleInput(gameTime);
        handleInputMouse(gameTime);
        playerWalkToMouse();

        this.camera.Update(gameTime);
        this.camera.Position = playerPosition;



        base.Update(gameTime);
    }

    // ---------- M O U S E ----------
    private void handleInputMouse(GameTime gameTime)
    {
        /*
        MouseState mStateHandle = Mouse.GetState(); ;
        if (mStateHandle.LeftButton == ButtonState.Pressed)
            current_mouse_pos = new Vector2(mStateHandle.X, mStateHandle.Y);

        if (current_mouse_pos.X > PlayerPosition.X)
            playerKey = "playerWalkRight";
        else if (current_mouse_pos.X < PlayerPosition.X)
            playerKey = "playerWalkLeft";
        else if (current_mouse_pos.Y < PlayerPosition.Y)
            playerKey = "playerWalkUp";
        else if (current_mouse_pos.Y > PlayerPosition.Y)
            playerKey = "playerWalkDown";
        else
            playerKey = "playerPauseDown";
        */


        if (mState.LeftButton == ButtonState.Pressed)
            current_mouse_pos = new Vector2(mState.X, mState.Y);

        if (current_mouse_pos.X > PlayerPosition.X)
            playerKey = "playerWalkRight";
        else if (current_mouse_pos.X < PlayerPosition.X)
            playerKey = "playerWalkLeft";
        else if (current_mouse_pos.Y < PlayerPosition.Y)
            playerKey = "playerWalkUp";
        else if (current_mouse_pos.Y > PlayerPosition.Y)
            playerKey = "playerWalkDown";
        else
            playerKey = "playerPauseDown";
    }


    // ---------- D R A W ----------
    public override void Draw(GameTime gameTime)
    {
        SpriteBatch.Begin(this.camera);


        SpriteBatch.Draw(backgroundtexture, backgroundPosition, Color.White);

        celAnimationManager.Draw(gameTime, playerKey, OurGame.SpriteBatch, PlayerPosition, SpriteEffects.None);

        Vector2 cursorPos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
        SpriteBatch.Draw(mouse_Sprite, cursorPos, Color.White);

        SpriteBatch.End();
        base.Draw(gameTime);

    }

1 个答案:

答案 0 :(得分:1)

从GetState()获得的鼠标位置始终取决于渲染游戏的Windows。它永远不会与您的相机有关。

如果我正确理解了您的逻辑,只需将相机的x和y位置减去您从GetState()获取的鼠标位置即可。

通过这种方式,您可以将“世界”位置链接到摄像机位置。