博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Silverlight 解密游戏 之十 自定义粒子特效
阅读量:6439 次
发布时间:2019-06-23

本文共 3975 字,大约阅读时间需要 13 分钟。

  在第四篇《》中我们为游戏添加了一个粒子特效,但是当前的ParticleControl  只提供了一种圆形粒子,本篇将为其添加方形、三角形、星形等形状。

以下是五角星效果:

 

1. 首先在Interactivity\ParticlesBehavior 中加入ParticleShape.cs

class

其中包含ParticleShape 枚举:

public enum ParticleShape{       Circle,    Square,    Star4,   Star5,   Star8,   Custom }

2. 下面来修改ParticlesBehavior.cs 程序:

2.1. 添加ParticleShape、CustomShapePathData 属性:

[Category("Shape")]public ParticleShape ParticleShape{   get { return (ParticleShape)GetValue(ParticleShapeProperty); }   set { SetValue(ParticleShapeProperty, value); }}public static readonly DependencyProperty ParticleShapeProperty =   DependencyProperty.Register("ParticleShape", typeof(ParticleShape),   typeof(ParticlesBehavior), null);[Category("Shape")]public string CustomShapePathData{   get { return (string)GetValue(CustomShapePathDataProperty); }   set { SetValue(CustomShapePathDataProperty, value); }}public static readonly DependencyProperty CustomShapePathDataProperty =   DependencyProperty.Register("CustomShapePathData", typeof(string),   typeof(ParticlesBehavior), null);

2.2. 初始化ParticleShape:

public ParticlesBehavior(){   ... ...   this.ParticleShape = ParticleShape.Circle;}

 

2.3. 编辑OnShowParticles():

private void OnShowParticles(){   ... ...   p.ParticleShape = ParticleShape;   p.CustomShapePathData = CustomShapePathData;               AssociatedObject.Children.Add(p);}

3. 修改ParticleControl.xaml.cs 程序:

3.1. 同样在ParticleControl 类中添加ParticleShapeCustomShapePathData 属性,在ParticleControl() 方法中初始化ParticleShape。

3.2. 因为粒子形状要继承于Shape,所以将类中所有Ellipse 更改为Shape(其中涉及到SpawnParticle 与UpdateParticles 方法)。

3.3. 在ParticleControl 类中添加星形粒子形状:

private const string star4 = "F1 M 50,-7.62939e-006L 55.1144,41.3803C 56.427,42.2647 57.5178, 43.453 58.2861,44.8443L 100,50L 57.8372,55.2111C 57.1358, 56.2565 56.2419,57.162 55.2062,57.8767L 50,100L 44.8675, 58.4733C 43.3682,57.6757 42.0966,56.5067 41.1753, 55.0891L -2.3533e-006,50L 40.7417,44.9645C 41.6922,43.1858 43.166, 41.7288 44.9574,40.7991L 50,-7.62939e-006 Z";private const string star5 = "F1 M 50,7.62939e-006L 38.5,36.7447L -1.95619e-005,36.3271L 31.3926, 58.619L 19.0983,95.1057L 50,72.1381L 80.9017,95.1057L 68.6074, 58.619L 100,36.3271L 61.5,36.7447L 50,7.62939e-006 Z";private const string star8 = "F1 M 50,2.28882e-005L 54.558,36.8783C 55.5604,37.239 56.5077, 37.7153 57.3837,38.2912L 78.3425,21.6575L 61.6127,42.7374C 62.1035, 43.5727 62.5068,44.4656 62.81,45.4035L 100,50L 62.4628, 54.6395C 62.1585,55.3832 61.7896,56.0936 61.3631,56.7638L 76.3839, 76.3838L 56.6316,61.2618C 56.0088,61.6151 55.355,61.9202 54.6754, 62.172L 50,100L 45.3246,62.1719C 44.605,61.9053 43.9144, 61.579 43.2589,61.1989L 21.6575,78.3425L 38.7024,56.8655C 38.2481, 56.1656 37.8571,55.4209 37.5374,54.6395L 4.31164e-005,50L 37.1902, 45.4035C 37.4815,44.5022 37.8652,43.6425 38.3303,42.8356L 23.6162, 23.6161L 42.7073,38.232C 43.5578,37.6826 44.4742,37.2266 45.442, 36.8783L 50,2.28882e-005 Z";

 

3.4. 创建CreateShape 方法:

private Shape CreateShape(){   string pathData = "";   switch (ParticleShape)   {      case ParticleShape.Circle:           return new Ellipse();      case ParticleShape.Square:           return new Rectangle();      case ParticleShape.Star4:           pathData = star4;      break;      case ParticleShape.Star5:           pathData = star5;      break;      case ParticleShape.Star8:           pathData = star8;      break;      case ParticleShape.Custom:           if (string.IsNullOrEmpty(CustomShapePathData))              return new Ellipse();           else              pathData = CustomShapePathData;      break;      default:      return new Ellipse();   }   string xamlPath = string.Format( "
", pathData); Path path = (Path)System.Windows.Markup.XamlReader.Load(xamlPath); return path;}

 

4. 编译后进入Blend,选择LayoutRoot 中的ParticlesBehavior

pb

点选ShowParticles 中的EventTrigger,即可调整Shape 类型:

sp1

当然也可在CustomShapePathData 中自定义形状:

sp2

选择相应的形状后即可实现以下效果:

100609_0005_HiddenObjec4

 

源代码下载:

本文转自Gnie博客园博客,原文链接:http://www.cnblogs.com/gnielee/archive/2010/01/08/silverlight-puzzle-game-part10.html,如需转载请自行联系原作者

你可能感兴趣的文章
Node.js 连接 MySQL
查看>>
02-线性结构3. 求前缀表达式的值(25)
查看>>
csdn知识库
查看>>
安卓实训第四天--基于HttpClient来完毕数据在server和设备间的交互。
查看>>
软件測试、ios中的測试概念以及步骤
查看>>
具体图解 Flume介绍、安装配置
查看>>
tensorflow 1.0 学习:池化层(pooling)和全连接层(dense)
查看>>
LeetCode96_Unique Binary Search Trees(求1到n这些节点能够组成多少种不同的二叉查找树) Java题解...
查看>>
JAVA常见算法题(十二)
查看>>
spring-boot-oracle spring-batch
查看>>
URL编码与解码
查看>>
面向对象设计原则一:单一职责原则(SRP)
查看>>
Codeforces 839D Winter is here【数学:容斥原理】
查看>>
在js中怎样获得checkbox里选中的多个值?
查看>>
基于AllegroGraph实现Protege设计知识库模型的存储步骤
查看>>
线程中释放锁的方式
查看>>
VM环境下Linux虚拟机扩展存储空间操作方法总结
查看>>
PDB文件:每个开发人员都必须知道的
查看>>
深入理解生产者消费者
查看>>
EL表达式获取参数值${param.name}等
查看>>