• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Randomized Sound Arrays into Sound Channels and Transforms

New Here ,
Dec 30, 2013 Dec 30, 2013

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

TOPICS
ActionScript

Views

745

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 30, 2013 Dec 30, 2013

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

...

Votes

Translate

Translate
LEGEND ,
Dec 30, 2013 Dec 30, 2013

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:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getDefin...

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 30, 2013 Dec 30, 2013

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 30, 2013 Dec 30, 2013

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":

linkage.jpg

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 30, 2013 Dec 30, 2013

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 02, 2014 Jan 02, 2014

Copy link to clipboard

Copied

LATEST

You're welcome and good luck!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines