关于游戏引擎PushButton的教程和演示(五)

首先,谈不上教程,我也只是把我喜欢的写给大家而已,我对Flex技术并没有什么深入的理解,只是喜欢。

我对Flash并没有比大家更多的认识(我的朋友都知道,我不过只是做些ASP.NET动态网站和进行些浏览器编程,如ExtJs,jQuery),一个偶然的想法,我打开了FlexBuilder,辗转多个网页,感谢chrome,让我找到了PushButton,以下教程的大部分内容都来自于Matthew Casperson的博客,如果你也像我一样,对PushButton或者游戏开发忽然很感兴趣,请跟着Matthew Casperson一起开始游戏之旅。

如果你需要了解PushButton,你只需要在Google上搜索flex pushbutton就能获得一个关于 PushButton的概括性介绍,比较关键的介绍是,这是一个模块化的以组件为核心的游戏引擎(在以后的开发中,您将有深刻的体会),对于网络中的些许介绍,我想声明一点的是,当前的版本为r470,我并没有发现一些博客中所提到的ProjectManager.air(这困扰了我很久),不过很快你将发现这并不影响什么。

http://pushbuttonengine.com官方论坛,在这里你能找到你想要的大部分,包括下载链接以及http://pushbuttonengine.com/forum/index.php,PushButton的官方论坛,社区里人们很友善,这里包括技术交流也有组件买卖。 

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

上次说到让一个角色动起来,也就是让他“浑身颤抖”,在动游戏中,我们更新角色动作是一件基本的事情,在PushButton中,处理动作变换是一件简单的事,上次我们创建了一个AnimationControllerInfo,并且让它成为了角色默认的Animation(Default Animation),我们只需要在现有的基础上添加若干个AnimationControllerInfo然后让它们互相切换就可以了.我没接触过其他的游戏引擎,不过这让我觉得自然和方便。

 1     var IdleRightSpriteSheet:SpriteSheetComponent = new SpriteSheetComponent();
 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

1 public var animation:String;
2  public function get currentAnimation():String
3         {
4         return animation;
5         }

 

 

 

这样,我们便获得了一个对currentAnimation的一个引用,通过animation这个字符串,对currentAnimation进行相应的改变,也就即改变了这个Animation动作的变化。当然,改变 currentAnimation之后,我们需要激发相应的事件,以得到实体的响应。

我们需要对Animation组件做进一步修改,添加一个切换动作时的事件名称  

1  Animation.changeAnimationEvent ="PlayerAnimation"

 

 然后在键盘控制类中加入string成员

1     public var AnimationEventName:String;

 

 在引用键盘控制类时,这个属性赋值为PlayerAnimation

 1         Input.AnimationEventName = "PlayerAnimation";// important!

 

之后在相应的变换动作时我们可以激发这个事件,如 

 1                animation="RunRight";

2                 owner.eventDispatcher.dispatchEvent(new Event( AnimationEventName));

 

 在这个实例中,在人物奔跑或者静止的时候,使用了两个布尔标量     

1protected var facing:Boolean =true;//true means right ,false means left;

2         protected var idle:Boolean=true;//true means is idle,false means moving 

我用我尴尬的英语已经为大家做了以上注释,相应改变的时候便利用以上两个变量进行判断,因为这并不是该框架的主要内容只是一个简单的算法,所以这里不加赘述了。

贴出例子

 1 if (InputManager.isKeyDown(InputKey.RIGHT))
 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,感谢无私的分享。 

 

posted on 2010-03-12 10:01  华电混球  阅读(568)  评论(0编辑  收藏  举报

导航

我的应用