Copy link to clipboard
Copied
Any tips on combining SoundArrays into soundChannels with transform?
a movieclip child is triggured by a timer every 250ms and i've coded the sounds.
i'm trying to put this script below
//Lasers Array
var laserArray:Array = new Array[Zap1, Zap2, Zap3];
var oneLaser = laserArray[Math.floor(Math.random() * laserArray.length)];
trace ("oneLaser");
into
//Plays a laser sound
var laser:Sound = new Zap1();
var laserChan:SoundChannel = new SoundChannel;
laserChan = laser.play();
//pans a laser Sound
var laserTrans:SoundTransform = new SoundTransform;
{
laserTrans.volume = 1;
laserTrans.pan = (tempLaser.x / (stage.stageWidth / 2) -1);
}
laserChan.soundTransform = laserTrans;
thanks in advance
I wrote it by hand so it's probably just the difference in the documentation. They type cast the class and use an 'Object' as it's instantiation. In other words:
// randomly choose a laser
var soundClass:Class = getDefinitionByName(laserArray[Math.floor(Math.random() * laserArray.length)]) as Class;
// create the laser sound
var laser:Object = new soundClass();
// assign to the channel
var laserChan = new SoundChannel();
laserChan = laser.play();
// ...transform code
The bolded parts are the difference
...Copy link to clipboard
Copied
While it's going to consume memory to keep all the sounds in memory at the same time (in the Array), to randomly choose a laser combining both scripts is really just changing the first line of the second script:
// choose from the array
var laserArray:Array = new Array[new Zap1(), new Zap2(), new Zap3()];
var laser:Sound = laserArray[Math.floor(Math.random() * laserArray.length)];
// instantiate
var laserChan:SoundChannel = new SoundChannel;
laserChan = laser.play();
// rest....
While that works at full memory speed (preloaded) it keeps all the lasers in memory. If you're not firing them off extremely quickly then consider making the array contain the class name in a string and use the getDefinitionByName() method to retrieve it:
e.g.
import flash.utils.getDefinitionByName;
var laserArray:Array = new Array['Zap1', 'Zap2', 'Zap3'];
var soundClass:Class = getDefinitionByName(laserArray[Math.floor(Math.random() * laserArray.length)]);
var laser:Sound = new soundClass() as Sound;
Takes a little lookup time but lowers memory needs.
Copy link to clipboard
Copied
Thanks Sinious for your reply
I'm still running into some Coersion errors
"TypeError: Error #1007: Instantiation attempted on a non-constructor."
but i'll work with your first example as i want to learn this as best i can
i'm currently firing view a timer set to 250ms
i'll have a read into getDefinitionByName() once i've fully understood more of the basics.
Thanks again
Copy link to clipboard
Copied
I wrote it by hand so it's probably just the difference in the documentation. They type cast the class and use an 'Object' as it's instantiation. In other words:
// randomly choose a laser
var soundClass:Class = getDefinitionByName(laserArray[Math.floor(Math.random() * laserArray.length)]) as Class;
// create the laser sound
var laser:Object = new soundClass();
// assign to the channel
var laserChan = new SoundChannel();
laserChan = laser.play();
// ...transform code
The bolded parts are the difference and should clean up the error.
What getDefinitionByName() does is simply look in your library for any element that has the name you request as a class name (linkage). For example I imported an old MP3, right-clicked on it in the library, clicked the ActionScript tab and checked off to set the linkage and named the class "GoodDay":
What Flash will do is make a class called GoodDay and inside it will be the Sound. For me to get access to it I can use getDefinitionByName to get access to the library class. From that class I can create the Sound.
e.g.:
// get the class the library generates
var theClass:Class = getDefinitionByName('GoodDay') as Class;
// now use that class to get the sound
var theSound:Object = new theClass();
// now I have the Sound in "theSound" and can play it
var myChannel:SoundChannel = new SoundChannel();
myChannel = theSound.play();
Make sure your laser sounds all have a unique class linkage name (laser1, laser2, laser3, etc) and then you can use getDefinitionByName to access them.
Again this just reduces memory by not preloading all the sounds into an array ahead of time. It's good to know how to access library elements in this way anyhow.
Copy link to clipboard
Copied
Thanks for this i got it working, with the randomized sounds too!
getDefinitionByName is pretty amazing!
a definte must know method!
Copy link to clipboard
Copied
You're welcome and good luck!