all groups > flash actionscript > december 2006 >
You're in the

flash actionscript

group:

how to change max in "for" statement


how to change max in "for" statement aniebel
12/9/2006 7:23:48 PM
flash actionscript:
Hi, I am trying to set up a snow scene using the code below but I'd like to
alter it so that it looks more natural... ie: a few flakes begin to fall,
increasing as time progresses. Is there a way to increment the number used in
the "for" statement while it's looping?



init = function () {
width = 660; // pixels
height = 435; // pixels
max_snowsize = 3; // pixels
snowflakes = 10; // quantity
for (i=0; i<snowflakes; i++) {
t = attachMovie("snowflake", "snowHolder"+i, i);
t._alpha = 20+Math.random()*60;
t._x = -(width/2)+Math.random()*(1.5*width);
t._y = -(height/2)+Math.random()*(1.5*height);
t._xscale = t._yscale=50+Math.random()*(max_snowsize*10);
t.k = 1+Math.random()*10;
t.wind = -1.5+Math.random()*(1.4*3);
t.onEnterFrame = mover;
}
};
mover = function () {
this._y += this.k;
this._x += this.wind;
if (this._y>height+10) {
this._y = -20;
}
if (this._x>width+20) {
this._x = -(width/2)+Math.random()*(1.5*width);
this._y = -20;
} else if (this._x<-20) {
this._x = -(width/2)+Math.random()*(1.5*width);
this._y = -20;
}
};
this.createEmptyMovieClip("snowHolder_mc", this.getNextHighestDepth());
init();
Re: how to change max in "for" statement kglad
12/9/2006 9:10:47 PM
snowA contains the number of snowflakes to add every 3000 milliseconds:



tl = this;
init = function (flakeNum) {
width = 660;
// pixels
height = 435;
// pixels
max_snowsize = 3;
// pixels
if (!snowflakes) {
snowflakes = flakeNum;
} else {
snowflakes += flakeNum;
}
if (!nextStartI) {
nextStartI = 0;
}
for (i=nextStartI; i<snowflakes; i++) {
t = tl.attachMovie("snowflake", "snowHolder"+i, i);
t._alpha = 20+Math.random()*60;
t._x = -(width/2)+Math.random()*(1.5*width);
t._y = -height-Math.random()*height;
t._xscale = t._yscale=50+Math.random()*(max_snowsize*10);
t.k = 1+Math.random()*10;
t.wind = -1.5+Math.random()*(1.4*3);
t.cacheAsBitmap = true;
}
nextStartI = snowflakes;
delete tl.onEnterFrame;
tl.onEnterFrame = mover;
};
mover = function () {
trace(snowflakes);
for (var i = 0; i<snowflakes; i++) {
var sf = tl["snowHolder"+i];
sf._y += sf.k;
sf._x += sf.wind;
if (sf._y>height+10) {
sf._y = -20;
}
if (sf._x>width+20) {
sf._x = -(width/2)+Math.random()*(1.5*width);
sf._y = -20;
} else if (sf._x<-20) {
sf._x = -(width/2)+Math.random()*(1.5*width);
sf._y = -20;
}
}
};
this.createEmptyMovieClip("snowHolder_mc", this.getNextHighestDepth());
snowA = [10, 5, 8, 5, 8, 7, 5, 8, 5, 8, 7];
index = 0;
init(snowA[0]);
changeSnowI = setInterval(changeSnowF, 3000);
function changeSnowF() {
index++;
if (index<snowA.length) {
init(snowA[index]);
} else {
clearInterval(changeSnowI);
}
}
Re: how to change max in "for" statement aniebel
12/9/2006 9:24:22 PM
Re: how to change max in "for" statement kglad
12/9/2006 9:27:24 PM
Re: how to change max in "for" statement Dan_pc
12/9/2006 11:15:20 PM
I was inspired by your post and made this.... Hope it helps..

Enjoy! :)

AddThis Social Bookmark Button