Flash allocation scheme, bug or feature?
Having the following code:
var loadingSoundTitle:String;
var loadingSound:Soun开发者_运维知识库d = new Sound();
function loadFile(str:String)
{
loadingSound = new Sound(); //(1)
loadingSoundTitle = str;
try
{
loadingSound.load(new URLRequest(loadingSoundTitle));
}
catch(err:Error)
{
trace(err.message);
}
}
Doesn't work if the line marked with (1) is there, however if I comment out this line, its able to load one single file, before it stops working.
Using ActionScript 3.0 under CS5
Edit: I'm perfectly new to flash!
loadingSound.addEventListener(Event.COMPLETE, function(e:Event)
{
list.addItem({label:loadingSoundTitle, data:loadingSound});
process.value = 0; //for the nice little processbar I got :)
});
There's a lot that's missing here. I think I see what your problem is though. Firstly, the AS3 livedocs will tell you that a Sound object instance can load one and only one external mp3, so it's no surprise that removing the line marked with //(1) will work, but only one time.
The other problem I see here is that when you do include the line marked with //(1), you're never calling loadingSound.addEventListener(Event.COMPLETE, onCompleteHandler) or the like. You might be adding it to your old sound object (if you are, it isn't depicted here), but you're definitely not adding it to your new one. So with no way for the new sound to report that it's finished loading there's no way to play it after it's done because you never actually know when it's done.
Instead of repeatedly loading the sound over and over, a better way to handle this would be to load the sound one time and then just play it as needed.
EDIT:
Now that I see the listener code, I'm almost positive that's the problem.
- It's not inside the function call immediately after creating a new sound object. You have to add a new listener to every new Object instance you create.
- It's using an anonymous, unbound function.
You'd have to add that function every time you create the new Sound, not just the first time.
精彩评论