the reason is that your main.as does not have a valid view.
try passing the mxml view as an argument to it and call it's addChild
function;
e.g.
public function createSprite2():void{
var main:Main = new Main();
main.createSprite(this);
}
and in main.as
public function createSprite(con:UIComponent) : void{
var visual:Visual = new Visual();
var ref:UIComponent = new UIComponent();
con.addChild(ref);
ref.addChild(visual);
trace('called from Main.as');
}
and it should work...
jrag0n wrote On 01/21/2008 08:30 AM:
>
> Hello,
>
> When I try to draw a circle on the screen by calling a script that's
> in the mxml, it works
> fine:
>
> <mx:Button label="Button" click="createSprite1();" right="10" top="10"/>
> and
> public function createSprite1():void
> {
> var visual:Visual = new Visual();
> var ref:UIComponent = new UIComponent();
> addChild(ref);
> ref.addChild(visual);
> trace('called from mxml');
> }
>
> But if I try to call it from a class, it is called (I can tell because
> of the trace output) but it
> doesn't draw the circle on the screen.
>
> Here's my rough code that doesn't work so far:
>
> <mx:Button label="Button" click="createSprite2();" right="10" top="40"/>
> public function createSprite2():void
> {
> var main:Main = new Main();
> main.createSprite();
> }
>
> And then my Main.as file just has the same code:
>
> public function createSprite() : void
> {
> var visual:Visual = new Visual();
> var ref:UIComponent = new UIComponent();
> addChild(ref);
> ref.addChild(visual);
> trace('called from Main.as');
> }
>
> As I said, I know this class is being called because I can see the
> trace output. Any
> suggestions why this isn't working for me?
>
> Thanks for any help,
> Jon
>
>