I hate that I have to ask a homework question on here, but I can’t help it. I’m taking an ActionScript class in school, and it’s my first time learning a programming language so I feel rather noobish.
The problem was to make an RPG game to practice while loops.
var fighter1:String = "Wonder Woman";
var fighter2:String = "Ms. Marvel";
var health1:uint = 300;
var health2:uint = 180;
var damage1:uint = 30;
var damage2:uint = 40;
while (health1 >= 0 && health2 >= 0 )
{
var damageDealt1:uint = Math.random() * damage1;
var damageDealt2:uint = Math.random() * damage2;
var attack1:uint = health2 - damageDealt1;
var attack2:uint = health1 - damageDealt2;
health1 = attack2;
health2 = attack1;
trace("After attack: " + fighter1 + " HP: " + health1 + ", " +
fighter2 + " HP: " + health2);
}
if (health1==0&&health2==0){
trace("They both died in battle! There will now be WAR!");
} else if (health1==0) {
trace(fighter2 + " won!");
} else {
trace(fighter1 + " won!"
}
As you can probably tell, damage1 is the potential damage for fighter1, and damageDealt1 is the damage dealt per attack.
Everything seems to work normally except that when I run it I get this:
After attack: Wonder Woman HP: 4280752046, Ms. Marvel HP: 4284398685
After attack: Wonder Woman HP: 4280752039, Ms. Marvel HP: 4284398660
After attack: Wonder Woman HP: 4280752024, Ms. Marvel HP: 4284398658
After attack: Wonder Woman HP: 4280752018, Ms. Marvel HP: 4284398639
After attack: Wonder Woman HP: 4280751997, Ms. Marvel HP: 4284398638
After attack: Wonder Woman HP: 4280751996, Ms. Marvel HP: 4284398612
After attack: Wonder Woman HP: 4280751994, Ms. Marvel HP: 4284398609
ad infinitum…
I’ve translated it to php and it works fine, I just can’t figure out what I’m doing wrong here.
Any ideas? Thanks in advance.
as turbosqel said – you have to use int instead of uint: