A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: swapChildren - referencing (targetting) items..?

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    5

    swapChildren - referencing (targetting) items..?

    Bit of a problem I'm stuck on here learning AS3... Any help would be fantastic

    My doc class (Main.as) calls and builds a few graphics (from DoBuild.as) and some text (from mainText.as).

    I'm trying to target the dynamic text with a listener (in the MainText.as) and swap the graphics when clicked - trouble being that I can't seem to reference them back at all (i.e stage.swapChildren(doBuild.Navigation,doBuild.Navi gation2).

    Can anyone shed light on this?

    Main.as

    Code:
    package {
     import flash.display.Sprite;
     import flash.display.MovieClip; 
     import flash.display.*
     public class Main extends Sprite {
      public function Main():void {
       var doBuild = new DoBuild(this);
       addChild(doBuild);
     
       var mainText = new MainText(this);
       addChild(mainText);
     
      }
     }
    }
    MainText.as:
    Code:
    package {
     import flash.display.MovieClip; 
     import flash.text.*;
     import flash.events.*; 
     public class MainText extends MovieClip {
     
     public function MainText(stage:Main) { 
      var css:StyleSheet = new StyleSheet();
      var body:Object = new Object();
      body.fontFamily = "Arial";
      body.textIndent = 0;
      css.setStyle("body", body);
       var txtFld:TextField = new TextField();
       txtFld.x =830;
       txtFld.y =10;   
       txtFld.width = 100;
       txtFld.multiline = true;
       txtFld.wordWrap = true;
       txtFld.selectable = false;   
       txtFld.embedFonts = true;
       txtFld.autoSize = TextFieldAutoSize.LEFT;
       txtFld.styleSheet = css;
       txtFld.htmlText = "<body>";
       txtFld.htmlText += "<span class='heading'>Text</span>";
       txtFld.htmlText += "</body>";
       addChild(txtFld);
       txtFld.addEventListener(MouseEvent.CLICK, clickHandler);
        function clickHandler(event:Event):void
        {
         stage.swapChildren(DoBuild.Navigation,DoBuild.Navigation2);
     
        }   
    }
    }
    }
    DoBuild.as

    Code:
    package {
     import flash.display.Sprite; 
     public class DoBuild extends Sprite {
     public function DoBuild(stage:Main) {
       var Navigation2:Sprite=new rightPanel2();
       addChild(Navigation2);
       trace("NAV2 = " + getChildIndex(Navigation2));
       Navigation2.x=832;
       Navigation2.y=2.4; 
       var Navigation:Sprite=new rightPanel();
       addChild(Navigation);
       trace("NAV = " + getChildIndex(Navigation));
       Navigation.x=832;
       Navigation.y=2.4;   
      } 
     }
    }

    Pt2

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Two problems, the easy one - you're targetting swapChildren on stage, it should be:

    swapChildren(doBuild.Navigation,doBuild.Navi gation2)

    However, .Navigation and .Navigation2 are variables that only exist when the DoBuild constructor is run, afterwards those objects become anonymous...to fix it you'll need to store those in publicly accessable variables:

    PHP Code:
    package {

        
    import flash.display.Sprite

        public class 
    DoBuild extends Sprite {
        
        public var 
    Navigation:Sprite;
        public var 
    Navigation2:Sprite;
        
            public function 
    DoBuild(stage:Main) {
                
    Navigation2 = new rightPanel2();
                
    addChild(Navigation2);
                
    trace("NAV2 = " getChildIndex(Navigation2));
                
    Navigation2.832;
                
    Navigation2.2.4
                
    Navigation = new rightPanel();
                
    addChild(Navigation);
                
    trace("NAV = " getChildIndex(Navigation));
                
    Navigation.x=832;
                
    Navigation.y=2.4;   
            } 
        }


  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Same goes for doBuild. It's defined in the Main constructor, but you have no references to it in MainText. You could pass such a reference to MainText in the constructor, or otherwise set a property in your MainText to hold your DoBuild instance.

  4. #4
    Junior Member
    Join Date
    Jun 2008
    Posts
    5
    Excellent - apologies for not getting back to you sooner but many thanks for your help here!

    Pt2

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