I have a spark element, and I want to change the color of this element, then draw this element to a .png.
However, when I do these things one after another, the picture is taken before the color is changed.
Example:
//color is previously red
rectColor.color=0x000000;
trace("color set");
takeScreenShot();
private function takeScreenShot():void{
trace("screenshot taken");
//stuff
}
This prints
color set
screenshot taken
However, the "screenshot" image appears with the rectangle as red. The color has not changed.
Is this asynchronous? Does the color not actually update until the next frame?
The update won't happen until the next frame has been constructed. You're taking a screenshot of one frame and the changes you made won't be active until the next frame.
rectColor.color=0x000000;
trace("color set");
this.addEventListener( Event.FRAME_CONSTRUCTED, frameConstructedHandler );
private function frameConstructedHandler( e:Event ):void {
this.removeEventListener( Event.FRAME_CONSTRUCTED, frameConstructedHandler );
takeScreenShot();
}
private function takeScreenShot():void{
trace("screenshot taken");
//stuff
}
That will delay the screenshot until the following frame has finished construction. That may or may not be too soon (I can't remember the order of GUI events and I don't have time to look it up). If that doesn't work. swap FRAME_CONSTRUCTED
with ENTER_FRAME
and it should work. Regardless of which event needs to be used, that is your problem here