Object.watch; using the newval within the called function.

  • DrGonzo
  • Beginner
  • Beginner
  • DrGonzo
  • Posts: 36

Post 3+ Months Ago

Hi Guys,
I'm trying to slim down some code, which currently uses an onEnterFrame() function to add together some text boxes and display an up-to-date 'price'.

i.e:
Code: [ Select ]
onEnterFrame = function(){
    itemTotal_txt.text = String("Price: £")+Number(Number(price1_txt.text)+Number(price2_txt.text)+Number(price3_txt.text)+Number(price4_txt.text));
}
  1. onEnterFrame = function(){
  2.     itemTotal_txt.text = String("Price: £")+Number(Number(price1_txt.text)+Number(price2_txt.text)+Number(price3_txt.text)+Number(price4_txt.text));
  3. }

I've been playing with the Object.watch function, so I can avoid using onEnterFrame and my code looks like this instead;
Code: [ Select ]
function watch_price(prop,oldval,newval){
    itemTotal_txt.text = String("Price: £")+Number(Number(price1_txt.text)+Number(price2_txt.text)+Number(price3_txt.text)+Number(price4_txt.text));
    return newval;
}
price1_txt.watch("text",watch_price);
price2_txt.watch("text",watch_price);
price3_txt.watch("text",watch_price);
price4_txt.watch("text",watch_price);
  1. function watch_price(prop,oldval,newval){
  2.     itemTotal_txt.text = String("Price: £")+Number(Number(price1_txt.text)+Number(price2_txt.text)+Number(price3_txt.text)+Number(price4_txt.text));
  3.     return newval;
  4. }
  5. price1_txt.watch("text",watch_price);
  6. price2_txt.watch("text",watch_price);
  7. price3_txt.watch("text",watch_price);
  8. price4_txt.watch("text",watch_price);

The problem is, when the watch_price function is called, it adds the old value of the changed textbox, not the new value. I've tried returning 'newval' before adding the boxes together, but that stops the calulating happening at all and I've also tried putting...
Code: [ Select ]
-Number(oldval)+Number(newval)

...into the calucaltion, but it just returns undefined.

Does anyone know how to update the textbox value before running the function?

Thanks,

Dr G.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

well I'm not sure about the exact problem your having but I think the object.watch() might be a bit of overkill and I think ultimately a difficult thing to implement with a textfield. I always understood the watch() function as checking to see if a property has changed (_height, _alpha) etc.

If I understand correctly you want to take a few text fields and add their current value even after that value is modified by a user.

So I would do this instead
Code: [ Select ]
price1_txt.onChanged = function(){
   this._parent.updatePrice();
}
function updatePrice(){
   var price:Number = Number(price1_txt.text)+Number(price2_txt.text)+Number(price3_txt.text)+Number(price4_txt.text);
itemTotal_txt.text = "Price: £"+price;
}
  1. price1_txt.onChanged = function(){
  2.    this._parent.updatePrice();
  3. }
  4. function updatePrice(){
  5.    var price:Number = Number(price1_txt.text)+Number(price2_txt.text)+Number(price3_txt.text)+Number(price4_txt.text);
  6. itemTotal_txt.text = "Price: £"+price;
  7. }


So the deal here is that textfield.onChanged occurs any time the field is modified. You could essentially add that same code to each text field.
  • DrGonzo
  • Beginner
  • Beginner
  • DrGonzo
  • Posts: 36

Post 3+ Months Ago

Hi Graphixboy,
The problem is, that the price boxes are changed dynamically, based on a series of if/else statements... I could add the updatePrice() function to that statement - i.e every time there is a if(somthing){price_txt.text = (something) - but that seems like a lot of extra lines of code... It's a shame that onChanged doesn't work dynamically....
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

Ahh that makes sense. Did you try the setInterval() to check instead. You could check something like every second and thats a lot less processor intensive than the onEnterFrame

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 13 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.