# Want to build a "twinkling" starfield

**URL:** <https://forum.kirupa.com/t/want-to-build-a-twinkling-starfield/209131>\
**Category:** programming\
**Created:** [December 6, 2006, 4:41am UTC](https://forum.kirupa.com/t/want-to-build-a-twinkling-starfield/209131 "2006-12-06T04:41:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaoti](https://avatars.discourse-cdn.com/v4/letter/c/e47c2d/32.png) [@chaoti](https://forum.kirupa.com/u/chaoti)\
**Post date:** [December 6, 2006, 4:41am UTC](https://forum.kirupa.com/t/want-to-build-a-twinkling-starfield/209131/1 "2006-12-06T04:41:09Z")

</div>

I have used the following code to build a starfield:

starfield.star.\_visible = 0;  
initx = starfield.\_x;  
inity = starfield.\_y;

\_root.onEnterFrame = function() {  
with (starfield) {  
\_x += (horizfader.\_x-horizfader.initx)/15;  
\_y += 2  
// loop the starfield  
if (\_x\>initx+1100) {  
\_x = initx;  
}  
if (\_x\<initx) {  
\_x = initx+1100;  
}  
if (\_y\>inity+400) {  
\_y = inity;  
}  
if (\_y\<inity) {  
\_y = inity+400;  
}  
}  
};  
generate.onRelease = function() {  
for (i=1; i\<=2000; i += 4) {  
for (a=0; a\<=3; a++){  
duplicateMovieClip(starfield.star, “star”+(i+a), i+a);  
if (i%7 == 0) {  
starfield[“star”+(i+a)].\_xscale = 40;  
starfield[“star”+(i+a)].\_yscale = 40;  
}  
}  
starfield[“star”+i].\_x = Math.floor(Math.random()\*1100);  
starfield[“star”+i].\_y = Math.floor(Math.random()\*400);  
starfield[“star”+(i+1)].\_x = starfield[“star”+i].\_x-1100;  
starfield[“star”+(i+1)].\_y = starfield[“star”+i].\_y;  
starfield[“star”+(i+2)].\_x = starfield[“star”+i].\_x-1100;  
starfield[“star”+(i+2)].\_y = starfield[“star”+i].\_y-400;  
starfield[“star”+(i+3)].\_x = starfield[“star”+i].\_x;  
starfield[“star”+(i+3)].\_y = starfield[“star”+i].\_y-400;  
}  
};

NOW, how do i make those stars twinkle randomly. I am betting it involves the math class and alpha class, however i am relativley still new to actionscript. any suggestions?
