SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: 1+2=12 ????

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    1+2=12 ????

    Hi

    Although ive been learning flash+actionscript for some time now , this is a total newbie question , i came across this error in a a fla i was doing and just cant unserstand it.

    I have 2 input text boxes , 1st one has variable a ,2nd has variable b.
    I have a dynamic text box , variable is c.
    I have a button with the following code:

    on (release) {
    c=a+b;
    }

    when i run the movie , if i put something like 1 in the first input and 2 in the secont ,instead of 1+2=3 i get 1+2 = 12.
    It is working as the variables were strings.Why?

    But , if i change the "+" with "-" or "*" or "/" it will work perfectly and treat the variables as numbers , it just wont do that with the "+".

    Ive attached the fla file so you can see it better.
    I hope some of you can help me please

    Thanks.
    Attached Files Attached Files

  2. #2
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've never used actionscript, but are there any variable type casting statements?
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    New York, NY
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    strange.. i've never had that problem before.. but to get around it, simply add Number() around the vars i.e.:
    Code:
    c = Number(a) + Number(b);
    zing
    Better-Barter.com
    Logo store full of unique, professional identities
    perfect for you or your client!

  4. #4
    SitePoint Wizard aaron.martone's Avatar
    Join Date
    Feb 2003
    Location
    Florida, USA
    Posts
    2,322
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    zecarioca,

    The reason why 1 + 2 = 12 is called "Concatenation" Input text is treated and seen as text, or string, until you tell it otherwise.

    Zing's idea of Number(a) and Number(b) will work fine for you since it's converting the data to true integers, thus allowing the mathematical operation of addition.

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that's what I was getting at .

    That's the reason PHP uses the period for string concatenation, because of it's automatic type conversion. I guess Number(variable) is the correct method.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •