Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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# XNA 4.0-Alpha和多纹理。。。搞砸_C#_Xna_Alpha_Texture2d - Fatal编程技术网

C# XNA 4.0-Alpha和多纹理。。。搞砸

C# XNA 4.0-Alpha和多纹理。。。搞砸,c#,xna,alpha,texture2d,C#,Xna,Alpha,Texture2d,我想做一些滑动菜单。。。在一个圆圈中,当用户触摸图标并滑动它们时,图像将向下或向上滑动(如现代手机或赌场机器的水果等) 我有一个透明的圆圈,比如说三个图标。。。我如何以这种方式混合它们 项目: 问题: 我可以用spriteBatch一个接一个地按顺序画出来,但是怎样才能把它们混合在一起呢 对不起,我能感觉到这可能很容易,但我被卡住了 谢谢 你能不能把圆圈放大一点,让它下面什么都看不出来,然后把它放在上面?我做了。。。以下是方法: using System; using System.Coll

我想做一些滑动菜单。。。在一个圆圈中,当用户触摸图标并滑动它们时,图像将向下或向上滑动(如现代手机或赌场机器的水果等)

我有一个透明的圆圈,比如说三个图标。。。我如何以这种方式混合它们

项目:

问题:

我可以用spriteBatch一个接一个地按顺序画出来,但是怎样才能把它们混合在一起呢

对不起,我能感觉到这可能很容易,但我被卡住了


谢谢

你能不能把圆圈放大一点,让它下面什么都看不出来,然后把它放在上面?

我做了。。。以下是方法:

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 StencilTest
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D star, cloud, shape;
        AlphaTestEffect alphaTestEffect;
        DepthStencilState stencilAlways;
        DepthStencilState stencilKeep;
        RenderTarget2D rt;

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

            graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;

            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;

            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        Texture2D grass;

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            star = Content.Load<Texture2D>("star1");
            cloud = Content.Load<Texture2D>("cloud1");
            shape = Content.Load<Texture2D>("shape");
            back = Content.Load<Texture2D>("back");
            grass = Content.Load<Texture2D>("grass1");

            Matrix projection = Matrix.CreateOrthographicOffCenter(0, shape.Width, shape.Height, 0, 0, 1);
            Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);

            alphaTestEffect = new AlphaTestEffect(GraphicsDevice);
            alphaTestEffect.VertexColorEnabled = true;
            alphaTestEffect.DiffuseColor = Color.White.ToVector3();
            alphaTestEffect.AlphaFunction = CompareFunction.Equal;
            alphaTestEffect.ReferenceAlpha = 0;
            alphaTestEffect.World = Matrix.Identity;
            alphaTestEffect.View = Matrix.Identity;
            alphaTestEffect.Projection = halfPixelOffset * projection;

            // set up stencil state to always replace stencil buffer with 1
            stencilAlways = new DepthStencilState();
            stencilAlways.StencilEnable = true;
            stencilAlways.StencilFunction = CompareFunction.Always;
            stencilAlways.StencilPass = StencilOperation.Replace;
            stencilAlways.ReferenceStencil = 1;
            stencilAlways.DepthBufferEnable = false;

            // set up stencil state to pass if the stencil value is 1
            stencilKeep = new DepthStencilState();
            stencilKeep.StencilEnable = true;
            stencilKeep.StencilFunction = CompareFunction.Equal;
            stencilKeep.StencilPass = StencilOperation.Keep;
            stencilKeep.ReferenceStencil = 1;
            stencilKeep.DepthBufferEnable = false;

            rt = new RenderTarget2D(GraphicsDevice, shape.Width, shape.Height,
                                   false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8,
                                   0, RenderTargetUsage.DiscardContents);
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        float angle = 0f;
        private Texture2D back;
        Vector2 pos = new Vector2(400, 300);
        float cloudscale = 0.25f;

        protected override void Draw(GameTime gameTime)
        {

            // set up rendering to the active render target
            GraphicsDevice.SetRenderTarget(rt);

            // clear the render target to opaque black,
            // and initialize the stencil buffer with all zeroes
            GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil,
                                 new Color(0, 0, 0, 1), 0, 0);

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque,
                       null, stencilAlways, null, alphaTestEffect);

            spriteBatch.Draw(shape, Vector2.Zero, null, Color.White, 0f,
                             Vector2.Zero, 1f, SpriteEffects.None, 0f);

            spriteBatch.End();

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
                  null, stencilKeep, null, null);

            //for (int i = 0; i < Math.Ceiling(800 / (cloud.Width * cloudscale)); i++)
            //    for (int j = 0; j < Math.Ceiling(600 / (cloud.Height * cloudscale)); j++)
            //        spriteBatch.Draw(cloud, Vector2.Zero + new Vector2(i * cloud.Width * cloudscale, j * cloud.Height * cloudscale), null, Color.White, 0f,
            //         Vector2.Zero, cloudscale, SpriteEffects.None, 0f);

            spriteBatch.Draw(grass, new Vector2(rt.Width / 2, rt.Height / 2) + new Vector2(0f, -100f), null, Color.White, 0f,
         new Vector2(grass.Width / 2, grass.Height / 2), .85f, SpriteEffects.None, 0f);

            spriteBatch.Draw(cloud, new Vector2(rt.Width/2, rt.Height/2), null, Color.White, 0f,
                     new Vector2(cloud.Width / 2, cloud.Height / 2), 1f, SpriteEffects.None, 0f);

            spriteBatch.Draw(star, new Vector2(rt.Width / 2, rt.Height / 2) + new Vector2(0f, 100f), null, Color.White, 0f,
         new Vector2(star.Width / 2, star.Height / 2), .85f, SpriteEffects.None, 0f);

            spriteBatch.End();

            GraphicsDevice.SetRenderTarget(null);

            spriteBatch.Begin();

            spriteBatch.Draw(back, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);

            spriteBatch.Draw(rt, pos + new Vector2(50f * (float)Math.Cos(MathHelper.ToRadians(angle)), 50f * (float)Math.Sin(MathHelper.ToRadians(angle))), null, Color.White, 0f, new Vector2(rt.Width / 2, rt.Height / 2), 1f, SpriteEffects.None, 0f);

            spriteBatch.End();

            // TODO: Add your drawing code here

            angle +=0.5f;

            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;
名称空间模板测试
{
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
纹理2D星、云、形状;
AlphaTestEffect AlphaTestEffect;
脱模模版;
深度模具状态模具保持;
RenderTarget2D rt;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredDepthStencilFormat=DepthFormat.Depth24Stencil8;
graphics.PreferredBackBufferWidth=800;
graphics.PreferredBackBufferHeight=600;
IsMouseVisible=true;
}
受保护的覆盖无效初始化()
{
base.Initialize();
}
草的纹理;
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
star=内容加载(“star1”);
云=Content.Load(“cloud1”);
形状=内容。荷载(“形状”);
返回=内容。加载(“返回”);
草=内容负荷(“草1”);
矩阵投影=矩阵.createOrthographic偏心(0,形状.宽度,形状.高度,0,0,1);
矩阵半像素偏移=矩阵.CreateTranslation(-0.5f,-0.5f,0);
alphaTestEffect=新的alphaTestEffect(图形设备);
alphaTestEffect.VertexColorEnabled=真;
alphaTestEffect.DiffuseColor=Color.White.ToVector3();
alphaTestEffect.AlphaFunction=比较函数.Equal;
alphaTestEffect.ReferenceAlpha=0;
alphaTestEffect.World=Matrix.Identity;
alphaTestEffect.View=Matrix.Identity;
alphaTestEffect.Projection=半像素偏移*投影;
//设置模具状态以始终使用1替换模具缓冲区
Stencilaways=新的DepthStencilState();
stencilaways.StencilEnable=true;
Stencilaways.StencilFunction=CompareFunction.Always;
Stencilways.StencilPass=StencilOperation.Replace;
Stencilaways.ReferenceStencil=1;
Stencilaways.DepthBufferEnable=false;
//如果模具值为1,则将模具状态设置为“通过”
stencilKeep=新的DepthStencilState();
stencilKeep.StencilEnable=true;
stencilKeep.StencilFunction=比较函数.Equal;
stencilKeep.StencilPass=StencilOperation.Keep;
stencilKeep.ReferenceStencil=1;
stencilKeep.DepthBufferEnable=false;
rt=新的RenderTarget2D(图形设备、形状.宽度、形状.高度、,
false,SurfaceFormat.Color,DepthFormat.Depth24模具8,
0,RenderTargetUsage.DiscardContents);
}
受保护的覆盖无效UnloadContent()
{
}
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(Keyboard.GetState().IsKeyDown(Keys.Escape))
这是Exit();
//TODO:在此处添加更新逻辑
更新(游戏时间);
}
浮动角度=0f;
私人纹理2d-back;
矢量2位置=新矢量2(400300);
浮云标度=0.25f;
受保护覆盖无效绘制(游戏时间游戏时间)
{
//将渲染设置为活动渲染目标
GraphicsDevice.SetRenderTarget(rt);
//将渲染目标清除为不透明黑色,
//并用全零初始化模具缓冲区
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil,
新颜色(0,0,0,1,0,0);
spriteBatch.Begin(SpriteSortMode.Immediate,BlendState.不透明,
null、Stencilaways、null、alphaTestEffect);
spriteBatch.Draw(形状,矢量2.零,空,颜色.白色,0f,
矢量2。零,1f,精灵效果。无,0f);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Immediate、BlendState.AlphaBlend、,
空,模具保持,空,空);
//对于(int i=0;i<数学上限(800/(cloud.Width*cloudscale));i++)
//对于(int j=0;j<数学上限(600/(云高*云标));j++)
//spriteBatch.Draw(云,矢量2.Zero+新矢量2(i*云。宽度*云比例,j*云。高度*云比例),null,彩色。白色,0f,
//矢量2.零,云尺度,SpriteeEffects.无,0f);
spriteBatch.Draw(草,新矢量2(rt.Width/2,rt.Height/2)+新矢量2(0f,-100f),空,彩色。白色,0f,
新矢量2(草宽/2,克