代码直接贴出来了:)
希望给大家一些参考,课堂用到的。
var hero:Object = new Object(); var monster:Object = new Object(); //设置勇士的参数 hero.name="英雄"; hero.HP=10000; hero.ATT=90; hero.DEF=88; hero.AGI=60; hero.skill1="普通攻击"; hero.skill2="升龙拳"; hero.skill3="恶魔之魂"; //设置恶龙的参数 monster.name="恶龙"; monster.HP=10000; monster.ATT=120; monster.DEF=100; monster.AGI=22; monster.skill1="攻击"; monster.skill2="龙尾击"; monster.skill3="毁灭光线"; var timer:Timer = new Timer(1000); timer.addEventListener(TimerEvent.TIMER,round); timer.start(); function whoAttacks(a:Object,b:Object) { return a.AGI * Math.random() > b.AGI * Math.random()?[a,b]:[b,a]; } function round(e:TimerEvent):void { //damage为破坏力 var damage:Number; var skillIndex:uint; var roundFight:Array=whoAttacks(hero,monster); var attacker:Object=roundFight[0]; var defender:Object=roundFight[1]; //破坏力等于进攻方攻击力-防守方的防御力 damage = attacker.ATT*Math.random()-defender.DEF*Math.random(); if (damage<=0) { //如果破坏力为<=0; damage=0; } else{ damage=Math.ceil(damage); } defender.HP -= damage; //判断武器类型 //当破坏力小于进攻方攻击3分1时 if (damage<attacker.ATT/3) { skillIndex=1; //当破坏力小于进攻方攻击3分2时 } else if (damage<attacker.ATT*2/3) { skillIndex=2; } else { skillIndex=3; } trace(attacker.name+"向"+defender.name+"发动了"+attacker["skill"+skillIndex]+","+defender.name+"损失了"+damage+"点HP"); if (defender.HP<=0){ trace(defender.name+"终于支持不住了"+attacker.name+"取得了胜利"); timer.removeEventListener(TimerEvent.TIMER,round); trace("Game Over!"); } trace(hero.name+"HP:"+hero.HP+monster.name+"HP"+monster.HP); }