A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Stage from within class

  1. #1
    Junior Member
    Join Date
    Aug 2008
    Posts
    16

    Stage from within class

    Hi, i have textfield myTxt on stage, and i have Room.as class file.
    Actionscript Code:
    package  {
       
        public class Room {

            public function Room() {
                // constructor code
                myTxt.text = "yo!";
            }

        }
       
    }

    I need to access objects on stage from within class.
    Thank you.

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    PHP Code:
    addEventListener (Event.ADDED_TO_STAGEonAddedfalse0true);

    private function 
    onAdded (e:Event) {
        
    trace (stage);


  3. #3
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    5Five555, this code will not work in this situation.
    First, the class that Gugis created is not a DisplayObject, since it doesn't extend any of the display object classes, so it can never be aaded to stage.
    Second, he didn't ask how to access stage. He wanted to access objects on stage, which were, most probably, created in FLash IDE.

    To access them you need to add an instance name to each object, then, pass stage as a constructor to your class, and then use getChildByName() method.

    It's one of several methods to do this, but I think it's the simples one. Give it a try.

  4. #4
    Senior Member
    Join Date
    Aug 2006
    Posts
    322

    Stage TextField from Class

    AscessFromClassAS3.as

    Actionscript Code:
    package {

        public class AscessFromClassAS3 {
            public var Txt:String;
            public function AscessFromClassAS3(myTxt:String):void {
                Txt=myTxt;
            }
            public function get access():String{
                return Txt;
            }
            public function set access(txtVal:String):void {
                Txt = txtVal;
            }
        }

    }

    Main TimeLine Code of AscessFromClassAS3.fla which will be in the same directory.

    Actionscript Code:
    var setTxt:AscessFromClassAS3 = new AscessFromClassAS3("Default Label   :   ");

    setTxt.access += "Hello ";
    setTxt.access +="Flash Class !";
    var split:String=":";
    var arr=[];
    arr=(setTxt.access.split(this["split"]))
    //myText.htmlText="<b>"+setTxt.access+"</b>";
    myText.htmlText=arr[0]+split+"<font color='#BF1E6F'><b>"+arr[1]+"</b></font>";

    Obviously the TextField will be on your Stage with an Instance Name myText

    Think You are Looking similar to this.

    Regards



    marlopax
    Last edited by marlopax; 03-04-2011 at 02:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center