First from the components drag the TextArea component to the stage. Then give it an instance name txt. Then begin hacking.
var lines:int = 5;
var star:String = "*";
var space:String = " ";
var i:int;
var j:int;
var k:int;
var str:String="";
function init():void
{
/* Upper Triangle */
for( i=0; i< lines; i++ ) {
j = i;
str = "";
for( k=0; k<5-i; k++ ) {
str += space;
}
while( j != 0 ) {
str += star + space;
j--;
}
txt.text += str + "\n";
}
/* Lower Triangle */
for( i=lines; i!=0; i-- ) {
j = i;
str = "";
for( k=0; k<5-i; k++ ) {
str += space;
}
while( j != 0 ) {
str += star + space;
j--;
}
txt.text += str + "\n";
}
}
init();
//OutputThe idea behind this
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Its easier to think with lower triangle taken first. For the time being lets consider that we need only the lower triangle. i.e, say there will be 5 lines, and in the first line there will be 5 stars, in the second line there will be 4 stars and so on.
Think that if we simply put a for loop with the number of lines (5) and decrement it, in the first iteration we get 5, second iteration 4, (yeah, I know everyone knows that!), which is the number of stars we need to show per line. Now what we need is to actually display those number of stars.
The idea is concatenate those number of stars into a string and display them. Now what we get is a right angled triangle. That's because we need to concatenate the correct space along with the string. That's what the for loops in line number 16, and 30 does.
Now we have required triangle. You can easily make a recursive version if you would like.