Well I have been very busy lately and sadly away from flash and totally immersed in Blender3D for a current project. I love Blender to bits but missing flash and actionscript badly. Anyway Blender has some great effects and one is the hairs creation from particles, which gave me an idea for a little test in actionscript to see how simple it would be to create a grass or hair strand effect in flash. It proved to be incredibly easy. I will explain here.
All I have done is created a movie clip in flash with 7 frames, on each of these frames I have drawn a blade of grass by using the stroke tool and assigning a green color. I have then exported this for action script and in the class file for this I have assigned a random number (between 1 & 7) as a variable and in the constructor told the movie clip to stop at this frame.
-
private var num:int = Math.ceil(Math.random()*7);
-
-
public function Hair()
-
{
-
this.addEventListener(Event.ADDED_TO_STAGE, added);
-
}
-
private function added(e:Event):void
-
{
-
this.removeEventListener(Event.ADDED_TO_STAGE, added);
-
this.gotoAndStop(num);
-
}
In the main document class I have created a for loop and created 3000 instances of the hair movie clip and given them a random place on the stage, making sure that if the movie clip passes the 'bounds' of the stage it is moved to another random place on the stage.
-
private function init():void
-
{
-
for(var i:int = 0; i <2000; i++){
-
var h:Hair = new Hair();
-
randomPos(h);
-
h.x = xPos;
-
h.y = yPos;
-
h.filters = [drop];
-
this.addChild(h);
-
}
-
}
-
private function randomPos(object:Object):void
-
{
-
xPos = (Math.random()*stage.stageWidth);
-
yPos = (Math.random()*stage.stageHeight);
-
if(xPos> stage.stageWidth || xPos <0){
-
randomPos(object);
-
}
-
else if(yPos> stage.stageHeight || yPos <0 + (object.height + 30)){
-
randomPos(object);
-
}
-
}
Now this will just look like a bunch of green lines on the stage if it is published now so to finish it off we just add a drop shadow filter to each blade to give them depth and presto! Totally pointless but pretty sweet grass. Next to add some interactivity?!
Get the source here



![Validate my RSS feed [Valid RSS]](http://www.ultravisual.co.uk/blog/images/valid-rss.png)


Hey, this is quite cool, looks nice! I’ve been working in a grass movement algorithm ages ago, it is quite similar with this one here you’ve done using similar filters, the main diference is that I’m drawing the leaves completely using the bimap API every frame to achieve the moviment, you may like it: http://www.yoambulante.com/en/labs/grass.php
cheers!