PDA

View Full Version : Random Damage from a Range; how?


Sythius
04-19-2009, 03:10 PM
my current means of doing damage to the hp is this;

_root.bad = _root.bad-random(30);
if (bad<0) {
bad = 0;
}

As you can see.. This allows any number from 0-30 to damage the hp.
The hp is a Dynamic Text Field.
The Text Field's variable is "bad". As can be seen in the coding.
My problem is. I want to be able to do like .. a range...
like for an exmaple, 12-47.. so the min damage done is 12, the max is 47.. Can somebody please help me?

( The
if (bad<0) {
bad = 0;
just stops the hp going into like -12, etc. And keeps it to a minimum of 0.
}

thanks in advance.

rrh
04-19-2009, 04:09 PM
min+random(max-min)

where min is the lower end of your range, and max is the upper end

Sythius
04-20-2009, 08:56 AM
Didn't work.
I did
_root.bad = _root.bad-min+random(15-10);
and that made the HP variable go to NaN and insta-win.

bluemagica
04-20-2009, 11:52 AM
var tempDamage = Math.round(Math.random()*(47-12))+12;
if(_root.bad>=0 && (_root.bad-tempDamage)>0)
{
_root.bad -= tempDamage;
}
else
{
_root.bad = 0;
}

EightySeven
04-20-2009, 01:26 PM
Its easiest imo to just use a function, and ahve that function on hand at all times

/* randRange
This function is used to return a random number between
a floor number and a ceiling number
*/
function randRange(floor:Number, ceiling:Number) {
var randomNum:Number = Math.random()*(ceiling-floor)+floor;
return randomNum;
}

from my utils file

rrh
04-20-2009, 03:12 PM
Didn't work.
I did
_root.bad = _root.bad-min+random(15-10);
and that made the HP variable go to NaN and insta-win.
Because you replaced one min but not both min.

bluemagica
04-20-2009, 04:04 PM
i think it didn't work because he used "random" instead of "Math.random()"!

rrh
04-20-2009, 07:56 PM
random() is a real function, it's older than Math.random() but they kept it around, and you can still use it in AS2.

Sythius
04-21-2009, 09:25 PM
var tempDamage = Math.round(Math.random()*(47-12))+12;
if(_root.bad>=0 && (_root.bad-tempDamage)>0)
{
_root.bad -= tempDamage;
}
else
{
_root.bad = 0;
}


Thanks!
This I found to work very well! ^^
Really. thank you so much X3 ...
with your code i was even able to get it so it could show damage done! Thank you! ^^