I needed a Function to draw an arrow facing either up, down,left, or right and returned as either a Shape, Sprite, or MovieClip. This is what i came up with. My question is: could it be made more concise?
var myTriangleSprite:Sprite =drawTriangle(10,“left”,0xffffff,“Sprite”);
function drawTriangle(triangleHeight:Number=10,dir:String=“up”,color:uint=0xffffff,type:String=“MovieClip”){
var upArray:Array = [(triangleHeight/2),0,triangleHeight,triangleHeight,0,triangleHeight];
var rightArray:Array = [0,0,triangleHeight,(triangleHeight/2),0,triangleHeight];
var downArray:Array = [0,0,triangleHeight,0,(triangleHeight/2),triangleHeight];
var leftArray:Array = [triangleHeight,0,triangleHeight,triangleHeight,0,(triangleHeight/2)];
var a:Array;
var graphics:Graphics ;
if (type == "MovieClip"){
var triMC:MovieClip= new MovieClip();
triMC.name ="triangleMC";
graphics=triMC.graphics;
addChild(triMC);
}
if (type == "Sprite"){
var triSP:Sprite = new Sprite();
triSP.name="triangleSprite";
graphics=triSP.graphics;
addChild(triSP);
}
if (type == "Shape"){
var triSH:Shape = new Shape();
triSH.name="triangleShape";
graphics=triSH.graphics;
addChild(triSH);
}
switch (dir){
case "up":
a=upArray;
break;
case "right":
a=rightArray;
break;
case "down":
a=downArray;
break;
case "left":
a=leftArray;
break;
}
graphics.beginFill(color);
graphics.moveTo(a[0],a[1]);
graphics.lineTo(a[2],a[3]);
graphics.lineTo(a[4],a[5]);
graphics.lineTo(a[0],a[1]);
if (type == "MovieClip"){
return triMC;
}
if (type == "Sprite"){
return triSP;
}
if (type == "Shape"){
return triSH;
}
}