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));
}
Related
I have a Class called RenderingImage which is a bitmap like so:
[Embed(source="images/thumb.png")]
var RenderingImage:Class;
What I am trying to do is change the bitmap image in a loop.
for (var i:String in homes)
{
var renderingImg:Bitmap = new RenderingImage();
renderingImg.y = 50 + spaceY;
renderingImg.x = 0 + spaceX;
renderingImg.width = 320;
renderingImg.height = 185;
home.addChild(renderingImg);
}
Is there anyway to change the source that I put when defining the RenderingImage Class inside this loop?
You may be better off not embedding your image assets in your project. Regardless of how you store the images, you simply need to point to the new image when looping.
var images:Array = [
"https://cdn1.iconfinder.com/data/icons/freeline/32/home_house_real_estate-64.png",
"https://cdn2.iconfinder.com/data/icons/cute-tech-icon-set-1/512/Home-64.png",
"https://cdn3.iconfinder.com/data/icons/watchify-v1-0-80px/80/home-80px-64.png"
]
for (var i:int = 0; i < images.length; i++) {
var house:Loader = new Loader();
house.load(new URLRequest(images[i]));
addChild(house)
house.x = 65*i;
}
Alternatively, if you're heartset on only using explicit Bitmap objects, you could reference the loaded BitmapData after loading.
import flash.events.Event;
import flash.display.Loader;
import flash.display.Bitmap;
var images:Array = [
"https://cdn1.iconfinder.com/data/icons/freeline/32/home_house_real_estate-64.png",
"https://cdn2.iconfinder.com/data/icons/cute-tech-icon-set-1/512/Home-64.png",
"https://cdn3.iconfinder.com/data/icons/watchify-v1-0-80px/80/home-80px-64.png"
]
var bitmaps:Array = [];
var loadedCount:int = 0;
for (var i:int = 0; i < images.length; i++) {
var loader:Loader = new Loader();
loader.load(new URLRequest(images[i]));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
}
function loaded(e:Event):void {
bitmaps.push(e.currentTarget.loader.content);
loadedCount++;
if (loadedCount == images.length) {
for (var i:int = 0; i < images.length; i++) {
var houseBmp:Bitmap = new Bitmap();
houseBmp.bitmapData = bitmaps[i].bitmapData;
houseBmp.x = 65*i;
addChild(houseBmp);
}
}
}
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);
I am trying to create a vector from unknown class, but it fails, any ideas about how to do it?
This is what i tried:
var vector:Vector = new Vector(); // throw exception
function base():void{
var vector:Vector.<String> = createVector(String);// throw classCastException
}
function createVector(cls:Class):*{
var array:Array = new Array();
for(var i:int = 0; i < 10; i++){
var element:cls = new cls();
array.push(element);
}
return Vector(array);
}
Vector is expecting a parameter type so you can't do this like you want, but using getQualifiedClassName to get class info you can construct a string that will enable you to call the Vector. constructor using getDefinitionByName :
Ex.
// get class parameter name
var className:String=getQualifiedClassName(String);
// get the Vector class object for the given class
var o:Object=getDefinitionByName("__AS3__.vec::Vector<"+className+">");
// call the constructor
var v:*=o.prototype.constructor(["hello", "world"]);
So your function can be written as:
public function createVector(cls:Class):*{
var cn:String = getQualifiedClassName(cls);
var o:Object = getDefinitionByName("__AS3__.vec::Vector.<"+cn+">");
var array:Array = [];
for(var i:int = 0; i < 10; i++){
var element:* = new cls();
array.push(element);
}
return o.prototype.constructor(array);
}
live example at wonderfl:
http://wonderfl.net/c/pkjs
Based on #Patrick answer I found a working solution.
Check it out:
function createVector(cls:Class):*{
var className:String = getQualifiedClassName(cls);
var vectorClass:Class = getDefinitionByName("__AS3__.vec::Vector.<"+className+">") as Class;
var vector:* = new vectorClass(10);
for(var i:int = 0; i < 10; i++){
var element:MyClass = new cls(); // may be Object or Object extends cls
vector[i] = element;
}
return vector;
}
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).
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);
}