views:

25

answers:

2

Hi,

I am a newbie for ActionScript. I would like to add Sound dynamically to my flv. I have this code. I am pretty sure it is working because I found it in Textbook. :(.

package{
 import flash.display.Sprite;
 import flash.media.Sound;
 import flash.net.URLRequest;

 public class soundLoader extends Sprite{
  private var sound:Sound;

  public function soundLoader(){
   sound = new Sound(new URLRequest("sound.mp3"));
   addChild(sound);
  }

 }

}

When I try to run the code I got these error messages.

1046: Type was not found or was not a compile-time constant: Sound.

1180: Call to a possibly undefined method Sound.

Where did I do wrong? Please,please help me.

A: 

There are a few things that are wrong with this code. First of all, by convention class names in AS3 start with an upper case letter, so you should rename your class SoundLoader and not soundLoader. Second, the Sound object in AS3 is not a display object so you can't add it to a sprite's display list. Therefor the call to addChild( sound ) is wrong, and will cause compile errors.

That doesnt solve your probleme however. Are you using Flash or Flex to write this code ?

__dominic
Hi __dominic,Thanks for pointing my mistakes. I am using Adobe Flash CS3. So I guess it is Flash. Sorry, I am still very much confused between Flash and Flex.
cynwong
I compiled your code using Flash CS4 and it works. Try importing flash.media.* but I'm not sure it makes a difference... I don't have CS3 so I can only test in CS4
__dominic
I tried to import flash.media.*, but it is still giving me the same error. :(
cynwong
A: 

here is my flash file: http://www.signalsondisplay.com/sotest.rar I wrote it in Flash CS4 so I don't know if you can open it with CS3. Hope it helps, because I have no idea why you get that error. Try checking your classpath and that sort of stuff in the swf settings.

__dominic
Thanks but still does not work. I have checked the classpath in ActionScript 3.0 settings under Preferences. It has '$(AppConfig)/ActionScript 3.0/Classes'. Do I need to add some other ClassPath for this?
cynwong
In CS4 it's '$(AppConfig)/ActionScript 3.0/libs', it's probably the same thing. You should try making a very small Flash file with just the SoundLoader class you created, like I did in the .rar I sent you, just to test if you get the same error.Maybe it's a naming conflict. Have you created a class called Sound.as ? If you have, that might be the problem.If not, I have no idea what the problem is.
__dominic
Actually, it is a very small project like you said. I only have "var s:SoundLoader = new SoundLoader();" in my Flash Document and that SoundLoader class( in SoundLoader.as). Maybe CS3 does not like Sound Object class. Thanks, anyway, for your help.
cynwong