Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I managed to make a simple loop to move my character, onmousedown I move the character diagonally to where the user clicked. It's working but my problem is that I don't know how to prevent more loops to start over the first if the user clicks more than once.

Right now if you click more than once the character moves faster and faster since more loops are running at the same time.

This is the loop:

(function move() {
    if(cframes<frames) {
    cframes = cframes+1;
        character.style.left=parseInt(character.style.left)+pxsecx;
        character.style.top=parseInt(character.style.top)+pxsecy;
        setTimeout(move, 20);
}
})()
}

How do I prevent this loop from starting if it already has? or stop it and start another? Thanks

share|improve this question
4  
Honestly, I don't want to help you with this, as your problem is simple, but going down the wrong path for making anything more complex than pong. What you really need is a main loop that calls Update() and takes care of all the updating of the program in that one file, and a Draw() which draws everything to the canvas. –  AttackingHobo May 6 '11 at 3:58
1  
Quit using the (function)() pattern, it can be handy for library stuff, but you don't need it, and you probably don't have the understanding to gain any advantage by using it. –  eBusiness May 6 '11 at 14:59

3 Answers 3

One way of solving it is to not call the move function when clicking but only set the desired destination where you want to move the character (and trigger a main loop if it's for some reason not already running) and let the main loop move the character towards the destination.

See tips for writing the game loop question and this StackOverflow question titled best way for simple game-loop in Javascript.

share|improve this answer

setTimeout() returns a timer ID that can be passed to clearTimeout() later to cancel it. Store it in a variable somewhere and cancel the old one in the event callback. See https://developer.mozilla.org/en/window.setTimeout for more details.

share|improve this answer

Do not allow click() handler to cause movement until some time passes

Basically do not allow clicking faster than some value and allow it only once at the same time. :)

There is how You can do it:

Create global variable:

var not_yet; // it represents delay
// * when someone clicks it is set to nonzero
// * timer will decrease it until zero
// * if it is zero, then it will wait for user click
// * if it is zero and user clicks, then character is moved and it is set to nozero

Add a timer, which You will call each few miliseconds, and which will do this:

if (0 < not_yet) { 
// it is a clock which is decreasing since the previous click
// this way together with this (1) check You prevent from
    not_yet -= 1;
}

In handler of the click event :

if (0 < not_yet) { // (1)
    return; 
// user clicked few milliseconds ago
// it is not right time yet
} else {
    // do whatever You did there previously

    not_yet = 5; // start the timer and set it to delay for 5* units 
    // *5 is arbitrary, set it to whatever value will work the best for You
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.