简体   繁体   English

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

[英]How to stop the mouse cursor from moving with the camera

when i click the screen with mouse my sprite move to this position but when camera move the mouse cursor move with her and the sprite go to wrong position. 当我用鼠标单击屏幕时,我的精灵移动到该位置,但是当摄像机移动时,鼠标光标随她移动,而精灵移动到错误的位置。 how i fix that when my camera move my mouse stay in right position? 如何在相机移动鼠标时将其固定在正确的位置? I tried to change the position of the mouse accordingly and change the location of the camera but I still can not seem to perform it 我尝试相应地更改鼠标的位置并更改摄像头的位置,但是我似乎还是无法执行

thank's 谢谢

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);

    }

The mouse position obtained from the GetState() is always according to the Windows in which you game is rendered. 从GetState()获得的鼠标位置始终取决于渲染游戏的Windows。 It is never relative to your camera. 它永远不会与您的相机有关。

If I understand your logic correctly, you simply need to substract the x and y position of the camera to the mouse position your obtain from the GetState(). 如果我正确理解了您的逻辑,则只需要将相机的x和y位置减去从GetState()获取的鼠标位置。

This way, you will link your "world" position to the camera position. 这样,您可以将“世界”位置链接到摄像机位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM