views:

116

answers:

3

I am trying to trace stage.name in child view after addChild:

import flash.events.Event;
import flash.text.TextField;
public class TestView extends TextField {
    public function TestView() {
        addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
    }

    private function handleAddedToStage(event : Event) : void {
        this.text = "TEXT ON TEXTFIELD";
        trace("Stage is"+stage.name);
        trace("Stage width"+stage.stageWidth);
    }
}

stage.name is already overrided: in Main:

import flash.display.Sprite;
public class Main extends Sprite {
    public function Main() {
        addChild(new TestView());
    }
    override public  function get name():String {
        return "MAINSTAGE";
    }
}

But getting null in log: tail: flashlog.txt: file truncated Stage isnull

A: 

I don't see why the stage returns null, but overriding name in the Main class won't effect the stage.name property. The stage variable of a display object points to the globally unique Stage object of the flash movie which is different from the Main instance. In AS3 there is a unique flash.display.Stage instance which is accessible from any display object that is currently on the display list (added using addChild methods). If you want to access your main Sprite instance, use displayObject.root instead.

Amarghosh
A: 

You can access stage from anywhere inside your flex application with Application.application.stage

Yeti
A: 

Not quite clear on what you're trying to accomplish, but from a quick test I ran, stage is not null, but stage.name is null. I'm using Flex. That's probably just because we haven't given it a name.

<?xml version="1.0" encoding="utf-8"?>
<s:Application
    xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx">

    <s:Button id="button"
        addedToStage="trace(button.stage);trace(button.stage.name)"
        click="trace(button.stage.name)"
        horizontalCenter="0" verticalCenter="0"/>

</s:Application>  

Output:

[object Stage]
null
null

Overriding get name in Main won't affect this.

viatropos