0

I see a lot of guides that do something like this:

var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel(); //unnecessary allocation?
myChannel = mySound.play();

Is allocating a new SoundChannel() in the second line completely unnecessary? From the docs, it seems that Sound.play generates a new SoundChannel every time, so you're not forcing it to use that SoundChannel that was just allocated, right? What the second line actually does is allocate a new SoundChannel, which does nothing, and assigns the variable myChannel to it. Then in the third line, mySound.play() generates a new SoundChannel and makes myChannel point to it instead, leaving the first SoundChannel inaccessible.

Should the second line just be this?

var myChannel:SoundChannel;

There are several guides which seem to do that unnecessary allocation, including an example in the official docs.

BladePoint
  • 632
  • 5
  • 16
  • 2
    You don't need to create an instance of **SoundChannel**, there's no point to it, you get the valid one as the result of **Sound.play()**, that's correct. – Organis Dec 04 '17 at 06:01
  • 1
    Yes, that first `SoundChannel` object you create is just thrown away/garbage collected after you assign `mySound.play()` to the `myChannel` var. So indeed it is a complete waste to instantiate a new SoundChannel like that and is best avoided. – BadFeelingAboutThis Dec 04 '17 at 20:42

0 Answers0