C#开发WPF/Silverlight动画及游戏系列教程(Game Course):(十八) 完美精灵之八面玲珑(WPF Only)②
2010-10-01 07:07:47 来源:WEB开发网素材准备就绪,接着需要对这些属性进行设置来初始化主角精灵:
QXSpirit Spirit = new QXSpirit();
private void InitSpirit() {
Spirit.X = 300;
Spirit.Y = 400;
Spirit.Timer.Interval = TimeSpan.FromMilliseconds(150);
//设置角色身体及装备
Spirit.Equipment[0] = 0;
Spirit.Equipment[1] = 1;
Spirit.EachActionFrameRange = new int[] { 5, 8, 7, 6, 8 }; //这5个数字在第十七节中讲了多次
Spirit.DirectionFrameNum = 34; //每个方向行上有34列(第十七节中有说明)
Spirit.TotalWidth = 5100;
Spirit.TotalHeight = 1200;
//加载角色图片源(具体参数意思可以到QXSpirit中了解)
Spirit.Source = Super.EquipPart(Spirit.Equipment,
Spirit.DirectionNum, Spirit.DirectionFrameNum,
Spirit.TotalWidth, Spirit.TotalHeight,
Spirit.SingleWidth, Spirit.SingleHeight
);
Carrier.Children.Add(Spirit);
}
该方法很简单,注释描述得较清楚了,大家也可以将之与前面章节中的InitSpirit()进行比较来理解。接下来该让精灵动一下了,我们可以将精灵的生命线程进行如下改进:
//帧推进器
int _FrameCounter = 1;
public int FrameCounter {
get { return _FrameCounter; }
set { _FrameCounter = value; }
}
//精灵线程间隔事件
private void Timer_Tick(object sender, EventArgs e) {
//假如精灵动作发生改变,则调用ChangeAction()方法进行相关参数设置
if (OldAction != Action) {
ChangeAction();
}
//动态更改精灵图片源以形成精灵连续动作
Body.Source = Source[(int)Direction, FrameCounter];
FrameCounter = FrameCounter == CurrentEndFrame ? CurrentStartFrame : FrameCounter + 1;
}
Tags:开发 WPF Silverlight
编辑录入:爽爽 [复制链接] [打 印]- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››WPF学习笔记12. Binding (5)
- ››WPF学习笔记11. Binding (4)
- ››WPF学习笔记10. Binding (3)
- ››WPF学习笔记9. Binding (2)
- ››WPF学习笔记8. Binding
- ››WPF学习笔记7. Resource
- ››WPF学习笔记6. RoutedEvent
- ››WPF学习笔记5. DependencyProperty
- ››WPF学习笔记3. Navigation
- ››WPF学习笔记之2. Dispatcher
- ››WPF学习笔记之1. Application
更多精彩
赞助商链接