-
Senior Member
[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!
-
Script kiddie
Don't think that's possible? Well, not without a mess of enterFrames or some kind of event broadcaster.
-
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;
}
}
-
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;
....
-
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?
-
Style Through Simplicity
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
-
M.D.
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.
-
Senior Member
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!
-
....he's amazing!!!
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
-
Senior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|