Basic Math in Flash - From adding to generating random numbers.
Math is fun, ok maybe that’s just me, but before you code anything really cool, you need to know how to use math in flash.
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.
Be sure to check out our site pogollama.com for free flash games and highscore tables.
heck what a useless post...
ReplyDeleteHey I'm new at this whole thing but this tutorial was exactly what I needed for my noob-rpg-game ;)
ReplyDeleteMany thx!
Math is fun, ok maybe that’s just me, but
ReplyDeletegenerally if someone begins coding a flash game he has some understanding of flash and math in general. there is a lot more to it then buf, health, and damage calculations.
like: position, acceleration based movement, directions and angles (o ton of trigo, that works a bit weird in flash coordinate system), collisions, intersections, and a lot more.
ok... i silence myself and check out the site... ;)
hey but whay about when he gets to zero on health how do you script him to die, plz help soon
ReplyDeleteLearn to describe things in depth instead of just throwing them together. I learned nothing but lines. I did not learn how they work or anything.
ReplyDelete