2015-12-08 111 views
1

我的目标是创建一个小行星克隆,我有游戏设置,在chrism web教程中找到。我有几乎完全设置的游戏,但是,当我添加声音时,程序抱怨出现以下错误。FlashDevelop声音问题

\src\Main.as(21): col: 3 Error: Access of undefined property soundClip.

\src\Main.as(20): col: 17 Error: Type was not found or was not a compile-time constant: Sound.

的代码如下所示:

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.KeyboardEvent; 
    import flash.events.MouseEvent; 

    /** 
    * ... 
    * @author Chris Moeller 
    * http://www.chrismweb.com 
    * Tutorial: Creating an Asteroids Game: Part 4 
    */ 

    public class Main extends Sprite 
    { 
     [Embed(source = "snd/9mmshot.mp3")] 

     private const embeddedSound:Class; 
     var soundClip:Sound = new embeddedSound(); // Bullet 
     soundClip.play(); // Play the sound 
     private var game:Game; 
     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 
     } 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 
      // entry point 

      //create the game object passing in the swf width and height 
      game = new Game(stage.stageWidth, stage.stageHeight); 

      //add the game bitmap to the screen/ Main.as Sprite to make it visible 
      addChild(game.bitmap); 

      //Create the main game loop 
      addEventListener(Event.ENTER_FRAME, Run); 


      //add keylisteners 
      stage.addEventListener(KeyboardEvent.KEY_DOWN, game.KeyDown); 
      stage.addEventListener(KeyboardEvent.KEY_UP, game.KeyUp); 

      stage.addEventListener(MouseEvent.MOUSE_DOWN, game.MouseDown); 
      stage.addEventListener(MouseEvent.MOUSE_UP, game.MouseUp); 
      stage.addEventListener(MouseEvent.MOUSE_MOVE, game.MoveMouse); 
     } 

     private function Run(e:Event):void 
     { 
      game.Update(); 
      game.Render();    
     } 
    } 
} 

我试图研究这个问题,在这里没有运气,我甚至检查使用一些程序添加声音的教程。

如发现网上

但这里没有运气,我不知道发生了什么事我甚至减少音质。请帮忙!! 任何帮助将不胜感激! 我用在线音频转换器来转换音频。

回答

1

为避免你的第一个错误(Error: Type was not found or was not a compile-time constant: Sound.),您必须将Sound类导入到你的类:

// ... 

import flash.media.Sound; 

对于第二个错误,你应该知道,你不能使用你的soundClip对象的函数外,那部分只是为了声明(并初始化)你的对象,所以你可以这样做,例如:

private function shotFunction(): void 
{ 
    soundClip.play(); 
} 

希望能有所帮助。

+0

的第一个错误是固定的,但是,现在该程序将启动一个警告,说,\ SRC \ Main.as(21)抱怨西:7警告:VAR“soundClip”将范围限定为默认命名空间:主:内部。它在这个包之外是不可见的。我现在有以下代码公共类主要扩展Sprite \t { \t \t [嵌入(源= “SND/9mmshot.mp3”)] \t \t \t 私人\t常量embeddedSound:类; \t \t var soundClip:Sound = new embeddedSound(); //子弹 \t \t private function shotFunction():void { soundClip.play(); }研究没有帮助,但错误消失了。请帮帮我!!我几乎解决这个问题! –

+0

@JohnDoeLuis你可以把它'public':'公共变种soundClip:;西:19错误:属性声音=新embeddedSound()'... – akmozo

+0

该计划现在有了这个的\ src \ Main.as(21)抱怨是无效的,我几乎没有出错。请帮忙!!完整代码是在这里(”。公共类主要扩展Sprite \t { \t \t [嵌入(源= “SND/9mmshot.mp3”)] \t \t \t 私人\t常量embeddedSound:类; \t \t公共:公共VAR soundClip:声音=新embeddedSound(); // PlaySound \t \t \t \t私人变种游戏:游戏; \t \t公共函数main():无效“)。我的最终目标是声音加入到游戏中。 –