使用Flex和Actionscript开发Flash游戏―(八)

使用Flex和Actionscript开发Flash游戏―(八)
2010年07月11日
  这一部分,要加入音乐和音效了
  上一部分,我们实现了很帅的爆炸动画。现在,我们需要游戏更打动人一些,毫无疑问:声音。这一部分,我们就要加入音乐和声音。
  利用Flex的嵌入功能,我们可以很容易的实现这一切。首先在ResourceManager中加入声音。
  [b]ResourceManager.as
  [/b]package
  {
  import flash
  .display.*;
  import mx.core.*;
  public final class ResourceManager
  {
  [Embed(source="../media/brownplane.png")]
  public static var BrownPlane:Class;
  public static var BrownPlaneGraphics:GraphicsResource = new GraphicsResource(new BrownPlane(), 3, 20);
  [Embed(source="../media/smallgreenplane.png")]
  public static var SmallGreenPlane:Class;
  public static var SmallGreenPlaneGraphics:GraphicsResource = new GraphicsResource(new SmallGreenPlane(), 3, 20);
  [Embed(source="../media/smallblueplane.png")]
  public static var SmallBluePlane:Class;
  public static var SmallBluePlaneGraphics:GraphicsResource = new GraphicsResource(new SmallBluePlane(), 3, 20);
  [Embed(source="../media/smallwhiteplane.png")]
  public static var SmallWhitePlane:Class;
  public static var SmallWhitePlaneGraphics:GraphicsResource = new GraphicsResource(new SmallWhitePlane(), 3, 20);
  [Embed(source="../media/bigexplosion.png")]
  public static var BigExplosion:Class;
  public static var BigExplosionGraphics:GraphicsResource = new GraphicsResource(new BigExplosion(), 7, 20);
  [Embed(source="../media/smallisland.png")]
  public static var SmallIsland:Class;
  public static var SmallIslandGraphics:GraphicsResource = new GraphicsResource(new SmallIsland());
  [Embed(source="../media/bigisland.png")]
  public static var BigIsland:Class;
  public static var BigIslandGraphics:GraphicsResource = new GraphicsResource(new BigIsland());
  [Embed(source="../media/volcanoisland.png")]
  public static var VolcanoIsland:Class;
  public static var VolcanoIslandGraphics:GraphicsResource = new GraphicsResource(new VolcanoIsland());
  [Embed(source="../media/twobullets.png")]
  public static var TwoBullets:Class;
  public static var TwoBulletsGraphics:GraphicsResource = new GraphicsResource(new TwoBullets());
  [Embed(source="../media/cloud.png")]
  public static var Cloud:Class;
  public static var CloudGraphics:GraphicsResource = new GraphicsResource(new Cloud());
  [Embed(source="../media/gun1.mp3")]
  public static var Gun1Sound:Class;
  public static var Gun1FX:SoundAsset = new Gun1Sound() as SoundAsset;
  [Embed(source="../media/explosion.mp3")]
  public static var ExplosionSound:Class;
  public static var ExplosionFX:SoundAsset = new ExplosionSound() as SoundAsset;
  [Embed(source="../media/track1.mp3")]
  public static var Track1Sound:Class;
  public static var Track1FX:SoundAsset = new Track1Sound() as SoundAsset;
  }
  }
  Read more: http://gd2.fanqiang.org/fan.php?u=Oi8vd3d3LmJyaWdodGh1Yi5jb20vaW50ZXJuZXQvd2ViLWR ldmVsb3BtZW50L2FydGljbGVzLzExODkxLmFzcHg%3D&b=5#ixz z0QfbrpNPP
  复制代码嵌入声音与嵌入图片很像,同样为我们提供了方便的访问方法。
  现在声音准备好了,只等播放了。我们需要爆炸的声音,发射子弹的声音以及背景音乐。
  看看Enemy类的变化:
  [b]Enemy.as [/b]
  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, position:Point, speed:Number):void
  {
  super.startupAnimatedGameObject(graphics, position, ZOrders.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 + graphics.bitmap.height )
  this.shutdown();
  position.y += speed * dt;
  }
  override public function collision(other:GameObject):void
  {
  var animatedGameObject:AnimatedGameObject = AnimatedGameObject.pool.ItemFromPool as AnimatedGameObject;
  animatedGameObject.startupAnimatedGameObject(
  ResourceManager.BigExplosionGraphics,
  new Point(
  position.x + graphics.bitmap.width / graphics.frames / 2 - ResourceManager.BigExplosionGraphics.bitmap.width / ResourceManager.BigExplosionGraphics.frames / 2,
  position.y + graphics.bitmap.height / 2 - ResourceManager.BigExplosionGraphics.bitmap.height / 2),
  ZOrders.PlayerZOrder,
  true);
  this.shutdown();
  ResourceManager.ExplosionFX.play();
  }
  }
  }
  Read more: http://gd2.fanqiang.org/fan.php?u=Oi8vd3d3LmJyaWdo dGh1Yi5jb20vaW50ZXJuZXQvd2ViLWRldmVsb3BtZW50L2FydGl jbGVzLzExODkxLmFzcHg%2FcD0y&b=5#ixzz0Qfch6WQw
  复制代码
  调用play方法就可以播放了,很简单。Player类与它很类似,除了被击毁时还需要在发射武器时播放音效。请参考源文件吧。
  背景音乐的实现方法很类似。不过我们需要在退出游戏时停止播放,请看代码:
  [b]Level.as [/b]
  package
  {
  import flash.events.*;
  import flash.geom.*;
  import flash.media.*;
  import flash.net.*;
  import flash.utils.*;
  import mx.collections.*;
  import mx.core.*;
  public class Level
  {
  protected static var instance:Level = null;
  protected static const TimeBetweenLevelElements:Number = 2;
  protected static const TimeBetweenEnemies:Number = 3;
  protected static const TimeBetweenClouds:Number = 2.5;
  protected static const TimeToLevelEnd:Number = 2;
  protected var timeToNextLevelElement:Number = 0;
  protected var levelElementGraphics:ArrayCollection = new ArrayCollection();
  protected var timeToNextEnemy:Number = 0;
  protected var enemyElementGraphics:ArrayCollection = new ArrayCollection();
  protected var timeToNextCloud:Number = 0;
  protected var timeToLevelEnd:Number = 0;
  protected var backgroundMusic:SoundChannel = null;
  public var levelEnd:Boolean = false;
  static public function get Instance():Level
  {
  if ( instance == null )
  instance = new Level();
  return instance;
  }
  public function Level(caller:Function = null )
  {
  if ( Level.instance != null )
  throw new Error( "Only one Singleton instance should be instantiated" );
  levelElementGraphics.addItem( ResourceManager.SmallIslandGraphics);
  levelElementGraphics.addItem( ResourceManager.BigIslandGraphics);
  levelElementGraphics.addItem( ResourceManager.VolcanoIslandGraphics);
  enemyElementGraphics.addItem( ResourceManager.SmallBluePlaneGraphics);
  enemyElementGraphics.addItem( ResourceManager.SmallGreenPlaneGraphics);
  enemyElementGraphics.addItem( ResourceManager.SmallWhitePlaneGraphics);
  }
  public function startup():void
  {
  timeToNextLevelElement = 0;
  new Player().startupPlayer();
  timeToLevelEnd = TimeToLevelEnd;
  levelEnd = false;
  backgroundMusic = ResourceManager.Track1FX.play(0, int.MAX_VALUE);
  }
  public function shutdown():void
  {
  backgroundMusic.stop();
  backgroundMusic = null;
  }
  public function enterFrame(dt:Number):void
  {
  // add a background element
  timeToNextLevelElement -= dt;
  if (timeToNextLevelElement 游戏虽然有一些跳转,但是声音播放是一个很常见的任务。只有少量的修改。
  下一章,我们会将定义游戏的level结构,例如敌机从哪里出现,背景元素和其它元素。
  这次的结果:http://gd2.fanqiang.org/fan.php? ... 4Lmh0bWw%3D&b=5
  源码:http://gd2.fanqiang.org/fan.php? ... WQ9NjM0NTAy&b=5
  
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值