C# 使用精灵图纸动画检测碰撞
如何检测与精灵动画的碰撞 SpriteManager.csC# 使用精灵图纸动画检测碰撞,c#,xna,sprite-sheet,C#,Xna,Sprite Sheet,如何检测与精灵动画的碰撞 SpriteManager.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework; using System.IO; namespace Mini { public class SpriteManage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.IO;
namespace Mini
{
public class SpriteManager
{
protected Texture2D Texture;
public Vector2 Position = Vector2.Zero;
protected Dictionary<string, AnimationClass> Animations =
new Dictionary<string, AnimationClass>();
protected int FrameIndex = 0;
protected Vector2 Origin;
private int height;
private int width;
private string animation;
public string Animation
{
get { return animation; }
set
{
animation = value;
FrameIndex = 0;
}
}
public int Height
{
get { return height; }
}
public int Width
{
get { return width; }
}
public Rectangle Rectangle
{
get { return Animations[Animation].Rectangles[FrameIndex]; }
}
public Texture2D Texture2D
{
get { return Texture; }
}
public SpriteManager(Texture2D Texture, int Frames, int animations)
{
this.Texture = Texture;
width = Texture.Width / Frames;
height = Texture.Height / animations;
Origin = new Vector2(width / 2, height / 2);
}
public void AddAnimation(string name, int row,
int frames, AnimationClass animation)
{
Rectangle[] recs = new Rectangle[frames];
Color[][] frameImageData = new Color[frames][];
Texture2D[][] frameImage = new Texture2D[frames][];
for (int i = 0; i < frames; i++)
{
recs[i] = new Rectangle(i * width,
(row - 1) * height, width, height);
frameImageData[i] = new Color[width * height];
}
animation.Frames = frames;
animation.Rectangles = recs;
Animations.Add(name, animation);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position,
Animations[Animation].Rectangles[FrameIndex],
Animations[Animation].Color,
Animations[Animation].Rotation, Origin,
Animations[Animation].Scale,
Animations[Animation].SpriteEffect, 0f);
}
public bool IntersectPixels(SpriteManager b)
{
Rectangle rectangleA = this.Rectangle;
Rectangle rectangleB = b.Rectangle;
int top = Math.Max(rectangleA.Top, rectangleB.Top);
int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
int left = Math.Max(rectangleA.Left, rectangleB.Left);
int right = Math.Min(rectangleA.Right, rectangleB.Right);
Color[] dataA = new Color[rectangleA.Width * rectangleA.Height];
this.Texture.GetData(0, rectangleA, dataA, 0, rectangleA.Width * rectangleA.Height);
Color[] dataB = new Color[rectangleB.Width * rectangleB.Height];
b.Texture.GetData(0, rectangleB, dataB, 0, b.Width * b.Height);
Stream s = File.Create("t.png");
b.Texture2D.SaveAsPng(s, rectangleB.Width, rectangleB.Height);
for (int y = top; y < bottom; y++)
{
for (int x = left; x < right; x++)
{
Color colorA = dataA[(x - rectangleA.Left) + (y - rectangleA.Top) * rectangleA.Width];
Color colorB = dataB[(x - rectangleB.Left) + (y - rectangleB.Top) * rectangleB.Width];
if (colorA.A != 0 && colorB.A != 0)
{
return true;
}
}
}
return false;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework;
使用System.IO;
命名空间迷你
{
公共类精神管理器
{
保护纹理2D纹理;
公共向量2位置=向量2.0;
受保护的字典动画=
新字典();
受保护的int FrameIndex=0;
受保护向量2起源;
私人内部高度;
私有整数宽度;
私有字符串动画;
公共字符串动画
{
获取{返回动画;}
设置
{
动画=价值;
FrameIndex=0;
}
}
公共内部高度
{
获取{返回高度;}
}
公共整数宽度
{
获取{返回宽度;}
}
公共矩形
{
获取{返回动画[Animation]。矩形[FrameIndex];}
}
公共纹理2D纹理2D
{
获取{返回纹理;}
}
公共SpriteManager(纹理2D纹理、整数帧、整数动画)
{
这个。纹理=纹理;
宽度=纹理。宽度/帧;
高度=纹理。高度/动画;
原点=新矢量2(宽度/2,高度/2);
}
public void AddAnimation(字符串名称,int行,
int帧,AnimationClass动画)
{
矩形[]recs=新矩形[帧];
颜色[][]帧图像数据=新颜色[帧][];
Texture2D[][]帧图像=新的Texture2D[frames][];
对于(int i=0;i
使用此代码,可以在任何地方每隔几帧动画检测一次碰撞
是用来检测碰撞的。能否请您详细说明您正在尝试管理哪种碰撞?据我对你的代码的理解,一个简单的“矩形1.相交(矩形2);”就足够了。我对每像素的碰撞感兴趣