PDA

View Full Version : best appoach random fading mcs



patrickjv
August 9th, 2007, 04:43 PM
I have an MC which runs continuously with a addEventListener. At random moments it creates MC2s which need to fade away in a random duration. Imagine it like "waterdrops" which fade away.

I currently have them being generated at random moments with

function addchildobject(nr){
obj[nr] = new obj_pj_raindrops();
addChild(obj[nr]);
}

So far so good. Now I'm looking for best approach to fade them away. "waterdrops" also need to get a
- random duration fading away
- random scale

Idea I had
1) Have MC do all the work. -> Add an extra objvars[] array to every obj[x] e.g.
obj[x].objvars[1] = 1; // used as a counter
obj[x].objvars[2] = 20+Math.floor(Math.random()*20) // total duration to fade out
obj[x].objvars[3] = Math.random()*200 // random scale

"x" is a number increased every time a new "waterdrop" is generated.
Then use a obj[x].objvars[1]++ to increment counter and as soon as it reaches obj[x].objvars[2] remove it by;

if(obj[x]){
if(obj[x].parent != null){
removeChild(obj[x]);
}
}

And enclose/add a for() loop in addEventListener, fading away all "waterdrops" detected still pressent on stage.

Problem is, I can't manage to get an array added to the generated "waterdrops". I know I probably can predefine it in frame 1 of that MC2 but hope there's a way to add them on-the-fly.

2) I also know I probably can do the scripting within "waterdrop" itself but I didn't manage to make it remove itself when done.

Anybody any idea how to add an array within an child by array, in ohter words 'nest arrays'?

Or any other suggestion?
t.i.a.
P

geoken
August 9th, 2007, 05:27 PM
Why don't you create the fade tween and set the scale in the function that creates the drops?

patrickjv
August 9th, 2007, 07:16 PM
You mean keyframe tween? Wont work. That won't allow a random duration.

Update;
I most need is to set an Array in obj[x] . I tried adding to frame one of MC2 array definition
var objvars:Array = new Array();

But doing a quick test

obj[1].objvars[1] = 20+Math.floor(Math.random()*20);

Results in Error

Cannot access a property or method of a null object reference.

Not sure why, order seems correct... generate obj[2], then add values to arrays within it. But for some reason it's not accepted.

Anybody know why?

patrickjv
August 9th, 2007, 07:36 PM
darn... did some deduction test. Created a blank MC linkage name zzz and added to this MC in first frame

var objvars:Array = new Array();
objvars[0] = 1;
objvars[1] = 20;

Then in root first frame I put

var child = new zzz();
addChild(child);
trace(child.objvars);

and in second frame I put

stop();
trace(child.objvars);

it traces

null
1,20

In other words, the moment the child is generated the Array is not available yet, but one frame later it ís. Darn that, I need it right away... any feedback from anybody?
P

geoken
August 9th, 2007, 07:44 PM
No, I mean the Tween Class. If you create and set the tween inside the same function that you add the drops to the stage then you don't need to reference the clips. Kind of like this;

function addchildobject(nr){
obj[nr] = new obj_pj_raindrops();
addChild(obj[nr]);
var fadeDur = 20+Math.floor(Math.random()*20)
var fade = new Tween(obj[nr], 'alpha', Regular.easeOut, 1, 0, fadeDur, true);
obj[nr].scaleX = Math.random()*200;
obj[nr].scaleY = obj[nr].scaleX;

}

patrickjv
August 9th, 2007, 09:17 PM
thats an interesting suggestion. Will check it out, couldn't find correct syntax yet.. above gives error.

Anyhow, I actually simplified situation. MC2 contains 2 rings being keyframe-tweened from 75% to 100% and 100% to 150% scale. I had in mind doing that by script as well but need to check if Tween Class can include that as well (with a random duration)

I actually had the thought/need of nested arrays before so probably will open a new thread to see if there's some feedback or posting about it. Could make several things easier for me.
P