簡體   English   中英

鼠標單擊后保留XNA c#Sprite

[英]XNA c# Sprite remain after mouseclick

我有個問題。 有沒有一種方法可以使鼠標單擊后保留精靈,而不創建精靈列表? 我的意思是,我的代碼在屏幕上隨機生成精靈,如果我點擊紅色精靈,它就會消失。 單擊后如何使紅色精靈停留在屏幕上? 這是我的代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Ghost
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D ghostPic;
        Texture2D background;
        Random rand;
        int ghostColour;
        Rectangle ghostLocation;
        float timeRemaining;
        float showTime;
        int score;
        MouseState mouseState, mouseStatePrevious;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {

            graphics.PreferredBackBufferHeight = 550;
            graphics.PreferredBackBufferWidth = 750;
            this.IsMouseVisible = true;
            this.Window.Title = "Ghost Hunt | Score: 0";
            this.Window.AllowUserResizing = true;
            showTime = 0.5f;
            rand = new Random();
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            ghostPic = Content.Load<Texture2D>("GhostPic");
            background = Content.Load<Texture2D>("Background");
            // TODO: use this.Content to load your game content here
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        protected override void Update(GameTime gameTime)
        { 
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
                this.Exit();

            mouseState = Mouse.GetState();

            if (mouseState.LeftButton == ButtonState.Pressed && mouseStatePrevious.LeftButton == ButtonState.Released && ghostLocation.Contains(mouseState.X, mouseState.Y))
            {
                if (ghostColour == 0) score++;
                if (ghostColour == 1)
                {
                    score--;

                }
                this.Window.Title = "Ghost Hunt | Score: " + score.ToString();
                timeRemaining = 0.0f;
            }
            else
            {
                timeRemaining = MathHelper.Max(0, timeRemaining - (float)gameTime.ElapsedGameTime.TotalSeconds);
            }
            mouseStatePrevious = mouseState;
            if (timeRemaining == 0.0f)
            {
                ghostColour = rand.Next(2);
                ghostLocation = new Rectangle(rand.Next(GraphicsDevice.Viewport.Bounds.Width - ghostPic.Width), rand.Next(GraphicsDevice.Viewport.Bounds.Height - ghostPic.Height), ghostPic.Width / 2, ghostPic.Height / 2);
                timeRemaining = showTime;
            }

            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(background, new Rectangle(0,0,GraphicsDevice.Viewport.Bounds.Width,GraphicsDevice.Viewport.Bounds.Height), Color.White);
            switch (ghostColour)
            {
                case 0:
                    spriteBatch.Draw(ghostPic, ghostLocation, Color.White * 0.7f);
                    break;
                case 1:
                    spriteBatch.Draw(ghostPic, ghostLocation, new Color(255,0,0,50));
                    break;
            } 
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

我想您可以制作一個像Sprite []這樣的數組而不是一個List,但是重繪仍會清除屏幕並完全重繪它,從而清除所有未存儲的可繪制對象。

有趣的是制作一個LinkedList而不是在其中保存最后的10個精靈,當達到10個時將第一個扔掉。 這將使您能夠繪制跟隨當前精靈圖形位置的精靈圖形。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM