Projectile Targeting System and Projectile Animations

I am currently trying to create a Tower Defense Game but whatever i try i am not able to create a working Projectile
I have a list of all the Enemys that are on the field (in order since the enemy first on the field is the first in the list and when he dies or reaches the end he will be removed and the next enemy in the list is the target)(all enemys have the same speed)
the problem with that system is that i havent found a way to get the coordinates of the enemy without having to add hundreds of blocks (with that the system wouldnt be as modular as before)
(local variable "Target" -> select item from list "x" with Index 1)
with that i know the name of the enemy but nothing else so i cant use the - Move to X/Y block and the point towards block (with speed and intervall and so) doesnt want to work in combination with the variable "Target"
If possible i want to keep the System with the list since it uses not a lot of blocks and i want to practice it (its the first time i am using the lists)
and the only other way that i can think of how it could work compares every single enemys Y to all others Y to find out whos the closest (in one of my other attempts at a Tower Defense Game i used that system and the Targeting worked but i quickly ran into memorie issues and i had over 400 blocks or so just for that and the animation or movement of the sprite wouldnt work since it was to slow/inacurate)

(if you need/want the project files i will give them to you but the game isnt ment to be public i just want to practise with all the different mechanics so the grahpics might not match up on your screen and i used
a combination of German and English words so you might not understand everything)

Has Anybody an idea to help me with that problem?

Possibly Projectile motion

I dont need something that complicated (that i dont understand :sweat_smile:)
i just want a Sprite to move to the nearest enemy and collide with it
The Projectile Motion requires manual input and has a view from the side (i want a view from above)
Just as in my old attempt
TowerDefense.aia (60.5 KB)
but better (without crashing with an animation and with less blocks)

Have you read Creating Animated Apps .?

The tutorial shows developers how to move a Sprite

both manually and 'automatically'. The advice is a bit simpler than described in the Projectile motion link.

You need

.. that is possible.

I havent read that

but i know the basics of the sprite movement
my problem is the connection between the Targeting System and the Projectiles Movement

Do you know a way how to create a working Projectile (in connection with searching for the nearest enemy)?

Then you really probably don't 'know the basics of sprite movement'

After you have read and understand that tutorial; look for the Scott Ferguson animation samples at his (now defunct) library that might have an example similar to what you want to do.
Redirecting to Google Groups . Several years ago, Scott was the expert :slight_smile: in simple App Inventor animation.

I am not aware of any example. What you want to do seems possible. You will need to calculate the distances of all the enemy sprites from your cannon or gun first; calculate the nearest and then provide code for your projectile to 'attack' the nearest. This requires simple geometry skills, the Math Blocks, the mentioned tutorial and some patience. :cry:

thanks for the help
i will read the tutorial and then see what i can do

Good luck. Here is an App Inventor example showing how to calculate the distance between to objects (balls, sprites) on a Canvas. The link downloads a pdf showing the technique.

1 Like

one last quick question

if i have a list that contains Sprites
and i want to have the X and Y coordinates from the first sprite in the list
how should i do that?

realizing the X and Y coordinates provided are the lower left corner of the Sprite (not the center) you can use the Any component Block like this:


If you need the center of the Sprite, you have to do some math based on the Sprite dimensions.

i meant a list where you dont know how many sprites or what sprites are in the list
for example
List : Sprite4
Sprite 7
Sprite 2
List : Sprite 8
Sprite 1
Sprite 5
Sprite 9
Sprite 10

it doesnt matter matter what the name of the sprite is where it is on the canvas or how many Sprites are in the list
it will always give me the X and Y coordinates of the first Sprite in the list


i thought about something like that but for some reason it always changes the heading to 1 and not to the targetet sprite

i asked for the x and y coordinates because i thought of the
Move to X/Y Block but Heading would also work

:cry: The advice was provided because you

of the Sprite.

This example works with 3 randomly placed Balls. It will 'shoot' from any of the Balls in the list at Ball 2; something similar can be used with Sprites to shoot from Ball2 to any of the Balls in the List by adjusting the code.

Experiment :slight_smile:

you did everything right i just wanted to mention that in case if you were confused since i asked for the X/Y coordinates but used the heading block

Bad arguments to PointTowards
The operation PointTowards cannot aceept the arguments: ,[1]

so with that i have the same problem as before that it will output 1 instead of the object

ok wow i am so stupid

i never checked the enemy spawn system since it worked
at midnight or so i hastely gave the enemys health so i can test the projectile and since than i had problems with the targeting system
i accidently always set the variable that spawns the enemy to 1 (the hp) and than added the variable to the list so i would always add 1 instead of the enemys name
thats why it always was something with 1 and i thought it had something to do with the index 1
thats why it never worked

it finally works!!

thank you for your help and time