A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [F8][help]object variables

  1. #1
    Senior Member walnoot's Avatar
    Join Date
    Apr 2005
    Posts
    751

    [F8][help]object variables

    Hi!

    I need a bit of help on which terms or commands I have to search when I want to do something like this:

    Code:
    var p:Object = new Object();
    p.a = 1;
    p.b = 1;
    p.c = p.a+p.b;
    trace(p.c); //traces 2
    p.a += 1;
    trace(p.c); //traces 2, but I want it to trace 3 since a+b = 3 by now
    Thanks!

  2. #2
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Don't think that's possible? Well, not without a mess of enterFrames or some kind of event broadcaster.
    http://www.birchlabs.co.uk/
    You know you want to.

  3. #3
    Senior Member
    Join Date
    Dec 2005
    Location
    No where
    Posts
    105
    you could use a class and a get/set
    code:

    class P{
    public var a:Number;
    public var b:Number;
    public function p(_a:Number, _b:Number){
    a = _a;
    b = _b;
    }
    public function get c():Number{
    return a+b;
    }
    }


  4. #4
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    strange (can´t imagine that it doesn´t work), maybe you have to to use local variables when computing and assign them in the end, lik
    var a=1;
    var b=1;
    var c=a+b;
    ....

  5. #5
    Senior Member
    Join Date
    Feb 2004
    Posts
    312
    Quote Originally Posted by walnoot
    Code:
    var p:Object = new Object();
    p.a = 1;
    p.b = 1;
    p.c = p.a+p.b;
    trace(p.c); //traces 2
    p.a += 1;
    trace(p.c); //traces 2, but I want it to trace 3 since a+b = 3 by now
    p.c hasn't changed after u incremented p.a why would you expect p.c=3?

  6. #6
    Style Through Simplicity alillm's Avatar
    Join Date
    Mar 2004
    Location
    Wales
    Posts
    1,988
    Yea, you will have to update the value of p.c somehow, either in an enterFrame, just in the code:

    Code:
    var p:Object = new Object();
    p.a = 1;
    p.b = 1;
    p.c = p.a+p.b;
    trace(p.c); //traces 2
    p.a += 1;
    p.c = p.a+p.b
    trace(p.c); //traces 3
    or set up a function for the purpose:

    Code:
    function changeValues(incA, incB) {
    	p.a += incA;
    	p.b += incB;
    	p.c = p.a+p.b;
    }
    var p:Object = new Object();
    p.a = 1;
    p.b = 1;
    p.c = p.a+p.b;
    trace(p.c);
    //traces 2
    changeValues(1, 0);
    trace(p.c);
    //traces 3
    There may be a quick neat way of doing it, but I don't know it.

    Ali

  7. #7
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    3 ways you can do it with AS1, that i know of. Use an object or a class

    Code:
    var p = {}
    p.a = 1;
    p.b = 1;
    p.func = function(){ return this.a + this.b };
    p.addProperty("c", p.func, null);
    
    trace( p.c )
    
    p.a++
    
    trace( p.c )
    
    
    var p2 = {}
    p2.a = 5
    p2.b = 5
    p2.c = function() { return this.a + this.b }
    
    trace( p2.c() )
    
    p2.a++
    
    trace( p2.c() )
    depends if you want c to be treated as a property or a funciton

    ckdrkness presented to AS2 method
    Last edited by mr_malee; 03-01-2007 at 09:59 PM.
    lather yourself up with soap - soap arcade

  8. #8
    Senior Member walnoot's Avatar
    Join Date
    Apr 2005
    Posts
    751
    Code:
    var p = {}
    p.a = 1;
    p.b = 1;
    p.func = function(){ return this.a + this.b };
    p.addProperty("c", p.func, null);
    
    trace( p.c )
    
    p.a++
    
    trace( p.c )
    Yeah! Thanks for all the replies, but this is exactly what I meant. It's not that I'm too lazy to update the values everytime, but if you're working with a lot of different objects with a lot of different values I think self-regulating values can make my code a bit more transparent. Thanks!

  9. #9
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    It's not quite as elegant, but Object.watch is another way..
    It just fires an event when an object property is changed.
    Can come in useful


    Code:
    var p:Object = new Object();
    p.a = 1;
    p.b = 1;
    p.c = p.a+p.b;
    update = function (prop, oldVal, newVal) {
    	trace(prop); //traces a
    	trace(oldVal); //traces 1
    	trace(newVal); //traces 2
    	p.c = newVal+p.b;
    };
    p.watch("a", update);
    trace(p.c);//traces 2
    p.a += 1;
    trace(p.c);//traces 3

  10. #10
    Senior Member walnoot's Avatar
    Join Date
    Apr 2005
    Posts
    751
    Never heard about that command, I 'll have a look at it too, thanks!

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