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

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);

Related

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
}
}

Create image-ojbects dynamically from library

I've created an Array with the names of my image-Classes in the library.
var myArr:Array = new Array("myPic1","myPic2","myPic3");
Normally if I would like to create an object of my class I would
var libraryImage:Bitmap;
libraryImage = new Bitmap(new myPic1(0,0));
But how can I iterate through the array and create my images dynamically?
for(var i:uint = 0; i<myArr.length; i++){
var libraryImage:Bitmap;
libraryImage = new Bitmap(new myArr[i](0,0));
}
doesn't works
Try this:
var myArr:Array = new Array(new myPic1(0,0), new myPic2(0,0), new myPic3(0,0));
for(var i:uint = 0; i<myArr.length; i++){
var libraryImage:Bitmap;
libraryImage = new Bitmap(myArr[i]);
}
You can do something like this
for(var i:uint = 0; i<myArr.length; i++){
var myClass:Class = getDefinitionByName(myArr[i]) as Class;
var libraryImage:Bitmap;
libraryImage = new Bitmap(new myClass(0,0));
}

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).

Dynamic vars MovieClips in AS3

Hello I'm trying to do this (in as2 this worked but not in as3) I looked on google for 3 hours, but still don't found a solution (thans for your help) :
import flash.display.MovieClip;
var mcContainer:MovieClip = new MovieClip();
var mcImage0:MovieClip = new MovieClip();
var mcImage1:MovieClip = new MovieClip();
var mcImage2:MovieClip = new MovieClip();
var mcImage3:MovieClip = new MovieClip();
mcImage0.name = "Boy";
mcImage1.name = "Girl";
mcImage2.name = "Woman";
mcImage3.name = "Man";
var ArrayNamesOfMC:Array = new Array();
var i:int = 4;
while(i--) {
ArrayNamesOfMC.push(["mcImage"+i].name);
}
This donsn't work :
ArrayNamesOfMC.push(["mcImage"+i].name);
This is the simple answer to your question:
var mcImage0:MovieClip = new MovieClip();
var mcImage1:MovieClip = new MovieClip();
var mcImage2:MovieClip = new MovieClip();
var mcImage3:MovieClip = new MovieClip();
mcImage0.name = "Boy";
mcImage1.name = "Girl";
mcImage2.name = "Woman";
mcImage3.name = "Man";
var ArrayNamesOfMC:Array = new Array();
var i:int = 3;
while (i >= 0)
{
ArrayNamesOfMC.push(MovieClip(this["mcImage" + i]).name);
i--;
}// end while
The following may not be relevant in your case as I'm not quite sure what the purpose of your application is, but this is probably a better approach:
var sprites:Vector.<Sprite> = new Vector.<Sprite>();
var names:Vector.<String> = new <String>["Boy", "Girl", "Woman", "Man"];
for (var i:uint = 0; i < names.length; i++)
{
var sprite:Sprite = new Sprite();
sprite.name = names[i];
sprites.push(sprite);
}// end for
Disregard this if it is not applicable in your case.
this should do the trick:
var _movieClip:MovieClip = ("mcImage" + i) as MovieClip;
ArrayNamesOfMC.push(_movieClip.name);
Taurayi's answer is an interesting technique that I didn't know about.
Personally I would recommend restructuring your code to put all the movieclips in an array, like so:
var mcImages:Array = new Array();
for (var i = 0; i < 4; i++) {
mcImages.push(new MovieClip);
}
mcImages[0].name = "Boy";
trace(mcImages[0].name);
Incidentally, your while loop was constructed incorrectly. You need a condition in the parentheses and then do the decrement inside the loop. But with all your movieclips in an array then you can use this much simpler approach to loop through all of them:
for each (var mc in mcImages) {
trace(mc.name);
}

How to Create Sound Object Dynamically

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.