Reputation: 51
So I'm working on this project to develop a 2d game. In this game, each new level represents a city with 3 places to explore. The player will start in the center of the paths and can choose to enter each place by clicking on it. Since the path to each place is not a straight line, I have "mapped" them as a series of points, and the player should go in a straight line between them.
In this example, the player starts at the black circle, and to reach any location, it should be walking through the red dots. How do I make the player wait to reach the first dot before going to the second? and then waiting to reach the second to go to the third? And so on? This is an example of how I was trying to do it:
func goToNextPoint(p):
$player.move_and_collide($player.position.direction_to(paths[ep][op][p]-playerSize)*0.25)
while($player.position != paths[ep][op][p]-playerSize):
yield(get_tree().create_timer(0.2), "timeout")
if len(paths[ep][op]) < p+1:
goToNextPoint(p+1)
else:
return
This function uses the array called paths which for each map contains 3 other arrays, each with the dots that compose a path. the variables ep and op refers to which map and which place the player is going to.
When a player clicks on the location, this function is called with p=0 to make the player go to the first dot. While on the 3rd line should hold the function until the position is reached, but it does not work. The player simply tries to head straight to the last dot and instantly enters the location.
Is there a way to wait for that specific condition of reaching a position? Or maybe a better way to make the player move through the dots?
Upvotes: 2
Views: 405
Reputation: 33
You can try to make a multiple position 2d nodes, in the places where the red dots are. In the script group them in an array, make a function that gets the index of the player position, and you can either add one or subtract one. You get the element using this index from the array, and position the player to the position 2d node. Hope this helps.
Upvotes: 1