PDA

View Full Version : Randomly Generated Background


rustyboy
03-12-2007, 07:11 PM
Hi,

I am making a side scrolling shooter in Flash, as you do. It is set in space and I dont want to do the lazy thing by making a looping background with stars in it.
I have taken some of the code from this tutorial and am adapting it to my needs.
http://www.actionscript.org/resources/articles/549/1/Floating-Background/Page1.html
But the section where the float MC is randomly placed on the layer needs to be altered to set it at a random height just off the right hand side of the screen. How would i go about creating a random value for the _y value between the upper and lower stage heights?
Also, I cannot remember where i saw it or whether i just dreamt it but how can you apply AS code to an attachMovie instance?

Thanks very much.

misterdeak
03-24-2007, 11:08 PM
I didn't do the tut, but here's how I would do it (if I understand what it is you want):
//
starNum = 0;

function makeStars(amount)
{
for(var i: Number = 0; i < amount; i++)
{
var star = starLayer.attachMovie("star", "star_" + starNum, starNum);
starNum++;
star._x = random(Stage.width);
star._y = random(Stage.height);
}
}
makeStars(30);
//

Hope this helps...