FLEX和Actionscript开发FLASH游戏8-2-1
2010年11月26日
使用FLEX和Actionscript开发FLASH 游戏-音乐和声效 Enemy.as package { import flash.geom.Point; import mx.core.*; public class Enemy extends AnimatedGameObject { static public var pool:ResourcePool=new ResourcePool(NewEnemy); protected var logic:Function=null; protected var speed:Number=0; static public function NewEnemy():Enemy { return new Enemy(); } public function Enemy() { super(); } public function startupBasicEnemy(graphics:GraphicsResource,positi on:Point,speed:Number):void { super.startupAnimatedGameObject(graphics,position, ZOrder.PlayerZOrder); logic=basicEnemyLogic; this.speed=speed; this.collisionName=CollisionIdentifiers.ENEMY; } override public function shutdown():void { super.shutdown(); logic=null; } override public function enterFrame(dt:Number):void { super.enterFrame(dt); if(logic!=null) logic(dt); } protected function basicEnemyLogic(dt:Number):void { if(position.y>Application.application.height+graphi cs.bitmap.height) this.shutdown(); position.y+=speed*dt; } override public function collision(other:GameObject):void { var animatedGameObject:AnimatedGameObject=AnimatedGame Object.pool.ItemFromPool as AnimatedGameObject; animatedGameObject.startupAnimatedGameObject(Resou rceManager.BigExplosionGraphics,newPoint( position.x+graphics.bitmap.width/graphics.frames/2 - ResourceManager.BigExplosionGraphics.bitmap.width/ ResourceManager.BigExplosionGraphics.bitmap.height/ 2), ZOrders.PlayerZOrder,true); this.shutdown(); ResourceManager.ExplosionFX.play(); } } } 仅仅调用一次play函数我们就能播放声效。我曾讲过FlEX使得播放声音相当简单。Player类中生成一个武器时(在enterFrame函数里)调用相似的play函数,当被击毁时(在collision函数里)也调用之。在本文末下载源代码来查看这些改变。
播放背景音乐的进程是相似的,但是这里我们需要采取一些额外的步骤,因为我们需要能够在退出这一级时手工地停止音乐(例如当我们离开Game状态)。现在让我们看看在Level类中做的改变。 类别:flex 开发,组件
2010年11月26日
使用FLEX和Actionscript开发FLASH 游戏-音乐和声效 Enemy.as package { import flash.geom.Point; import mx.core.*; public class Enemy extends AnimatedGameObject { static public var pool:ResourcePool=new ResourcePool(NewEnemy); protected var logic:Function=null; protected var speed:Number=0; static public function NewEnemy():Enemy { return new Enemy(); } public function Enemy() { super(); } public function startupBasicEnemy(graphics:GraphicsResource,positi on:Point,speed:Number):void { super.startupAnimatedGameObject(graphics,position, ZOrder.PlayerZOrder); logic=basicEnemyLogic; this.speed=speed; this.collisionName=CollisionIdentifiers.ENEMY; } override public function shutdown():void { super.shutdown(); logic=null; } override public function enterFrame(dt:Number):void { super.enterFrame(dt); if(logic!=null) logic(dt); } protected function basicEnemyLogic(dt:Number):void { if(position.y>Application.application.height+graphi cs.bitmap.height) this.shutdown(); position.y+=speed*dt; } override public function collision(other:GameObject):void { var animatedGameObject:AnimatedGameObject=AnimatedGame Object.pool.ItemFromPool as AnimatedGameObject; animatedGameObject.startupAnimatedGameObject(Resou rceManager.BigExplosionGraphics,newPoint( position.x+graphics.bitmap.width/graphics.frames/2 - ResourceManager.BigExplosionGraphics.bitmap.width/ ResourceManager.BigExplosionGraphics.bitmap.height/ 2), ZOrders.PlayerZOrder,true); this.shutdown(); ResourceManager.ExplosionFX.play(); } } } 仅仅调用一次play函数我们就能播放声效。我曾讲过FlEX使得播放声音相当简单。Player类中生成一个武器时(在enterFrame函数里)调用相似的play函数,当被击毁时(在collision函数里)也调用之。在本文末下载源代码来查看这些改变。
播放背景音乐的进程是相似的,但是这里我们需要采取一些额外的步骤,因为我们需要能够在退出这一级时手工地停止音乐(例如当我们离开Game状态)。现在让我们看看在Level类中做的改变。 类别:flex 开发,组件