www.webdeveloper.com
Results 1 to 9 of 9

Thread: please help

  1. #1
    Join Date
    Jul 2012
    Posts
    5

    please help

    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.

  2. #2
    Join Date
    Jul 2007
    Posts
    386
    The reason is due to the fact that you forget to pass all variable back to timer when you reset the setTimeout.

    Those are self enclosed variables, if you do not pass them again, they are simple lost.

    Try it this way:
    Code:
    setTimeout(function(){timer(x, autos, y, mob)}, 1000);
    Also:
    Code:
    x = x - 1;
    can be rewritten
    Code:
    x--;

  3. #3
    Join Date
    Jul 2012
    Posts
    5
    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.

  4. #4
    Join Date
    Jul 2007
    Posts
    386
    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.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
        <meta http-equiv="Content-Language" content="en"/>
        <script type="text/javascript">
    function autoattack_mob(autos,y,mob)
    {
    if (autos > 0) {
    var mob = document.getElementById("monstername").value;
    //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--;
    setTimeout(function(){timer(x, autos, y, mob)}, 1000);
    } else {
    document.getElementById("autotimer").innerHTML = "Ready";
    autoattack_mob(autos,y,mob);
    }
    }
        </script>
        <title></title>
    </head>
    <body>
    
    <input type="submit" value="Auto" onclick="autoattack_mob(6, 1)"/><br/><br/><br/>
    
    Auto Timer: <span id="autotimer">Ready</span><br/>
    Autos: <span id="autos"></span><br/><br/><br/>
    
    <span id="monstername">Argos</span>
    
    </body>
    </html>

  5. #5
    Join Date
    Jul 2012
    Posts
    5
    oddly enough, i have your script running perfectly on my server (as a seperate thing) aswell. however it doesnt seem to work when its with my code.

  6. #6
    Join Date
    Jul 2007
    Posts
    386
    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.

  7. #7
    Join Date
    Jul 2012
    Posts
    5
    Code:
    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.

  8. #8
    Join Date
    Jul 2007
    Posts
    386
    After visiting your site and doing a play by play of the events happening in the code I found where things are getting stumped.

    Code:
    if(autos > 0){
        if(autos == y){
            var mob = document.getElementById("monstername").value;
            attack_mob(mob);
            autos = autos - 1;
            timer(6,autos,y,mob);
            document.getElementById("autos").innerHTML = autos;		
        }
    }
    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.

  9. #9
    Join Date
    Jul 2007
    Posts
    386
    Replace the hidden span with this:

    Code:
    <form id="autoattack" method="get" action="" style="display:none">
        <input type="hidden" name="attack" value="<?php echo $variable_for_attack_mode ?>" readonly="readonly"/>
        <input type="hidden" name="monstername" value="<?php echo $variable_for_monstername ?>" readonly="readonly"/>
        <input type="hidden" name="verify" value="<?php echo $variable_for_verify_code ?>" readonly="readonly"/>
    </form>
    Modified attackmob function:
    Code:
    function attack_mob(box)
    {           
        overlay_on();
        if(!box)
            box = '#autoattack';
        $.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;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles