I tried to get a really simple battle system working, but I can't get it to attack...
If you don't know pokemon pretend this is a plain battle with health, attack, defense, and levels because I really need help...
<html>
<head>
<script type="text/javascript">
function searchforpkmn()
{
var wp;
wp=(Math.ceil(Math.random()*3));
var lv;
lv=(Math.ceil(Math.random()*4));
lv=(lv+1)
var pk;
var text;
var hp;
hp=(lv*2+10)
var att;
att=(lv)
var def;
def=(Math.ceil(lv/2));
var HP;
HP=20;
var LV;
LV=5;
var ATT;
ATT=5;
var DEF;
DEF=3;
var dmg;
var DMG;
if (wp == 1)
{
text="You didn't find a pokemon.";
}
else if (wp == 2)
{
text="You found a <b>Pidgey</b>, level " + (lv) + ". Go, Meowth!";
document.getElementById("hide").style.display="none";
pk="Pidgey";
}
else
{
text="You found a <b>Rattata</b>, level " + (lv) + ". Go, Meowth!";
document.getElementById("hide").style.display="none";
pk="Rattata";
}
document.getElementById("replacedtext").innerHTML=text;
if (wp != 1)
{
document.getElementById("hidp").innerHTML="Meowth, lv. 5, HP " + HP + "<br />" + pk + ", lv. " + lv + ", HP "+ hp;
document.getElementById("hidp").style.display="block";
document.getElementById("hidab").style.visibility="visible";
document.getElementById("hidrb").style.visibility="visible";
}
}
</script>
<script type="text/javascript">
function attackopp()
{
DMG=(Math.ceil(LV/2+ATT/def));
hp=hp-DMG;
document.getElementById("hidp").innerHTML="Meowth, lv. 5, HP " + HP + "<br />" + pk + ", lv. " + lv + ", HP "+ hp;
}
</script>
</head>
<body>
<p id="hidp">Text</p>
<p id="replacedtext">Click below to search for a pokemon!</p>
<button type="button" id="hide" onclick="searchforpkmn()">Search!</button>
<button type="button" id="hidab" onclick="attackopp()">Attack!</button>
<button type="button" id="hidrb" onclick="runaway()">Run!</button>
<script type="text/javascript">
document.getElementById("hidp").style.display="none";
document.getElementById("hidab").style.visibility="hidden";
document.getElementById("hidrb").style.visibility="hidden";
</script>
</body>
</html>What it's supposed to do is search for a pokemon on the button press, then has a 1/3 chance of finding nothing, 1/3 finding pidgey, 1/3 finding rattata. That works.
Then, if it finds a pokemon, it generates its random level from 2-5, attack of equivalent value, defense of (Math.ceil(lv/2) ), and HP of (lv*2)+10. That works.
Now, I'm just trying to get Meowth to attack the wild pokemon after pressing the [Attack!] button. That should trigger the attack() function, and then the function should write the hp of the opponent... but it doesn't work... Can someone help me please?
Last edited by kayybee (2011-07-11 00:56:52)
Offline
found it. u have declared LV in the function find pokemon right ? now it becomes a private variable and ends as the function ends. then when u run the attack function the LV gets undefined a b8r solution would have been declaring LV,DMG etc in global scoper or rather write an object for them
in the global scope b4 starting any function
<html>
<head>
<script type="text/javascript">
var LV;
var DMG;
var ATT;
var pk;
var lv;
var hp;
var HP;
function searchforpkmn()
{
var wp;
wp=(Math.ceil(Math.random()*3));
var lv;
lv=(Math.ceil(Math.random()*4));
lv=(lv+1)
var pk;
var text;
hp=(lv*2+10)
var att;
att=(lv)
def=(Math.ceil(lv/2));
HP=20;
LV=5;
ATT=5;
var DEF;
DEF=3;
var dmg;
if (wp == 1)
{
text="You didn't find a pokemon.";
}
else if (wp == 2)
{
text="You found a <b>Pidgey</b>, level " + (lv) + ". Go, Meowth!";
document.getElementById("hide").style.display="none";
pk="Pidgey";
}
else
{
text="You found a <b>Rattata</b>, level " + (lv) + ". Go, Meowth!";
document.getElementById("hide").style.display="none";
pk="Rattata";
}
document.getElementById("replacedtext").innerHTML=text;
if (wp != 1)
{
document.getElementById("hidp").innerHTML="Meowth, lv. 5, HP " + HP + "<br />" +
pk + ", lv. " + lv + ", HP "+ hp;
document.getElementById("hidp").style.display="block";
document.getElementById("hidab").style.visibility="visible";
document.getElementById("hidrb").style.visibility="visible";
}
}
</script>
<script type="text/javascript">
function attackopp()
{
DMG=(Math.ceil(LV/2+ATT/def));
hp=hp-DMG;
document.getElementById("hidp").innerHTML="Meowth, lv. 5, HP " + HP + "<br />" +
pk + ", lv. " + lv + ", HP "+ hp;
}
</script>
</head>
<body>
<p id="hidp">Text</p>
<p id="replacedtext">Click below to search for a pokemon!</p>
<button type="button" id="hide" onclick="searchforpkmn()">Search!</button>
<button type="button" id="hidab" onclick="attackopp()">Attack!</button>
<button type="button" id="hidrb" onclick="runaway()">Run!</button>
<script type="text/javascript">
document.getElementById("hidp").style.display="none";
document.getElementById("hidab").style.visibility="hidden";
document.getElementById("hidrb").style.visibility="hidden";
</script>
</body>
</html>works for most of the part of it rest u can get it to work
Offline
btw install firebug (if u are using firefox) or use the google page console (if u are using chrome) or see the errors in IE to see whats wrong
I recommend firebug for debugging btw
Offline
kayybee wrote:
Oh thank you! I completely forgot about the local and global variable rule.
As you might be able to tell, I am new at JavaScript. Just learned it yesterday.
I guessed that, i have an worthy advice for u
use OBJECTS rather then global variables for making new types.
Objects might be a new world concept to you guys but its easy to understand
think of a real world Pokemon it has some properties and behaviors the representation of a pokemon object in code will be an object which has some variables as properties and functions as behaviors. heres an example of objects
function pokemon(Type,ATK,DEF,LVL)
{ this.Type = Type;
this.ATK = ATK;
this.DEF = DEF;
}
var My = pokemon("RATTATA",30,30,2); // this will give u an object of ur character
now u can use it like
My.Type ;/ this tells that which type it is. and so on ...
Offline
Help again?
<html>
<head>
<script type="text/javascript">
function searchforpkmn()
{
wp=(Math.ceil(Math.random()*3));
lv=(Math.ceil(Math.random()*4));
lv=(lv+LV-2)
hp=(lv*2+10)
att=(lv)
def=(Math.ceil(lv/2));
exp=(Math.ceil(lv*4-LV))
if (wp == 1)
{
text="You didn't find a pokemon.";
}
else if (wp == 2)
{
text="You found a <b>Pidgey</b>, level " + (lv) + ". Go, Meowth!";
document.getElementById("hide").style.display="none";
pk="Pidgey";
}
else{
text="You found a <b>Rattata</b>, level " + (lv) + ". Go, Meowth!";
document.getElementById("hide").style.display="none";
pk="Rattata";
}
document.getElementById("replacedtext").innerHTML=text;
if (wp != 1)
{
document.getElementById("hidp").innerHTML="Meowth, lv. " + LV + ", HP " + HP + " EXP " + EXP + "/" + EXT + "<br />" + pk + ", lv. " + lv + ", HP "+ hp;
document.getElementById("hidp").style.display="block";
document.getElementById("hidab").style.visibility="visible";
document.getElementById("hidrb").style.visibility="visible";
}
}
</script>
<script type="text/javascript">
function attackopp()
{DMG=(Math.ceil((LV/2+ATT/def)*((Math.random()/5)+.9)));
hp=hp-DMG;
if (hp < 0)
{hp=0}
if (hp > 0)
{
dmg=(Math.ceil((lv/2+att/DEF)*((Math.random()/5)+.9)));
HP=HP-dmg;
if (HP < 0)
{
HP=0;
LV=LV-1
}
}
else
{dmg=0}
text="Meowth did " + DMG + " damage. " + pk + " did " + dmg + " damage.";
if (hp == 0)
{
text=text + " You have defeated " + pk + ". You have gained " + exp + " EXP points."
EXP=EXP+exp
if (EXP >= EXT)
{
LV=LV+1;
HP=LV*2+10;
ATT=LV;
DEF=LV/2;
EXP=EXP-EXT;
EXT=LV*3;
text=text + " You have leveled up. You are now level " + LV + "."
}
document.getElementById("hidab").style.visibility="hidden";
document.getElementById("hidrb").style.visibility="hidden";
document.getElementById("hide").style.visibility="visible";
}
document.getElementById("replacedtext").innerHTML=text;
document.getElementById("hidp").innerHTML="Meowth, lv. " + LV + ", HP" + HP + " EXP " + EXP + "/" + EXT + "<br />" + pk + ", lv. " + lv + ", HP "+ hp;
}
function runaway()
{
if ((Math.ceil(Math.random()*5) == 1)
{
document.getElementById("hide").innerHTML="Couldn't escape!"
}
else
{
document.getElementById("hide").innerHTML="Got away safely!"
document.getElementById("hidab").style.visibility="hidden";
document.getElementById("hidrb").style.visibility="hidden";
document.getElementById("hide").style.visibility="visible";
}
</script>
</head>
<body>
<p id="hidp">Text</p>
<p id="replacedtext">Click below to search for a pokemon!</p>
<button type="button" id="hide" onclick="searchforpkmn()">Search!</button>
<button type="button" id="hidab" onclick="attackopp()">Attack!</button>
<button type="button" id="hidrb" onclick="runaway()">Run!</button>
<script type="text/javascript">
PK="Meowth"
HP=20;
LV=5;
ATT=5;
DEF=3;
EXP=0;
EXT=LV*3;
document.getElementById("hidp").style.display="none";
document.getElementById("hidab").style.visibility="hidden";
document.getElementById("hidrb").style.visibility="hidden";
</script>
</body>
</html>After I win the battle, it won't show the search button...
Can someone help again?
Offline
Yeah, I really think you should use php for this kind of thing, javascript should be used for some other calculators or effects but know thats its not hard at all for a user to alter the values. Use php properly, and no one should be able to alter the values, and the scripting is similar anyways.
Offline
I don't really know any Javascript, but I use Python 3.1. I already have a small game in Python, so if you can turn this into Javascript (Python uses simpler and more intuitive syntax than Javascript, so it should be easy) just copy and paste. The hash marks (#) signify that the rest of the line is a comment, and the 'import random' and 'random.randint' statements generate random numbers (random must be imported to use the random.randint command). The numbers inside of the parentheses () are the min and max. Also, the only thing that blank lines do is organize code. They don't make any real difference.
I wrote:
##Importing##
import random
##########
##Initiating Variables and Such Items##
dmg = 0# dmg can also refer to choices such as menu selection and many other varying possibilities
edmg = 0
level = 1
weap = 0
min = 0
max = 0
hp = 35
ehp = 35
mp = 3
shieldOn = 1
eshieldOn = 1
gold = 0
win = 0
loop = 1
##########
input('''Hello and Welcome to the Arena\n(Press the Enter Key to Continue)''')
print('''Your Fight has begun!\n\n\t\tEngarde!\n''')
while loop == 1:
print('''You have ''', hp, '''HP.\nYou have ''', mp, '''MP.''')
dmg = input('''>>> ''')
if dmg == '''attack''':
dmg = random.randint(1, 5) + level + weap
ehp -= dmg // eshieldOn + weap
eshieldOn = 1
print('''You have attacked.''')
print('''Enemy HP:\n\t''', ehp)
elif dmg == '''magic''':
if mp > 0:
dmg = random.randint(4, 9)
ehp -= (dmg // eshieldOn) + 1 + weap + level
mp -= 1
eshieldOn = 1
print('''You have used magic''')
print('''Enemy HP:\n\t''', ehp)
elif mp == 0:
print('''You do not have enough MP to use this ability.''')
elif dmg == '''shield''':
print('''You have raised your shield.''')
shieldOn = 2
else:
print('''That is not a valid option. Your character will now pass.''')
if ehp <= 0:
print('''Congradulations! You have won!''')
win = 1
loop = 0
level += 1
elif hp <= 0:
print('''You have lost!''')
loop = 0
win = 0
edmg = random.randint(0, 10)# Here we set the enemy choice: attack, magic, or shield
if loop == 1:
if edmg < 7:
hp -= 2 // shieldOn
print('''Your opponent has attacked.''')
shieldOn = 1
elif edmg < 9 and edmg > 6:
hp -= 4 // shieldOn
print('''Your opponent has used magic.''')
elif edmg > 8:
print('''Your opponent has raised their shield.''')
eshield = 2
I know that it's not Pokémon, but after you convert it to Javascript it should be simple enough to add more things based on my program.
Last edited by maxskywalker (2011-07-12 09:25:42)
Offline
archmage wrote:
Yeah, I really think you should use php for this kind of thing, javascript should be used for some other calculators or effects but know thats its not hard at all for a user to alter the values. Use php properly, and no one should be able to alter the values, and the scripting is similar anyways.
php hmm using HTML5 he can use PHP just as data parser from 1 client to other
Offline
fanofcena wrote:
archmage wrote:
Yeah, I really think you should use php for this kind of thing, javascript should be used for some other calculators or effects but know thats its not hard at all for a user to alter the values. Use php properly, and no one should be able to alter the values, and the scripting is similar anyways.
php hmm using HTML5 he can use PHP just as data parser from 1 client to other
with ajax possibly but he should probely use a java applett
Offline
TRocket wrote:
fanofcena wrote:
archmage wrote:
Yeah, I really think you should use php for this kind of thing, javascript should be used for some other calculators or effects but know thats its not hard at all for a user to alter the values. Use php properly, and no one should be able to alter the values, and the scripting is similar anyways.
php hmm using HTML5 he can use PHP just as data parser from 1 client to other
with ajax possibly but he should probely use a java applett
naaaaaaaahhhhhhhhhhhhhhhhh~~!!!!!!!
http://chats.ws << check that running through ajax only
and php no java Required!! wheeeeeeeeeeeeee JS can 8-) .
Offline