This is not my tutorial just thought it could seriously help some people with esting thier small rpg's. sorry its in as2 but i havent tried it in as3.
Basic Math in Flash - From adding to generating random numbers.
Adding and Subtracting with Variables. Letâs say you have a variable for your main characterâs health. What if he gets hit by a bad guy. We want to subtract some health. To do this we can use.
health = health â" 15 or health -= 15
Both will take the value of âhealthâ and subtract 15 from it. Now if our main character(lets call him Bob), so if Bob uses a health potion, his health will rise. We would use this code.
health = health +30 or health += 30
Multiplying and Dividing with Variables. Now Bob found a new item. It makes his attack 50% stronger. To calculate this we use.
attack = attack * 1.5 or attack *= 1.5
A bad guy cast a spell that makes Bobâs defense less effective. Now we use.
defense = defense/2 or defense /= 2
Rounding and Generating Random Numbers. Now we donât want Bob to do the same amount of damage every time he attacks. Letâs add an element of chance by generating a random number.
damage = Math.random ( ) * 25
The random function finds a random number between 0 and 1.
(Ex: 0.342514613451) We then multiply it by 25 so that it finds a number between 0 and 25.
Now what if we donât want bob to have decimals in his attack damage. We use this code.
damage = Math.round(damage)
Or we can put this all in one line by doing this.
damage = Math.round(Math.random( ) * 25)
But what if we want Bob to do at least 10 damage but not more than 25? Then we use something like this.
damage = Math.round(Math.random( ) * 15) +10
This code finds a random number between 0 and 15, then adds 10 to it, creating a range of 10-25.
You can also trick it a bit to individual needs but also i wonder if i can use this for makeing dice in a flash game. Enjoy and please give feedback. Hope you find it usefull
Depending on the language you're using, it can be even easier:
$dmg = rand($low,$high) ; // php
The tricky part comes in where you need to factor in several dice rolls, like one for whether you even hit or not, one for how hard you'll hit (random between minimum/maximum dmg of your weapon), one for whether the opponent can block, and if so how much dmg they mitigate, etc., and if the final value is 0 then call it a miss/parry/etc.