How to Create Sound Object Dynamically - actionscript-3

How to create sound object dynamically in one movieClip.
Example
for(i=1;i<5;i++){var sound + i = new Sound();}

You can try putting all the sounds from your loop inside an Array:
var soundArray:Array = [];
for (var i:uint = 0; i < 5; i++) {
var sound:Sound = new Sound();
// don't forget to set the path of the file you want to play
soundArray.push(sound);
}
To play the the sound, all you need to do is take note of the index:
Sound(soundArray[0]).play();
Hope this helps.
irot

you need a MovieClip to store your sounds into then:
var mc:MovieClip = new MovieClip();
for( var i:int = 0; i < 5; i++ )
{
mc[ 'sound_' + i ] = new Sound();
}
to access a sound, you can then call:
mc[ 'sound_0' ].play();
if you're already in the scope of a MovieClip, ignore the mc creation and replace 'mc' with 'this' in the loop.

Related

Movieclip names

I'm having troubles with AS3 and addChild methods.
First, I create an object called "container". Inside container I create an empty object with an empty MovieClip from library called "holder". Then I create the Movieclips inside the container.holder
But I cannot access to the MovieClips! Anyone knows why? Here is the code:
// Creating object
var container:Object {
x: 30,
y: 30
}
// Empty object
var eObject: MovieClip = new MovieClip();
container.holder = eObject;
// Creating Movieclips
var mc : MovieClip;
for (var i : int = 0; i < 5; i++) {
var mc: _myClip = new _myClip(); // _myClip is a MC from my library.
mc.name = "myMc"+ i;
mc.x = 10;
container.holder.addChild(mc);
}
// Calling MovieClips
container.holder["myMc"+3].x = 40; // Nothing happens
You can reduce the complexity and needless usage of the name property by using an Array:
var items:Array = [];
for (var i:int = 0; i < 5; i++) {
var mc:_myClip = new _myClip();
container.holder.addChild(mc);
items.push(mc);
}
items[2].x = 40;

as3 air check if file is loaded

How can I run code then the array is ready?
When I run the following code I get an error saying:
TypeError: Error #1010: A term is undefined and has no properties.
import flash.filesystem.File;
var desktop:File = File.applicationDirectory.resolvePath("sounds/drums");
var sounds:Array = desktop.getDirectoryListing();
for (var i:uint = 0; i < sounds.length; i++)
{
trace(sounds[i].nativePath); // gets the path of the files
trace(sounds[i].name);// gets the name
}
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("sounds/drums/"+sounds[i].name+""));
myChannel = mySound.play();
I usually use something like the below, each time a sound is loaded it is stored and a counter is incremented, once all sounds are loaded you can dispatch an event or start playing any of the sounds stored in loadedSounds.
var sounds:Array = desktop.getDirectoryListing();
var loadedSounds:Object = {};
var soundsLoaded:int = 0;
for (var i:uint = 0; i < sounds.length; i++)
{
var mySound:Sound = new Sound();
mySound.addEventListener(Event.COMPLETE, onSoundLoaded);
mySound.load(new URLRequest("sounds/drums/"+sounds[i].name));
}
private function onSoundLoaded(e:Event):void
{
var loadedSound = e.target as Sound;
// just get the file name without path and use it as key
var lastIndex:int = loadedSound.url.lastIndexOf("/");
var key:String = loadedSound.url.substr(lastIndex+1);
// store sounds for later reference
loadedSounds[key] = loadedSound ;
soundsLoaded++;
if (soundsLoaded == sounds.length)
{
//all sounds loaded, can start playing
}
}

how to change shared object path when I save data in it

how to change sharedObject save location,I want to save shared object on server for particular user, what I do for this?
You can look at SharedObject.getRemote() if you have a Flash Media server. Alternatively you can encode the data associated with your SharedObject as JSON and store it in a database.
Bad way, but it works
var src:ByteArray = your byteArray;
var arr:Array = new Array();
for (var i:int = 0; i < src.length; i++)
{
arr.push(src.readByte());
}
var jsonStr:String = arr.join("|");
and conversely...
var retArr:Array = jsonStr.split("|");
var ba:ByteArray = new ByteArray();
for (var j:int = 0; j < retArr.length; j++)
{
ba.writeByte(retArr[j]);
}
var ldr:Loader = new Loader();
ldr.loadBytes(ba);
addChild(ldr);

How to create a series of class instances in a for loop, as3

In my library I have a bunch of classes named tip1, tip2, tip3, tip4...and so on. Is it possible to create one instance of each on the stage using a for loop? I tried this but it didn't seem to work.
var tips:int = 12;
for(var i:int = 1; i<=tips; i++){
var tipName:String = "tip"+i
var tip:MovieClip = new tipName();
tip.name = "tip" + i
tip.x = stage.width;
tip.y = 0;
addChild(tip);
}
Any help would be appreciated. Thanks!
You were missing the "getDefinitionByName" part.
// Up top
import flash.utils.getDefinitionByName;
// Down below
var tips:int = 12;
for (var i:int = 1; i < tips; ++i ) {
var myClass:Class = getDefinitionByName('tip' + i) as Class;
var tip:Object = new myClass();
tip.name = "tip" + i;
....
}
Instead of
var tip:MovieClip = new tipName();
Try (written from memory)
var clazz:Class = getDefinitionByName(tipName) as Class;
var tip:MovieClip = new clazz();
Also, you generally want to use stage.stageWidth instead of stage.width, since the latter will return the stage bounding box width (which might not be the same as the area the swf file covers).

Accessing pieces of bitmaps inside a movieclip

I have a movieclip with 10 bitmaps in it. I wanna access each of them.
myMovieClip is the movieclip containing those 10 bitmaps. I wanna access those bitmaps one by one. All 10 bitmaps are imported separately.
I tried this :
for ( var i =0 ; i< myMovieClip.numChildren ; i++)
{
trace ( myMovieClip.getChildAt(i) );
}
Problem is numChildren comes "1" only, as if it doesnot consider those 10 pieces of bitmap. Any other way, to access those bitmaps ?
thanks
V.
what do you mean by pieces of bitmaps?? Do you mean 10 different bitmap objects are children of the movieClip??
In addition, your code does have a syntax error.
var newMc:MovieClip = MovieClip();
should be:
var newMc:MovieClip = new MovieClip();
second off all, in your loop, numChildren will be always changing since you are taking the reference of a child from the myMoiveClip and moving it to the newMc object. there are two way to fix this.
either set a local variable to the value of myMovieClip.numChildren and use that value in your loop
example:
var numOfChildren:int = myMovieClip.numChildren;
for(var i:int = 0; i < numOfChildren; i++){
var newMc:MovieClip = new MovieClip();
newMc.addChild(myMovieClip.getChildAt(i));
}
this will move the bitmaps out of myMovieClip and into newMc, if you want to keep them there you can create a new bitmap inside the loop and then add the new bitmap to the newMc.
example:
for(var i:int = 0; i < myMovieClip.numChildren; i++){
var newMc:MovieClip = new MovieClip();
var b:Bitmap = new Bitmap(Bitmap(myMovieClip.getChildAt(i)).bitmapData);
newMc.addChild(b);
}