A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Silly Problem with variables

  1. #1

    Silly Problem with variables

    I keep coming across a problem were i say:
    varTotal = var1 + var2

    and instead of adding them together for a combined total, its puts them gether... for example if var1 equaled 1, and var2 equaled 2, together they wouldnt equal 3, but 12... kinda anoying and im not sure what im doing wrong to make this happen...

    Can anyone tell me some specifics that change how the variables hold the values? and what makes it so they no longer combine but mearly add on to other variables?

    Heres the code just in case you can pick out an error that is making my code not work. Below explains the variables in question
    /code
    //code resides on 1st frame of main movit timeline
    //start up values
    _root.outputAgil = 5;
    _root.outputStr = 5;
    _root.outputWepSkill = 5;
    _root.outputWepDam = 1.5
    _root.outputLevel = 1;
    //crit chart
    critArray = [0,6,6.25,6.50,7,7.25,7.50,8,8,8,9,9,9,10,10,10,11, 11,12,12]

    function calcStats(){
    //-----CRIT CHANCE
    //crit chance info
    playerAgility = _root.outPutAgil;
    playerLevel = _root.outputLevel;
    critMod = _root.critArray[playerLevel];
    critChance = playerAgility / critMod;
    critChancePer = critChance * 10;
    //check skills of player to mod crit chance
    playerWepSkill = _root.outputWepSkill;
    wepSkillMax = playerLevel * 5;
    wepSkillMod = playerWepSkill / wepSkillMax;
    wepSkillPer = wepSkillMod * 100;
    //mod crit chance by weapon skill
    critChancePer100 = critChancePer / critChancePer;
    critChancePer100total = critChancePer100 * 100;
    finalCritMod = critChancePer100Total / wepSkillPer;
    totalCritChance = critChancePer / finalCritMod;
    //send complete chance to output
    _root.outputCrit = totalCritChance;
    //-----ATTACK POWER
    agilityMod = playerAgility / 5;
    >> totalAttackPower = agilityMod + playerWepSkill ;
    _root.outputAtt = totalAttackPower ;

    }


    /code

    The problem is with the line i put the ">>" on. If i trace totalAttackPower it doesnt combine the variables together but tags on onto the other... Thats where my problem resides and i hope you can help me with =)

    Hope i have been clear, and thanks in advance if you can help =)

    Ely

  2. #2
    skylogic.ca MD004's Avatar
    Join Date
    Oct 2003
    Location
    Canada
    Posts
    366
    I think you just need to convert it to a number: int(variable), Or you can get creative with using '-' instead.
    Code:
    varTotal = -(-var1 - var2);
    or
    varTotal = int(var1) + int(var2);
    You can also convert it somewhere else
    Code:
    var1 = int(var1);
    or
    var1 = int(someOtherVariable);
    By the way, it's doing this because it thinks that 1 or both of your variables are strings (such as 'abc') and not numbers. Also, the '+' operator is used to combine strings as well as to add numbers together so it picks which version of '+' to use, which is why '-' works because it always applies to numbers so flash converts them to numbers for you.

    ~MD

  3. #3
    thank you for your reply =) helped

    Though when i use int(variable) i cannot use anything besides whole numbers... so 1.2 becomes 1, and i need the ability to have fractions. Know a way to be able to define a variable as a integer but still able to hold fractions?

    Thanks so much =)

    Ely

    edit: as a side note, some fractions get insanly long, such as 3.3333333~, know a way that it will stop after the hundredth? so it would only display 3.33? doesnt apply but i didnt want to make a new thread for justy this silly question =) thanks!!
    Last edited by ElyxiaElvenbright; 01-03-2006 at 03:54 AM.

  4. #4
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    It shouldn't do this at all. What version of AS are you using?

    if you are using AS 2, you could try decaring the type of variable.

    e.g

    var myNumber:Number = 5
    lather yourself up with soap - soap arcade

  5. #5
    Junior Member
    Join Date
    May 2005
    Posts
    28
    Try using Number(var1) + Number(var2)
    Cheers

    Rolf Lidén | http://e-cube.dk

  6. #6
    thanks so much for the replies!!

    Number(var) worked like a charm thanks =)

    Not sure about what version AS im using, how would i tell? Im using FlashMX if that helps.

    NEW QUESTION =)

    Figure asking them here is better then new topics =)

    Q: Is it possible to random a fraction? Currently my code would look like this:
    Var1 = 2.6
    Var2 = random(var1)+1;

    var2 will random the whole number, but it will ALWAYS be .6. The .6 isnt randomized. Is there a way to do so? so that its truly random.

    Thanks again for your help =)

    Ely

  7. #7
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    Code:
    var1=2.6;
    //Set the integer part of the random number
    random1=random(Math.floor(var1))+1;
    //Set the decimal part of the random number
    tempvar=var1-Math.floor(var1);
    random2=(random(10*tempvar)+1)/10;
    
    final=random1+random2;
    I guess this is not the best way to solve your problem but it works
    Last edited by fil_razorback; 01-03-2006 at 05:27 PM.

  8. #8
    Junior Member
    Join Date
    May 2005
    Posts
    28
    What about something like this:

    var1 = 2.6;
    var2 = Math.random() * var1;

    This would return a number in the range 0 - 2.6. This could fx return 2.00021

    OR if you only want numbers like 1.4, 3.1 etc try something like this:

    var1 = 2.6;
    var2 = (Math.floor(Math.random() * var1*10)/10);


    BTW You are using AS1 if you got MX.

    Cheers
    Rolf Lidén

    http://e-cube.dk

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