I have an online game and im trying to make an update. but my javascript/JQuery isnt that great. basically trying to make an auto attack feature in the game that would do a fight, a 6 second timer counts down, then when it hits 0 sets to ready and the next attack takes place.
It works to the extent where you can hit the auto attack button, it fights the first monster, the timer counts down and an auto attack is taken from your total. then when the timer hits 0 and sets to "Ready" it just stops and doesnt go onto the next attack. I cant see where its wrong, so can someone help me please? heres the code:
//functions
function autoattack_mob(autos,y,mob)
{
if (autos > 0) {
if (autos == y) {
var mob = document.getElementById("monstername").value;
} else {
var mob = z;
}
attack_mob(mob);
autos = autos - 1;
timer(6,autos,y,mob);
document.getElementById("autos").innerHTML = autos;
}
else if (autos == 0) {
document.getElementById("autotimer").innerHTML = "Ready";
document.getElementById("autos").innerHTML = y;
}
}
function timer(x,autos,y,mob) {
document.getElementById("autotimer").innerHTML = x;
if (x > 0) {
x = x - 1;
setTimeout("timer("+x+")",1000);
} else {
document.getElementById("autotimer").innerHTML = "Ready";
autoattack_mob(autos,y,mob);
}
}
// Auto Button code
<input type ='submit' value='Auto' onclick=autoattack_mob(".$eauserstats3[autos].",".$eauserstats3[autos].")>
// Timer and auto display...
print "<td>Auto Timer: <div id = 'autotimer'>Ready</div></td><td> Autos: <div id = 'autos'>".$eauserstats3[autos]."</div></td>";
Also, the Div's on the timer/auto display make the number show on the next line, if anyone knows how to fix that too, would help alot.
thanks for your input, however it has made no difference. the first fight takes place, the timer goes down, "autos" is reduced from its starting 20 to 19, , and still stops at "Ready" with 19 autos left. at this point the process should start all over again with 1 less total "autos" and keep doing so until it has 0 autos left.
Works for me, here is the complete code so you can compare.
Small changes:
Since I do not have the z variable, I just made it take a mob name from the HTML. I commented the attackmob function, since its undefined for me.
Well, there are some parts missing from your code that I cannot see. I do not know if anything from there is interfering with the rest of the script you posted here.
Check if there is something that is making x stay with a value of 0.
function attack_mob(box)
{
overlay_on();
$.ajax({
type: "POST",
data: $(box).serialize(),
url: "attackmob.php",
dataType: "html",
success:function(data){
var result = data.split("<sep></sep>");
$("#level_up").html(result[3]);
$("#main_place").html(result[0]+result[2]);
overlay_off();
}});
return false;
}
thats the other function thats any relation to this. box is the dropdown bar in which the "monstername" is gained, which is an ID compared to the db to get its stats.
attackmob.php is the actual fight code. determining the results of the battle.
if it'd help the test site is test.dragon-kingdoms.com make an account, log in. you have to do 1 fight normally for the variables of auto attack to be gained. then you can hit auto attack and see it work... and then fail.
Here. When autos comes back with a value of 19, it passes as true on the first if, but the second if validates it as false. Therefore, stopping all code execution and failing to call the function timer() again.
So, you can either get rid of the second if. Or use an else clause to make it continue with the function timer().
EDIT:
Indeed. That if is not present in your starting code example, so that is why it works for me.
Bookmarks