PDA

View Full Version : using variables created inside a function outside the function?


invulse
06-09-2008, 10:02 PM
I am trying to use a variable I am creating inside of a function, outside of that function, but I seem to have a scope issue when I do this.

My code is:

function drawlines(centerx, centery, radius, radians){
var radiansSoFar:Number = 0

var piechart:MovieClip = new MovieClip();
piechart.name = "piechart"
piechart.x = stage.stageWidth/2;
piechart.y = stage.stageWidth/2;


stage.addChild(piechart);
}

but I want to make a function outside of this which removes the variable piechart, but because im so unfamiliar with how flash names instances now I have no idea how to do this.

It works if I define var piechart:MovieClip = new MovieClip() outside of the function, but this isnt very useful as I eventually need to create multiple movieclips dynamically and name them dynamically.

So basically how would make it so I would be able to use the code


function removeChart(chartname){
stage.removeChild(chartname)
}

to dynamically remove a movieclip I created in a function?

Sekhar
06-10-2008, 12:28 AM
Why not just return the clip from the function? E.g.,

function drawlines(centerx, centery, radius, radians):MovieClip {
...
return piechart;
}