Jump to content

Render in a loop


LirycMoaners
 Share

Recommended Posts

Hi everyone,

 

I tried my best to find a solution for my problem but I'm still blocked with this, so now it's time to post !

I'm currently trying to code a serious game for programmation learning.

In this game, I need to write code's line and execute at the end and it will execute action on a character, step by step.

For example, if I need to move up on 5 tiles then left on 3 tiles I will write this :

character.move('up', 5);

character.move('left', 3);

I will click on execute and the character will move up on 5 then stop and after it will move left on 3.

 

My problem here is that I need a loop to move but when I put a loop in the update function, the game stops.

And that's normal because the render loop is stopped, and so the character can't reach his new position and we got an infinite loop.

 

So I need your help to find a solution about how to render my game in a loop inside the update function or to find a better solution.

 

Thanks everyone !

Link to comment
Share on other sites

  • 2 weeks later...

Hello,

I just had the same problem and I solved it using setInterval. I needed a goto position function, because I wanted to enable mouse controls.

Here is an example.

After the mouse event is called, I check where the player is going to move and call the moveToYInterval Function with setInterval.

if(directionY === 'up') {
	player.playAnimation([3,4]);
	yIntervall = setInterval(moveToYInterval, 200);

}

and then:

function moveToYInterval(){
	
	if(directionY === 'up') {

		if(player.y >= moveToY) {
				player.y -= 5;
		}
		else {
			clearInterval(yIntervall);
			player.stopAnimation();
			player.gotoAndStop(0);
			isMovingToPostion = false;
		}
	
	}
	else if(directionY === 'down') {
		
		if(player.y <= moveToY) {
				player.y += 5;
		}
		else {
			clearInterval(yIntervall);
			player.stopAnimation();
			player.gotoAndStop(0);
			isMovingToPostion = false;
		}
		
	}
	
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...