简体   繁体   中英

XNA c# Sprite remain after mouseclick

I have a question. Is there a way to make sprite remain after mouseclick on it without making a List of sprites? I mean, my code generates sprite randomly at the screen and if I hit red sprite it dissappear. How to make red sprites stay on the screen after click? Here is my code:

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

I suppose you could make an array like Sprite[] instead of a List but the redraw will still clear your screen and redraw it entirely, clearing any non-stored drawables.

What could be fun is making a LinkedList instead where you keep the last 10 sprites saved, throw away the first when 10 is reached. This would allow you to draw a trail of sprites that follows the position of your current sprite drawing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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