Best Way to search for a Sprite with the highest Y Value

Hello

I am trying to create a Tower Defense Game
For that i need an Algorithm that can detect the closest Enemy (just an ImageSprite) (with close i mean the highest Y Value)
I had three Ideas
1.
Comparing every Sprites Y Value with every single other Y Value to check for the highest
Problem:
It is very Slow especially if you have many Sprites (i tested it with 10 but in the end i want around 20 or 25)
And it uses A lot of Blocks (that can cause Memory Issues)

When a Sprite spawns (appears on the field and starts walking) its name is stored in a list and gets removed when it dies or reaches the end
when the Tower fires it takes the first Sprite from the list and targets it
Problem:
All Enemys need to have the same Speed so new Enemy Typs (like Archers) are not possible
I Tested it and after 200 - 800 Enemys something goes wrong (targets air or the wrong enemy but shortly after it fixes itself but still annoying)

Something Similar as in 2 but without a list and instead two Variables
but shortly after the start it always targets something completly different (again other enemys or air but it doesnt fix itself and stays broken) (i know that that is caused by a mistake in the code but it uses a lot of blocks and is very unorganized so i cant spot it)
Problems:
Uses a lot of blocks
All Enemys need to have the same Speed


I am using Idea 2 for my current attempt at an Tower Defense Game but as metioned its not a very good system
it works but i cant add new Things to it (like Archers or Slow Moving Enemys or very Fast Moving Enemys)

Do you have an Idea how i can fix that problem?

Example using Balls instead of Sprites:
setAllSpriteSpeed

800 sprites? It takes time to traverse a List.

App Inventor is not a Game Engine. Its worst feature is the graphics are s l o w compared to compilers using java like Android Studio. You get what you paid for and it is a great deal. :slight_smile:

What is the best way to search? You have several reasonable ideas. Create a test Project using different methods and tell us which is best?

What is the advantage from using Balls?

I wanted to test the Algorithm so i let the Game run and after around 800 dead enemys i stopped the test (i had a counter that counted how many Sprites were "killed")

I know that it is not a Game Engine but i just want to practice my understanding of programming before i start to learn how to create an actual game with unity

after many hours (probably like 30 to 40) of creating three (not finished) Tower Defense Games i only came up with these three Ideas and none of them is good enough
thats why i asked it here in hope that someone has a better solution

How do you sort?

javascript sorting generally is faster than using Math Blocks

  • easier to Provide an example
  • the center is the center if you set that Property whilst with Sprites the Y value for a sprite is the value at the lower left corner of the ImageSprite.
  • does not require a graphic
  • might make rendering of an enemy faster than using a Sprite since no special image is used.

What does a better solution mean to you Michael? The ONLY way you can conduct tests in my opinion is to create a test Project using each of the methods to determine the highest Y value and time the results for comparison.

I tested each of the ideas but either they are to slow to messy or just incorret

i now used the - For each item in list - block but the result is very confusing :sweat_smile:

Try
maxValueinList

wait i am blind
i just now see the blue thing :man_facepalming:

One way is to create a List of all the Y values of all your Sprites.

YvalueList

I use Balls. :slight_smile: to create the maxYList which is just a List of Y values.

it doesnt work

The Operation add items to list cannot accept the Arguments 0, 147.5

So your list of Sprites cannot accept a 0. Don't use Y value zeros. or use a different method.

The example works fine for me. :slight_smile: It won't accept an empty list

Note that due to a design decision in App Inventor you will need to consider Balls and ImageSprites separately, but here is the code to find ImageSprite with the largest Y value (toward the bottom of the screen):

If you want to mix sprite types, you'll need a second version to consider Ball.Y rather than ImageSprite.Y, and then of those return the one with the largest Y.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.