0

I'm making a tower desence game for a game jam in HTML5. I have a subroutine for the AI of the towers. However, for some reason, this function call without a loop ends up in an infinite loop. It only happens when there are at least two towers.

function aiTower(id){
    if(id === 1)alert("towerId = 1 call 1");
    var l = zamerajCiel(id);
    if (l !==  null) towers[id].shoot(l.x,l.y);
    if(id === 1)alert("towerId = 1 call 2");
};

The loop that calls it:

function aiLoop(){
    for(i=0;i<enemies.length;i++){
        aiMon(i);
    }
    for(i=0;i<towers.length;i++){
        aiTower(i);
            if(i === 1)alert("towerId = 1 call 3");
    }
}

The debug msgs are alternating "towerId = 1 call 1" and "towerId = 1 call 2", so the problem probably isn't in the aiLoop(). Also, I have used a regexp to search my code for aiTower(). These were the only two occurences.

1
  • I think we will need the code for .shoot and zamerajCiel' as well. Just an advice: use console.log('msg u want to see')`, if you use firebug it will be such a great logging tool. getfirebug.com/logging Commented Nov 10, 2012 at 20:37

1 Answer 1

2

Make i local to the function by providing var i. It is likely that you modify it elsewhere.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.