-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
上次说到让一个角色动起来,也就是让他“浑身颤抖”,在动游戏中,我们更新角色动作是一件基本的事情,在PushButton中,处理动作变换是一件简单的事,上次我们创建了一个AnimationControllerInfo,并且让它成为了角色默认的Animation(Default Animation),我们只需要在现有的基础上添加若干个AnimationControllerInfo然后让它们互相切换就可以了.我没接触过其他的游戏引擎,不过这让我觉得自然和方便。
2 IdleRightSpriteSheet.imageFilename = " ../media/idleright.png " ;
3
4 var divider:CellCountDivider = new CellCountDivider();
5 divider.xCount = 6 ;
6 IdleRightSpriteSheet.divider = divider;
7
8 var IdleRightAnimation:AnimationControllerInfo = new AnimationControllerInfo();
9 IdleRightAnimation.frameRate = 12 ;
10 IdleRightAnimation.loop = true ;
11 IdleRightAnimation.spriteSheet = IdleRightSpriteSheet;
12
13 Animation.animations[ " IdleRight " ] = IdleRightAnimation;
14 /// end of IdleRight
15 var IdleLeftSpriteSheet:SpriteSheetComponent = new SpriteSheetComponent();
16 IdleLeftSpriteSheet.imageFilename = " ../media/idleleft.png " ;
17 var divider2:CellCountDivider = new CellCountDivider();
18 divider2.xCount = 6 ;
19 IdleLeftSpriteSheet.divider = divider2;
20
21 var IdleLeftAnimation:AnimationControllerInfo = new AnimationControllerInfo();
22 IdleLeftAnimation.frameRate = 12 ;
23 IdleLeftAnimation.loop = true ;
24 IdleLeftAnimation.spriteSheet = IdleLeftSpriteSheet;
25 Animation.animations[ " IdleLeft " ] = IdleLeftAnimation;
26 // end of IdleLeft
27 var RunLeftSpriteSheet:SpriteSheetComponent = new SpriteSheetComponent();
28 RunLeftSpriteSheet.imageFilename = " ../media/runleft.png " ;
29 var divider3:CellCountDivider = new CellCountDivider();
30 divider3.xCount = 12 ;
31 RunLeftSpriteSheet.divider = divider3;
32
33 var RunLeftAnimation:AnimationControllerInfo = new AnimationControllerInfo();
34 RunLeftAnimation.frameRate = 12 ;
35 RunLeftAnimation.loop = true ;
36 RunLeftAnimation.spriteSheet = RunLeftSpriteSheet;
37 Animation.animations[ " RunLeft " ] = RunLeftAnimation;
38 // end of RunLeft
39 var RunRightSpriteSheet:SpriteSheetComponent = new SpriteSheetComponent();
40 RunRightSpriteSheet.imageFilename = " ../media/runright.png " ;
41
42 var runRightDivider:CellCountDivider = new CellCountDivider();
43 runRightDivider.xCount = 12 ;
44 RunRightSpriteSheet.divider = runRightDivider;
45
46 var runRightAnimation:AnimationControllerInfo = new AnimationControllerInfo();
47 runRightAnimation.frameRate = 12 ;
48 runRightAnimation.loop = true ;
49 runRightAnimation.spriteSheet = RunRightSpriteSheet;
50
51 Animation.animations[ " RunRight " ] = runRightAnimation;
52 // end of RunRight
53 entity.addComponent(Animation, " Animation " );
54 // end of Animation
这着实是一个繁琐而体力的过程,这在以后的开发中会进行优化(利用结构化的XML直接构建) 。但是原作者在第一版的教程中全部使用了Actionscript,不过在第二版的教程中,作者使用了以xml为基础开发的类似于“小蜜蜂”的游戏,在这一期中,我们仍然需要机械的书写一些代码,就如上面贴出的一样。
在方向控制上,我们需要在键盘控制类中,加入变换角色动作必要代码,首先添加一个getter
2 public function get currentAnimation():String
3 {
4 return animation;
5 }
这样,我们便获得了一个对currentAnimation的一个引用,通过animation这个字符串,对currentAnimation进行相应的改变,也就即改变了这个Animation动作的变化。当然,改变 currentAnimation之后,我们需要激发相应的事件,以得到实体的响应。
我们需要对Animation组件做进一步修改,添加一个切换动作时的事件名称
1 Animation.changeAnimationEvent ="PlayerAnimation";
然后在键盘控制类中加入string成员
在引用键盘控制类时,这个属性赋值为PlayerAnimation
1 Input.AnimationEventName = "PlayerAnimation";// important!
之后在相应的变换动作时我们可以激发这个事件,如
1 animation="RunRight";
在这个实例中,在人物奔跑或者静止的时候,使用了两个布尔标量
1protected var facing:Boolean =true;//true means right ,false means left;
我用我尴尬的英语已经为大家做了以上注释,相应改变的时候便利用以上两个变量进行判断,因为这并不是该框架的主要内容只是一个简单的算法,所以这里不加赘述了。
贴出例子
2 {
3 if ( ! facing || idle)
4 {
5 facing = true ;
6 idle = false ;
7 animation = " RunRight " ;
8 owner.eventDispatcher.dispatchEvent( new Event(AnimationEventName));
9 }
10
11 xMovement += Speed * tickRate;
12 while (xMovement >= 1 )
13 {
14 position.x += 1 ;
15 xMovement -= 1 ;
16 }
17 }
其他方向键大家就可以顺理推出了。
下一次我们尝试做一些类似于”超级玛丽“中的platform样的东西,其实就是一些能支撑角色跳来跳去的台子。每一次迈进一个新的阶段都让我内牛满面...。
Demo演示页面:http://www.brighthub.com/hubfolio/matthew-casperson/media/p/61014.aspx
Demo下载页面:http://www.brighthub.com/hubfolio/matthew-casperson/media/p/61069.aspx
教程原页面:http://www.bukisa.com/articles/226577_pushbutton-tutorial-series-platforms(我个人怀疑bukisa已经被和谐,因为我自从假期学完之后就再也没打开过)
最后,感谢 Matthew Casperson,感谢无私的分享。