我正在尝试使用C#在Silverlight中为精灵表设置动画。 我已经取得了很大的进步,动画正在发挥作用,但问题是它们之间的表格不一致。 例如,我有这3张纸:
http://pokit.org/get/?8fc4562a63e6d7918789b0c84776a751.jpg
http://pokit.org/get/?ff4a073a307d50a7b950ac8dab26d60f.jpg
http://pokit.org/get/?053f25308d6a85a635913eb3f26f1c9f.jpg
前两个动画很好,但是当最后一个是动画时,我会看到其他帧的部分,如下所示:
http://pokit.org/get/?9c0dc2266b0a84a2243daf4db5b7217a.jpg
这是我用来为表格设置动画的代码:(宽度和高度是我之前确定的特定帧的尺寸)
public class Animation { private const int timeAnimationConstant = 10; private const int marginConstant = -11; private const int divisionConstant = 30; private const int frameTimeConstantMS = 150; public Animation() { } /* Funkcija koja radi animaciju bilo čega - prima containter(Rectangle s forme), sheet koji animira, sirinu sheeta, * visinu sheeta, broje frameova u sheetu, i status(statička, dinamička), loop - da li je repeatat?, repetitions koliko puta? */ public void Animate(Rectangle container, AnimationParams parameters) { // komit komentar ImageBrush imageBrush = new ImageBrush() { Stretch = Stretch.None, AlignmentX = AlignmentX.Left, AlignmentY = AlignmentY.Top }; imageBrush.Transform = new CompositeTransform(); imageBrush.ImageSource = parameters.Sheet; /* Ovdje pretpostavljam da smo po uzecu iz baze formirali BitmapImage tip i proslijedili */ container.Margin = new Thickness(marginConstant, 0, marginConstant, 0); container.Width = parameters.Width + parameters.Width/divisionConstant; container.Height = parameters.Height + parameters.Height/(divisionConstant/3); container.Stroke = new SolidColorBrush(Colors.Transparent); container.Fill = imageBrush; container.RadiusX = 5; container.RadiusY = 5; // Storyboard za animaciju Storyboard sb = new Storyboard(); if(parameters.Loop ) sb.RepeatBehavior = RepeatBehavior.Forever; // odredjujem da li je na repeat ObjectAnimationUsingKeyFrames frm = new ObjectAnimationUsingKeyFrames(); // inicijaliziram frameove animacije frm.BeginTime = new TimeSpan(0, 0, 0); int time = 0; // vrijeme kojim odredjujem trajanja frameova if (parameters.IsStatic == true) { // Statička animacija for (int j = 0; j < parameters.Repetitions; j++) { for (int i = 0; i < parameters.NumberOfFrames; i++) { DiscreteObjectKeyFrame dokf = new DiscreteObjectKeyFrame(); dokf.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(time)); dokf.Value = -(i * parameters.Width); frm.KeyFrames.Add(dokf); time += parameters.NumberOfFrames * timeAnimationConstant; } } KidCode.Game.isAnimationComplete = true; } else { // Dinamicka animacija } Storyboard.SetTarget(frm, container.Fill); Storyboard.SetTargetProperty(frm, new PropertyPath("(ImageBrush.Transform).(CompositeTransform.TranslateX)")); sb.Children.Add(frm); sb.Begin(); }AnimationParams类,以防万一:
public class AnimationParams { public BitmapImage Sheet { get; set; } public int Width { get; set; } public int Height { get; set; } public int NumberOfFrames { get; set;} public bool IsStatic { get; set; } public bool Loop { get; set; } public int Repetitions { get; set; } public AnimationParams(BitmapImage sheet, int width, int height, int numberOfFrames, bool isStatic, bool loop = false, int repetitions = 1) { Sheet = sheet; Width = width; Height = height; NumberOfFrames = numberOfFrames; IsStatic = isStatic; Loop = loop; Repetitions = repetitions; } }我希望有人可以帮助我,并提前感谢。
I'm trying to animate a sprite sheet in Silverlight with C#. I have made a lot of progress, the animations are working, but the problems is that they are not consistent between sheets. For example, I have these 3 sheets:
http://pokit.org/get/?8fc4562a63e6d7918789b0c84776a751.jpg
http://pokit.org/get/?ff4a073a307d50a7b950ac8dab26d60f.jpg
http://pokit.org/get/?053f25308d6a85a635913eb3f26f1c9f.jpg
The first two animate fine, but when the last one is animating I see parts of other frames, like this:
http://pokit.org/get/?9c0dc2266b0a84a2243daf4db5b7217a.jpg
This is the code I use to animate the sheets:(width and height are dimensions of particular frame which I determine earlier)
public class Animation { private const int timeAnimationConstant = 10; private const int marginConstant = -11; private const int divisionConstant = 30; private const int frameTimeConstantMS = 150; public Animation() { } /* Funkcija koja radi animaciju bilo čega - prima containter(Rectangle s forme), sheet koji animira, sirinu sheeta, * visinu sheeta, broje frameova u sheetu, i status(statička, dinamička), loop - da li je repeatat?, repetitions koliko puta? */ public void Animate(Rectangle container, AnimationParams parameters) { // komit komentar ImageBrush imageBrush = new ImageBrush() { Stretch = Stretch.None, AlignmentX = AlignmentX.Left, AlignmentY = AlignmentY.Top }; imageBrush.Transform = new CompositeTransform(); imageBrush.ImageSource = parameters.Sheet; /* Ovdje pretpostavljam da smo po uzecu iz baze formirali BitmapImage tip i proslijedili */ container.Margin = new Thickness(marginConstant, 0, marginConstant, 0); container.Width = parameters.Width + parameters.Width/divisionConstant; container.Height = parameters.Height + parameters.Height/(divisionConstant/3); container.Stroke = new SolidColorBrush(Colors.Transparent); container.Fill = imageBrush; container.RadiusX = 5; container.RadiusY = 5; // Storyboard za animaciju Storyboard sb = new Storyboard(); if(parameters.Loop ) sb.RepeatBehavior = RepeatBehavior.Forever; // odredjujem da li je na repeat ObjectAnimationUsingKeyFrames frm = new ObjectAnimationUsingKeyFrames(); // inicijaliziram frameove animacije frm.BeginTime = new TimeSpan(0, 0, 0); int time = 0; // vrijeme kojim odredjujem trajanja frameova if (parameters.IsStatic == true) { // Statička animacija for (int j = 0; j < parameters.Repetitions; j++) { for (int i = 0; i < parameters.NumberOfFrames; i++) { DiscreteObjectKeyFrame dokf = new DiscreteObjectKeyFrame(); dokf.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(time)); dokf.Value = -(i * parameters.Width); frm.KeyFrames.Add(dokf); time += parameters.NumberOfFrames * timeAnimationConstant; } } KidCode.Game.isAnimationComplete = true; } else { // Dinamicka animacija } Storyboard.SetTarget(frm, container.Fill); Storyboard.SetTargetProperty(frm, new PropertyPath("(ImageBrush.Transform).(CompositeTransform.TranslateX)")); sb.Children.Add(frm); sb.Begin(); }AnimationParams class, just in case:
public class AnimationParams { public BitmapImage Sheet { get; set; } public int Width { get; set; } public int Height { get; set; } public int NumberOfFrames { get; set;} public bool IsStatic { get; set; } public bool Loop { get; set; } public int Repetitions { get; set; } public AnimationParams(BitmapImage sheet, int width, int height, int numberOfFrames, bool isStatic, bool loop = false, int repetitions = 1) { Sheet = sheet; Width = width; Height = height; NumberOfFrames = numberOfFrames; IsStatic = isStatic; Loop = loop; Repetitions = repetitions; } }I hope someone can help me and thanks in advance.
最满意答案
我认为问题在于图像本身。 我试图用相同的宽度绘制每个框架的边框,这是不可能的。
我尝试了233像素宽:
151像素(由KooKiz建议)也不起作用:
因此,我修改了整个图像,剪切了所有帧,设置了一个宽度(181个像素)并再次连接它们。 这是结果:
这是我的解决方案。 使用下面的图像(您知道,右键单击,另存为...)并将帧的宽度设置为181像素:
有趣的事实:图像的高度是170像素,而不是218 ...
I think the problem is in the image itself. I tried to draw the border of each frame with the same width and it's impossible.
I tried 233 pixels width:
151 pixels (suggested by KooKiz) also doesn't work:
So, I modified the the whole image, cut all the frames, set one width (181 pixels) and join them again. That's the result:
Here's my solution. Use the image below (you know, right click, save as...) and set width of the frame to 181 pixels:
Interesting fact: the height of the image is 170 pixels, not 218...
更多推荐
发布评论