mobone Posted June 10, 2009 Share Posted June 10, 2009 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 Link to comment Share on other sites More sharing options...
justsomeguy Posted June 10, 2009 Share Posted June 10, 2009 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 More sharing options...
mobone Posted June 10, 2009 Author Share Posted June 10, 2009 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 More sharing options...
justsomeguy Posted June 10, 2009 Share Posted June 10, 2009 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". Link to comment Share on other sites More sharing options...
mobone Posted June 10, 2009 Author Share Posted June 10, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.