Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的绳子没有画出来?_C#_Button_Menu_Xna_Draw - Fatal编程技术网

C# 为什么我的绳子没有画出来?

C# 为什么我的绳子没有画出来?,c#,button,menu,xna,draw,C#,Button,Menu,Xna,Draw,当你点击最上面的按钮时,它会在屏幕上画一个字符串,但是它不会出现。我移动了mainMenu.UpdateButtons();到Main.cs中的Draw方法,但会绘制字符串,然后再次绘制背景图像。使其在字符串出现的瞬间出现并消失。为什么要这样做 Main.cs using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.F

当你点击最上面的按钮时,它会在屏幕上画一个字符串,但是它不会出现。我移动了mainMenu.UpdateButtons();到Main.cs中的Draw方法,但会绘制字符串,然后再次绘制背景图像。使其在字符串出现的瞬间出现并消失。为什么要这样做

Main.cs

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;
using TestGame.Controls;
using TestGame.GameStates;

namespace TestGame
{
public class Main : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    InputHandler inputHandler;
    public SpriteBatch spriteBatch;
    public SpriteFont spriteFont;
    MainMenu mainMenu;
    Vector2 position;

    public Main()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        inputHandler = new InputHandler();
        mainMenu = new MainMenu(this);

        graphics.PreferredBackBufferWidth = 1280;
        graphics.PreferredBackBufferHeight = 720;
    }

    protected override void Initialize()
    {
        this.IsMouseVisible = true;

        base.Initialize();
        mainMenu.MenuInitialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        spriteFont = Content.Load<SpriteFont>(@"Fonts\MainFont");

        mainMenu.MenuLoadContent();
    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        inputHandler.Update();

        if (inputHandler.currentKeyState.IsKeyDown(Keys.Escape))
            this.Exit();
        mainMenu.frameTime = gameTime.ElapsedGameTime.Milliseconds / 1000;
        MouseState mouseState = Mouse.GetState();
        mainMenu.mouseX = mouseState.X;
        mainMenu.mouseY = mouseState.Y;
        mainMenu.previouslyPressed = mainMenu.mousePressed;
        mainMenu.mousePressed = mouseState.LeftButton == ButtonState.Pressed;
        mainMenu.UpdateButtons();
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);
        spriteBatch.Begin();
        mainMenu.MenuDraw();
        spriteBatch.End();
        base.Draw(gameTime);
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
使用TestGame.Controls;
使用TestGame.GameStates;
命名空间测试游戏
{
公共类Main:Microsoft.Xna.Framework.Game
{
图形管理器图形;
InputHandler-InputHandler;
公共SpriteBatch SpriteBatch;
公共精神;
主菜单主菜单;
矢量2位置;
公用干管()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
inputHandler=新的inputHandler();
主菜单=新的主菜单(本);
graphics.PreferredBackBufferWidth=1280;
graphics.PreferredBackBufferHeight=720;
}
受保护的覆盖无效初始化()
{
this.IsMouseVisible=true;
base.Initialize();
main menu.MenuInitialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
spriteFont=Content.Load(@“font\MainFont”);
main menu.MenuLoadContent();
}
受保护的覆盖无效UnloadContent()
{
}
受保护覆盖无效更新(游戏时间游戏时间)
{
inputHandler.Update();
if(inputHandler.currentKeyState.IsKeyDown(Keys.Escape))
这是Exit();
main menu.frameTime=gameTime.ElapsedGameTime.millides/1000;
MouseState=Mouse.GetState();
main menu.mouseX=mouseState.X;
main menu.mouseY=mouseState.Y;
main menu.previously pressed=main menu.mouse pressed;
mainMenu.mousePressed=mouseState.LeftButton==ButtonState.Pressed;
mainMenu.UpdateButtons();
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色。黑色);
spriteBatch.Begin();
main menu.MenuDraw();
spriteBatch.End();
基础。抽签(游戏时间);
}
}
}
MainMenu.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TestGame.GameStates
{
public class MainMenu
{
    enum buttonState { hover, up, released, down }
    const int numberOfButtons = 4, newGameButtonIndex = 0, loadGameButtonIndex = 1, optionsButtonIndex = 2, quitButtonIndex = 3, buttonHeight = 48, buttonWidth = 80;
    Color[] buttonColor = new Color[numberOfButtons];
    Rectangle[] buttonRect = new Rectangle[numberOfButtons];
    buttonState[] buttonSt = new buttonState[numberOfButtons];
    Texture2D[] buttonTexture = new Texture2D[numberOfButtons];
    double[] buttonTimer = new double[numberOfButtons];
    public bool mousePressed, previouslyPressed = false;
    public int mouseX, mouseY;
    public double frameTime;
    int buttonPadding;

    Main main;
    Texture2D backgroundImage;
    Texture2D backgroundImageFade;

    public MainMenu(Game game)
    {
        main = (Main)game;
    }

    public void MenuInitialize()
    {
        for (int i = 0; i < numberOfButtons; i++)
        {
            buttonSt[i] = buttonState.up;
            buttonColor[i] = Color.White;
            buttonTimer[i] = 0.0;
            buttonRect[i] = new Rectangle(0, buttonPadding, buttonWidth, buttonHeight);
            buttonPadding += buttonHeight;
        }
    }

    public void MenuLoadContent()
    {
        backgroundImage = main.Content.Load<Texture2D>(@"Backgrounds\titlescreen");
        backgroundImageFade = main.Content.Load<Texture2D>(@"Backgrounds\titlescreenfade");
        buttonTexture[newGameButtonIndex] = main.Content.Load<Texture2D>(@"Sprites\desktop");
        buttonTexture[loadGameButtonIndex] = main.Content.Load<Texture2D>(@"Sprites\desktop");
        buttonTexture[optionsButtonIndex] = main.Content.Load<Texture2D>(@"Sprites\desktop");
        buttonTexture[quitButtonIndex] = main.Content.Load<Texture2D>(@"Sprites\desktop");
    }

    public void MenuDraw()
    {
        main.spriteBatch.Draw(backgroundImage, new Vector2(0, 0), Color.White);

        for (int i = 0; i < numberOfButtons; i++)
        {
            main.spriteBatch.Draw(buttonTexture[i], buttonRect[i], buttonColor[i]);
        }
    }

    Boolean targetImageAlpha(Rectangle rect, Texture2D texture, int x, int y)
    {
        return targetImageAlpha(0, 0, texture, texture.Width * (x - rect.X) / rect.Width, texture.Height * (y - rect.Y) / rect.Height);
    }

    Boolean targetImageAlpha(float tx, float ty, Texture2D texture, int x, int y)
    {
        if (targetImage(tx, ty, texture, x, y))
        {
            uint[] data = new uint[texture.Width * texture.Height];
            texture.GetData<uint>(data);

            if ((x - (int)tx) + (y - (int)ty) * texture.Width < texture.Width * texture.Height)
            {
                return ((data[(x - (int)tx) + (y - (int)ty) * texture.Width] & 0xFF000000) >> 24) > 20;
            }
        }
        return false;
    }

    Boolean targetImage(float tx, float ty, Texture2D texture, int x, int y)
    {
        return (x >= tx && x <= tx + texture.Width && y >= ty && y <= ty + texture.Height);
    }

    public void UpdateButtons()
    {
        for (int i = 0; i < numberOfButtons; i++)
        {
            if (targetImageAlpha(buttonRect[i], buttonTexture[i], mouseX, mouseY))
            {
                buttonTimer[i] = 0.0;
                if (mousePressed)
                {
                    buttonSt[i] = buttonState.down;
                    buttonColor[i] = Color.Blue;
                }
                else if (!mousePressed && previouslyPressed)
                {
                    if (buttonSt[i] == buttonState.down)
                    {
                        buttonSt[i] = buttonState.released;
                    }
                }
                else
                {
                    buttonSt[i] = buttonState.hover;
                    buttonColor[i] = Color.LightBlue;
                }
            }
            else
            {
                buttonSt[i] = buttonState.up;

                if (buttonTimer[i] > 0)
                {
                    buttonTimer[i] = buttonTimer[i] - frameTime;
                }
                else
                {
                    buttonColor[i] = Color.White;
                }
            }

            if (buttonSt[i] == buttonState.released)
            {
                onButtonClick(i);
            }
        }
    }

    void onButtonClick(int i)
    {
        switch (i)
        {
            case newGameButtonIndex:
                main.spriteBatch.Begin();
                //main.spriteBatch.DrawString(main.spriteFont, "Creating new game", new Vector2(100, 200), Color.White);
                main.spriteBatch.Draw(backgroundImageFade, new Vector2(0, 0), Color.White);
                main.spriteBatch.End();
                break;
            default:
                break;
        }

    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Graphics;
命名空间TestGame.GameStates
{
公共类主菜单
{
枚举按钮状态{悬停,向上,释放,向下}
const int numberOfButtons=4,newGameButtonIndex=0,loadGameButtonIndex=1,optionbuttonindex=2,quitButtonIndex=3,buttonHeight=48,buttonWidth=80;
颜色[]按钮颜色=新颜色[numberOfButtons];
矩形[]按钮rect=新矩形[numberOfButtons];
buttonState[]buttonSt=新的buttonState[numberOfButtons];
Texture2D[]buttonTexture=新的Texture2D[numberOfButtons];
double[]ButtonTime=新的double[numberOfButtons];
public bool mouse pressed,previously pressed=false;
公共int mouseX,mouseY;
公共双帧时间;
int按钮添加;
主要的;
纹理2D背景图像;
纹理2D背景图像淡入淡出;
公共主菜单(游戏)
{
主要=(主要)游戏;
}
public void菜单初始化()
{
对于(int i=0;i>24)>20;
}
}
返回false;
}
布尔目标图像(浮点tx、浮点ty、纹理2D纹理、整数x、整数y)
{
返回(x>=tx&&x=ty&&y 0)
{
buttonTimer[i]=buttonTimer[i]-帧时间;
}
其他的
{
按钮颜色[i]=颜色。白色;
}
}
如果(按钮状态[i]==按钮状态已释放)
{
onButtonClick(i);
}
}
}
空o