Jump to content

Battle Game Script


mobone
 Share

Recommended Posts

In the game everyone can have an ice, or bleeding, or poison attack and such. And I have the script, but I would rather make a function that can handle both the attackers and defenders elemental damage, instead of writing all three scripts twice.

<script type="text/javascript">defender['timer'] = setInterval('defender_hit()', defender['attack_rate'])  // start the timersfunction defender_hit() {attacker['hp']=attacker['hp']-defender['str']; // defender attacks attacker		// ice attack	if (attacker['dex']>0) {	attacker['dex']=attacker['dex']-(defender['ice']/8); // defender decrease attackers dex, slowing him	attacker['attack_rate'] = 5000/attacker['dex'];		 // set the attackers new rate	clearInterval(attacker['timer']);					// clear the attackers old timer			if (attacker['dex']>0)						   // set the attackers new timer		attacker['timer'] = setInterval('attacker_hit()', attacker['attack_rate']);				if (attacker['dex']<3)		  //change status if frozen		attacker['status'] = 'Frozen';	}}</script>

I'd like to have something I could call like this instead:

<script type="text/javascript">function ice_attack(giver,taker) {//ice attack}</script>

But I don't know how I would automatically know whose attributes and timers would need to be changed. What would be great is if I could pass in a variable name in as a variable. Then I could specify when calling the function who's doing what.Like:

'variable_name'[dex]='variable_name'[dex]-1

Any ideas? Thanks

Edited by Mobone
Link to comment
Share on other sites

Instead of having a defender variable, and an attacker variable, have a characters array that contains whatever characters you have. You can refer to each one by name.

var characters = new Array();characters['attacker'] = {  'hp': 100,  'str': 10,  'ice': 5  ...};characters['defender'] = {  'hp': 100,  'str': 10,  'ice': 5  ...};

You can pass the word "attacker", for example, and look up characters[character_name]. You can just have a generic "attack" function, where you pass the attacker, the defender, and the type of attack.

Link to comment
Share on other sites

So if tom was attacking nick, it would be.. correct me if I'm wrong..

basic_attack(attacker,defender) {	 characters[defender][hp]=characters[defender][hp]-characters[attacker][str];}basic_attack(tom,nick);

Is that how you actually reference the array within the array like you have it?

Link to comment
Share on other sites

If tom and nick are variables, then yeah. Otherwise, you need to quote them if they are strings. The characters array should have elements called "tom" and "nick".
Awesome, I got it working, exactly what I was lookin for. Your great. Btw, I discovered the "onunload" event I was asking you about.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...