Hi. I am working on a game project that requires the player to move around the screen to catch food. There are 20 pieces of food scattered around the canvas. However, there should be two mice also wandering around and chasing the player to eat all the food first. As I want the game to look playable and more natural, I don't want to let them glide in random positions or go to the closest food as they may outrun the player, so I want the mice to move to the second closest piece of food from them every few seconds. I've tried a few times but I still cannot manage to do a code like that. Can anyone help me with this problem? Thanks!
This example might help with regard to calculating 'distance' between sprites.
Remember that the x,y coordinates reported for a sprite are the upper left hand corner of the sprite unless you write some code to determine the 'center' of the sprite.
Wouldn't that starve the mice?
They would never reach food, always turning away from food as they get close to it, dying from Attention Deficit Disorder?
Thank you for the suggestion, appreciate it very much. I just want to ask if I have to do the same process for 20 times as I have 20 food? Also, should I put it in a list so that it can sort out the order of numbers of the items put in it? Thanks
Yes, calculate the distance to all the other sprites and add them to a List of distances.
Yes. The distances to each sprite you calculate should go into a List so you can determine the second closest. You need to be able to associate the second closest distance with the Sprite that is second closest. You could do part of that by using a List or Pairs perhaps.
How? complicated. First determine all the distances of the sprites; sort them closest to farthest; create a new List called sortedSpriteDistances perhaps,. Item 1 is the nearest. Then select item 2 which is the second nearest, what you want.