Selacius Posted July 10, 2010 Share Posted July 10, 2010 I have a function in my exploration system which I effectively want to use as a means to get the player to automatically move (somewhat like automatic cut scene). The code I am using is function movedown (num) { if (num > 0 ){ player.y++; draw_map (player.y, player.x); setTimeout(movedown(num-1), 3000) } } Which works, except that when I move onto the tile with the script, I immediately move the "num" of tiles down from my current position, instead of moving down in a stepwise fashion.Any hints? Link to comment Share on other sites More sharing options...
Synook Posted July 10, 2010 Share Posted July 10, 2010 When you write setTimeout(movedown(num-1), 3000), you are actually calling movedown() immediately, and assigning the return value (nothing) to be called after three seconds. However, since this causes recursion, you get movedown() being called again, as fast as possible, until num > 0 is false.The solution is to use a wrapper function: setTimeout(function() { movedown(num-1);}, 3000); Link to comment Share on other sites More sharing options...
Selacius Posted July 11, 2010 Author Share Posted July 11, 2010 Perfect. That works exactly how I want it. Will just need to adjust the delay. Now, my next question is, why would my eval statement not be working in this line of code: script = maps.scripts[player.y][player.x].split("|"); for (i=0; i < script.length; i++) { eval (script[i]); } The script variable is an array which will contain the functions which are to be automated (move left, down, right, up). So I want to loop through the length of this array and process the functions one at a time. Currently it only does the first function and then ends. Link to comment Share on other sites More sharing options...
thescientist Posted July 11, 2010 Share Posted July 11, 2010 are you sure script has more than one index? Link to comment Share on other sites More sharing options...
Selacius Posted July 11, 2010 Author Share Posted July 11, 2010 Yes, it certainly does. If I forgo the For statement and manually tell it to eval the two indices of the array I move on a left downward diagonal. Link to comment Share on other sites More sharing options...
thescientist Posted July 11, 2010 Share Posted July 11, 2010 what if you do something like this: script = maps.scripts[player.y][player.x].split("|");for (i=0; i < script.length; i++) { evalMe(i);};function evalMe(index){ eval(script[index]);}; Link to comment Share on other sites More sharing options...
Selacius Posted July 12, 2010 Author Share Posted July 12, 2010 Nope, that still doesn't work.:S Link to comment Share on other sites More sharing options...
Synook Posted July 12, 2010 Share Posted July 12, 2010 Try alerting out the string to be evaluated, to check whether it's what you think it is. Link to comment Share on other sites More sharing options...
Selacius Posted July 12, 2010 Author Share Posted July 12, 2010 Ive tried that in attempts to debug the script. When I alert it only pops up the first portion of the script variable (index 0) and does not show index 1. So its not proceeding onto the 2nd eval line. However when I remove eval from the for statement it alerts both index 0 and 1. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.