Movieclip names - actionscript-3

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;

Related

how addChild is being used in below AS3 code?

For addChild(iconObject) (second line inside function startAmoebaAttack()), can anyone explain me where will iconObject be added in? because it doesn't have any object or array before addChild like iconObject.addChild(newSoldier);
private var iconObject:Sprite;
public function startAmoebaAttack() {
iconObject = new Sprite();
addChild(iconObject);
createSoldierIcon();
}
public function createSoldierIcon(){
soldierIcon = new Array();
for(var i:uint = 0; i<soldierLeft; i++){
var newSoldier:SoldierIcon = new SoldierIcon();
newSoldier.x = 65 + i *24;
newSoldier.y = 590;
iconObject.addChild(newSoldier);
soldierIcon.push(newSoldier);
}
}
It will automatically run it in this's context (same as this.addChild(iconObject)). In your case this is descendant of DisplayObjectContainer .

change movieclip instance name in runtime

I'm trying change movieclip instance name in runtime, with name property but I have error
Error #2078: The name property of a Timeline-placed object cannot be modified.
I tried to create new movieclip in runtime asign my old movie clip and change name property but I have same error...
and is any way to change instance name of movieClip in runtime?
So far the only workaround I've found is to use an array:
import flash.display.MovieClip;
import flash.geom.ColorTransform;
var t:Boolean; // for toggle function
var square: Array = new Array();
var changeColor: ColorTransform = new ColorTransform();
for (var i: int = 0; i < 5; ++i) {
var rect: MovieClip = new MovieClip();
rect.graphics.beginFill(0xaaaaaa);
rect.graphics.drawRect(10, 10, 50, 50)
addChild(rect);
rect.x = 75 * (i + 1);
rect.y = 100;
square.push(rect)
}
// This toggles the middle square up and down, and gray to red.
square[2].addEventListener(MouseEvent.CLICK, toggle);
function toggle(event: MouseEvent): void {
if (!t) {
changeColor.color = 0xff00000;
square[2].transform.colorTransform = changeColor;
square[2].y = 50;
} else {
changeColor.color = 0xaaaaaa;
square[2].transform.colorTransform = changeColor;
square[2].y = 100;
}
t=!t;
}

How can we control behavior of individual movie clips added randomly to stage from an array [as3]

The code below adds a movie clip to the stage randomly from an array of movie clips. Once a movie clip is added to stage it moves horizontally on an x axis by echoing a corresponding sound clip. But my problem is, only the same sound clip is being played for any of the movie clips added on stage. The reason why its happening is of course because of the line:
var mc:MovieClip = new enemies[printArray[p]];
Can anyone please tell me how i can individually control the behavior of each movie clip that falls within "var mc". Instead of writing something like:
if(mc.x <= -100) { Enem1Timer.stop()} else if(mc.x <= 570){ Enem1Timer.start() }
is there a way to write like:
if(Enem1.x <= -100).....
Any help is most welcome. Thanx in advance.
import flash.utils.Timer;
import flash.display.MovieClip;
var sound1:Letter1 = new Letter1();
var sound2:Letter2 = new Letter2();
var sound3:Letter3 = new Letter3();
var sound4:Letter4 = new Letter4();
var sound5:Letter5 = new Letter5();
var sound6:Letter6 = new Letter6();
var printArray:Array = [0, 1, 2, 3, 4, 5];
var enemies:Array = [Enem1, Enem2, Enem3, Enem4, Enem5, Enem6];
for(var n:int = 0; n <= 6; n++)
{
var randNo:int = int(Math.random() * 6);
printArray.push(randNo);
}
var Enem1Timer:Timer = new Timer(1000, 1);
Enem1Timer.addEventListener(TimerEvent.TIMER, playA1);
function playA1(e:TimerEvent):void
{sound1.play();}
for(var p:int = 0; p < printArray.length; p++)
{
trace(printArray[p]);
var mc:MovieClip = new enemies[printArray[p]];
addChild(mc);
mc.x = 600; mc.y = 200;
stage.addEventListener(Event.ENTER_FRAME, loop)
function loop(e:Event){
if(mc.x <= -100) { Enem1Timer.stop()} else if(mc.x <= 570){ Enem1Timer.start() }
mc.x -= 3;
}}
if you mcs are already created in the array (not null) then you should remove "new" since you're accessing it, not creating it:
var mc:MovieClip = enemies[p] as MovieClip;

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.

How to pass a variable into an Event.COMPLETE function?

I am running a loop to pull thumbs into a containing movieclip from an xml list. What I want to do is have the thumb's parent movieclip fade in after they are done loading, but I can't figure out how to reference the parent once it's loaded.
My code(which currently doesn't work the way I want it):
var vsThumb:articleBox;
var currentarticleX:Number = 0;
var articleLinkURL:String;
var articleImageURL:String;
var articleText:String;
var vsThumbLoader:Loader;
var next_x:Number;
next_x = 9;
var thumbAlphaTween:Tween;
var articlevsThumb:Array = new Array();
function loadarticleHeadlines():void
{
for (var i:int = 0; i < egarticleXml.articlelist.articleitem.length(); i++)
{
vsThumb = new articleBox();
vsThumb.alpha = 0;
vsThumbLoader = new Loader();
vsThumbLoader.load(new URLRequest(egarticleXml.articlelist.articleitem[i].articlethumbnail));
articleListContainter.addChild(vsThumb);
vsThumb.articleImage.addChild(vsThumbLoader);
vsThumb.articleTitle.text = egarticleXml.articlelist.articleitem[i].articletitle;
titleAutosize(vsThumb.articleTitle);
vsThumb.x = next_x;
next_x += 260;
articlevsThumb[i] = vsThumb;
vsThumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showBox);
vsThumb.clickBtn.buttonMode = true;
}
function showBox(event:Event):void
{
thumbAlphaTween = new Tween(articlevsThumb[i],"alpha",None.easeNone,0,1,.25,true);
}
}
So how do I refer back to the loader's parent so I can fade in the whole movieclip? Can I pass a variable into the showBox function?
Don't use nested functions. They tend to make things more complicated.
i will always have the end value (articleitem.length()-1) in all of the event handlers you create, because its scope is the outer function, loadarticleHeadlines (it will increase by 1 on every iteration). That's probably why your code doesn't work.
The event will be fired on the loaderInfo of your loader, so you can find the loader's parent by using event.target.loader.parent:
function loadarticleHeadlines() : void
{
for (var i:int = 0; i < egarticleXml.articlelist.articleitem.length(); i++)
{
vsThumb = new articleBox();
vsThumb.alpha = 0;
vsThumbLoader = new Loader();
vsThumbLoader.load(new URLRequest(egarticleXml.articlelist.articleitem[i].articlethumbnail));
articleListContainter.addChild(vsThumb);
vsThumb.articleImage.addChild(vsThumbLoader);
vsThumb.articleTitle.text = egarticleXml.articlelist.articleitem[i].articletitle;
titleAutosize(vsThumb.articleTitle);
vsThumb.x = next_x;
next_x += 260;
articlevsThumb[i] = vsThumb;
vsThumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showBox);
vsThumb.clickBtn.buttonMode = true;
}
}
function showBox(event:Event):void
{
thumbAlphaTween = new Tween(event.target.loader.parent,"alpha",None.easeNone,0,1,.25,true);
}
You don't need to pass a variable to your showBox, use the target property of the Event to retrieve the Loader:
function showBox(event:Event):void
{
var li:LoaderInfo=LoaderInfo(event.target);
// be nice remove your listener when your are done
li.removeEventListener(Event.COMPLETE, showBox);
var ldr:Loader=li.loader; // here is your loader
// do whatever you want with loader
thumbAlphaTween = new Tween(articlevsThumb[i],"alpha",None.easeNone,0,1,.25,true);
}