CodeCanyon

Subtract value from Dynamic Text field.

45 posts
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
datoshway says

I have a small cart i’m building and would like to build a button to remove an item. SO this is my code, problem is, it goes into negatives after it hits $0. How can I stop it at $0?

on (release) { tellTarget (””) { product.quantity1.text = Number(product.quantity1.text)-1; product.total1.text = Number(product.quantity1.text)*45; }

}

111 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 1 000 and 5 000 dollars
  • Taiwan
savorks says
product.quantity1.text = Number(product.quantity1.text)-1;

to

product.quantity1.text = Math.max(Number(product.quantity1.text)-1 , 0);
45 posts
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
datoshway says

You rock man, thank you!

111 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 1 000 and 5 000 dollars
  • Taiwan
savorks says

My pleasure . :)

45 posts
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
datoshway says

One more question while I gotcha =)

What if we wanted it to do something at 0?

something like if 0 gotoandplay

111 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 1 000 and 5 000 dollars
  • Taiwan
savorks says
if you want do something after values apply to text fields, just check value of quality before function end . like
if(Number(product.quantity1.text) == 0)
{
    // your scripts
}
or if you want execute something before 0 apply to text files
var tempQuality:Number = Math.max(Number(product.quantity1.text), 0);
if(tempQuality == 0)
{
    // your scripts
}
else
{
    product.quantity1.text = tempQuality;;
    product.total1.text = tempQuality*45;    
}
45 posts
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
datoshway says

So freakin awesome. Thank you thank you !

111 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 1 000 and 5 000 dollars
  • Taiwan
savorks says

No problem :)

by
by
by
by
by