声音和加载进度条

Playing Sounds

有两种主要的方法去播放声音,调用库中的或者调用外部文件
对于大多数游戏来说,最好的方法应该是调用库中的声音, 你可以把声音倒入flash的库中
去使用这个声音你需要设置linkage,填写class name, 然后我们能在代码中使用这个名字来控制声音
我们命名为sound1

然后播放声音,只需要两行代码
var sound1:Sound1 = new Sound1();
var channel:SoundChannel = sound1.play();

我们也可以用一行表示
var channel:SoundChannel = (new Sound1()).play();

播放外部声音的话有一点点地复杂, 首先你需要载入这个声音到一个对象里面

var sound2:Sound = new Sound();
var req:URLRequest = new URLRequest(“PlayingSounds.mp3”);
sound2.load(req);

然后使用播放命令去播放这个声音
sound2.play();

  1. var button1:Button1 = new Button1();
  2. var button2:Button2 = new Button2();
  3. button1.x =50;
  4. button2.x =200;
  5. button1.y = 100;
  6. button2.y = 100;
  7. addChild (button1);
  8. addChild (button2);

  9. button1.addEventListener(MouseEvent.CLICK, playinternal);
  10. button2.addEventListener(MouseEvent.CLICK, playexternal);

  11. function playinternal (Event:MouseEvent)
  12. {
  13. var sound1:Sound1 = new Sound1();
  14. var channel:SoundChannel = sound1.play();}

  15. function playexternal (Event:MouseEvent)
  16. {
  17. var sound2:Sound = new Sound();
  18. var req:URLRequest = new URLRequest("external.mp3");
  19. sound2.load(req);
  20. sound2.play();
  21. }
复制代码

Loading Screen

flash是流媒体的,意思是尽管flash没有完全下载完毕!只要下载一点点动画还是可以开始播放
这个功能很好!不用用户去等,但是游戏中就不一样了, 因为游戏中需要所有的游戏元素下载完毕
才能运行,所以使用一个loading screen去强制所有的影片元素下载完毕才可以开始播放

简单的方法是你在第一帧加上个stop();

然后我们设置一个ENTER FRAME监听去调用loadProgress函数

addEventListener(Event.ENTER_FRAME, loadProgress);

这个函数可以通过设置this.root.loaderInfo得到影片的状态, 通过bytesLoaded 和bytesTotal
得到影片下载的字节数和影片总字节数

function loadProgress(event:Event) {
// get bytes loaded and bytes total
var movieBytesLoaded:int = this.root.loaderInfo.bytesLoaded;
var movieBytesTotal:int = this.root.loaderInfo.bytesTotal;
// convert to KiloBytes
var movieKLoaded:int = movieBytesLoaded/1024;
var movieKTotal:int = movieBytesTotal/1024;

去展示给用户下载的过程

progressText.text = “Loading: “+movieKLoaded+”K/”+movieKTotal+”K”;

当下载的字节数和影片总字节数相等的时候,我们删除监听并开始播放影片

// move on if done
if (movieBytesLoaded >= movieBytesTotal) {
removeEventListener(Event.ENTER_FRAME, loadProgress);
gotoAndStop(2);
}
}

  1. stop();

  2. addEventListener(Event.ENTER_FRAME, loadProgress);

  3. function loadProgress(event:Event) {
  4. // get bytes loaded and bytes total
  5. var movieBytesLoaded:int = this.root.loaderInfo.bytesLoaded;
  6. var movieBytesTotal:int = this.root.loaderInfo.bytesTotal;

  7. // convert to KiloBytes
  8. var movieKLoaded:int = movieBytesLoaded/1024;
  9. var movieKTotal:int = movieBytesTotal/1024;

  10. // show progress
  11. progressText.text = "Loading: "+movieKLoaded+"K/"+movieKTotal+"K";

  12. // move on if done
  13. if (movieBytesLoaded >= movieBytesTotal) {
  14. removeEventListener(Event.ENTER_FRAME, loadProgress);
  15. gotoAndStop(2);
  16. }
  17. }
原文地址:https://www.cnblogs.com/top5/p/1667776.html