2

so I have a class:

package
{
    public final class Main extends Sprite
    {
        private var TextHolder:Sprite = new Sprite();

        public function Main():void
        {
            spawnHolder();
        }
        private function spawnHolder():void
        {
            TextHolder.graphics.beginFill(0xFFFFFF);
            TextHolder.graphics.drawRect(0, 0, 100, 80);
            TextHolder.graphics.endFill();
            stage.addChild(TextHolder);
        }
    }
}

but the problem is.. I do not see anything on the stage.. This Main Class is the document class btw.

I'd appreciate it very much if anyone could help me out! Thanks in advance.

~Gg

GilaGuy
  • 23
  • 1
  • 5
  • 1
    Just because it says `[object Sprite]` doesn't mean that it's on the display list. It just means that it's an object of the type `Sprite`. – Simon Forsberg Nov 07 '12 at 02:09
  • Ran your code in FlashDevelop with a black background and it works fine. It could be something in your environment. I suggest debugging and checking if it actually gets added to the stage and checking the colors. – Barış Uşaklı Nov 07 '12 at 02:58

2 Answers2

1

Stage is not available until the display object has been added to the stage. It will be null until that happens.

package
{
    public final class Main extends Sprite
    {
        private var TextHolder:Sprite = new Sprite();

        public function Main():void
        {
            this.addEventListener(Event.ADDED_TO_STAGE,spawnHolder);
        }
        private function spawnHolder():void
        {
            TextHolder.graphics.beginFill(0xFFFFFF);
            TextHolder.graphics.drawRect(0, 0, 100, 80);
            TextHolder.graphics.endFill();
            stage.addChild(TextHolder);
        }
    }
}
The_asMan
  • 6,364
  • 4
  • 23
  • 34
  • Better if you test for `if(stage)` before adding the event listener, and call `spawnHolder` directly if it evaluates to true: `if(stage) spawnHolder() else this.addEventListener(Event.ADDED_TO_STAGE,spawnHolder);` – Jude Fisher Nov 07 '12 at 14:21
  • @JcFx you also left out that the event listener isn't being removed :) – The_asMan Nov 07 '12 at 15:09
  • Heh. Pedants anonymous will throw me out of their inner circle ... :) – Jude Fisher Nov 07 '12 at 15:20
  • Heh the "if(stage) init;....." was what I tried right b4 seeing this post! Thanks anyways guys! :D – GilaGuy Nov 10 '12 at 02:37
0

Try to change a color, probably your document background is also white. Do not name vars with capital like TextHolder, use textHolder instead. Try to do all work after stage initialized.

Sergey Dmitriev
  • 454
  • 3
  • 13