A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: misunderstanding about "with"

  1. #1
    Senior Member
    Join Date
    Jul 2008
    Posts
    418

    misunderstanding about "with"

    i thought that what "with (myObj){statements}" does making myObj execute statements. however this seems to not be completely true:
    i have 2 vars with the same name: myVar
    one is in class mainClass, the other is declared in the with statements. when i change myVar within the with statements, and trace it, back in mainClass the change to the var myVar in mainClass is (also?) made.
    so what does with do really?

  2. #2
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    with (obj) {} is used to set properties on the object, not execute statements.

    example:

    PHP Code:
    with(obj) {
        
    alpha 0;
        
    visible false;
        
    0;
        
    0;


  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    With only affects properties and methods. For example, this doesn't work because var a is defined twice in the same namespace (you can't say m.var a = ""):

    PHP Code:
    var m:MovieClip = new MovieClip();
    var 
    a:String 'outside';

    with(m){
        var 
    a:String 'inside';
        
    trace(a);
    }

    trace(am.a); 
    This does work because you have created the property on mc before going into the with statement:

    PHP Code:
    var m:MovieClip = new MovieClip();
    var 
    a:String 'outside';

    m.= new String();

    with(m){
        
    'inside';
        
    trace(a);
    }

    trace(am.a); 

  4. #4
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    but when i do:
    PHP Code:
    var bb:int =5;
    with (myObj)
    {
    trace (bb);

    it gives: undefined.

  5. #5
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    The same just worked for me:

    PHP Code:
    var m:MovieClip = new MovieClip();
    var 
    a:String 'outside';

    with(m){
        
    trace(a);

    Are you using a custom class? Is it dynamic or does it already have a public param/method with the same name?

  6. #6
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    Post your FLA file - you must be missing something.

    Make sure myObj exists on the stage, otherwise it would work.

  7. #7
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    PHP Code:
    public class MainClass extends MovieClip
        
    {
            private var 
    t1e:Number;
            var 
    bla1:AccDirSpd = new AccDirSpd();
            public function 
    MainClass()
            {
                
    t1e 5;
                
    trace(t1e);
                
    with (bla1)
                {
                    
    trace(t1e);
                    var 
                    
    t1e:Number;
                                    
    t1e 7;
                                    
    trace(t1e);
                }
                
    trace(t1e);
    }

    AccDirSpd class has no t1e var.

  8. #8
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    What does this give you?
    PHP Code:
    public class MainClass extends MovieClip
    {
        private var 
    bla1:AccDirSpd = new AccDirSpd();
        private var 
    t1e:Number;
        
        public function 
    MainClass()
        {
            
    with (bla1)
            {
                
    t1e 7;
                
    trace(t1e);
            }
        }


  9. #9
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    7. and out of the with statement also 7

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