Jump to content

Slow Down Execution of Script


Selacius

Recommended Posts

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...