Discuss Scratch

Senneih_cram
Scratcher
18 posts

Detecting if a sprite is more likely north, south, east, or west ?

i've done the sprite, the animation for most, i made that my units can move thanks to the helping community of yours, and now i need help once more and maybe at last x)
My game is greatly inspired from the advance wars game (or fire emblem kinda) so my ennemy and i can only move tile per tile.
The problem here is that, i can't do an AI for an ennemy that can't even know if i'm below or above him and i don't really know if that's possible but i wish it is !
Here's my game: https://scratch.mit.edu/projects/416539753/editor/
mstone326
Scratcher
1000+ posts

Detecting if a sprite is more likely north, south, east, or west ?

You could compare x and y positions. If the xposition of the enemy is < your xposition then the enemy is to the left. If the yposition of the enemy is > than your ypostion the enemy is above you.
Senneih_cram
Scratcher
18 posts

Detecting if a sprite is more likely north, south, east, or west ?

i'll try what you said right now it should work , i'll make variable “for this sprite only” so if an ennemy is south-west, he should be gambling between going down and going left thanks in advance ^^
Senneih_cram
Scratcher
18 posts

Detecting if a sprite is more likely north, south, east, or west ?

It should work but there is still a problem about who should he traget, i feel like the easiest way would be to check who's distance is the smallest between the allies and that ennemy and then target the chosen ally but it looks complicated to add
1guy444
Scratcher
92 posts

Detecting if a sprite is more likely north, south, east, or west ?

Hello Senneih_cram!

Finding the distance between the allies and the enemy is pretty simple; it's the same as finding the distance between two points. For that, you can use the distance formula, which is d = \sqrt{(x_2 - x_1)^2 + (y_2-y_1)^2}. It looks complicated, but it's really not. (x_1, y_1) is the coordinate of the first sprite, and (x_2,y_2) is the coordinate pair for the second sprite.

In Scratch blocks it would look something like this
sqrtofx2-x1*x2-x1+y2-y1*y2-y1

Simply input the first sprite's screen coordinates with the coordinates of the sprite you want to compare to into this formula, and store it somewhere. Then do the same with the coordinates of all the other sprites you want to compare to, then take the smallest number of the lot, which is the shortest distance.

Hope this helps!

Last edited by 1guy444 (Aug. 11, 2020 11:55:27)

Senneih_cram
Scratcher
18 posts

Detecting if a sprite is more likely north, south, east, or west ?

But how can i make a command that chooses what is the smallest number ? (srry for the bit late reply)
1guy444
Scratcher
92 posts

Detecting if a sprite is more likely north, south, east, or west ?

Here's the breakdown; get two lists, and name one of them as “list with coords”. Name the other, “list with sprite names”. Then, use the formula above in a script, with (x1,y1) being the coordinates of the sprite that is the one being referenced, and (x2,y2) being the coordinates of every other sprite you want to compare to..
Then, add the result to the two lists, like this

adddistanceformularesulttolist with coordsaddnameofspriteornameofclonetolist with sprite names

Remember that the sprite name or clone name that you're adding to the second list should be one of the sprites that you're comparing to (x2, y2), not the sprite with (x1, y1).

After that, iterate through the list with coords to find the smallest number. This is fairly simple. Just make something like this:
definefindminimuminlistsetindexto0setsmallest number/minimumto240setsmallest number indexto0repeatuntilindex=lengthoflist with coordschangeindexby1ifitemindexoflist with coords<smallestnumber/minimumthensetsmallest number/minimumtoitemindexoflist with coordssetsmallest number indextoindexsettargettoitemsmallestnumberindexoflist with sprite names

The above script compares each item in the list to find the minimum number, and then records the index of that number so it can be used in the list with sprite names, which has the same order. I would recommend putting the above script in a custom block definition, and having the box ticked “run without screen refresh”.

Hope this helps!

Last edited by 1guy444 (Aug. 13, 2020 09:44:08)

Senneih_cram
Scratcher
18 posts

Detecting if a sprite is more likely north, south, east, or west ?

it works very well.. but :p it doesn't really figure out if the ally is north or south, east or west ^^ i probably made something wrong about the ally list (list with sprite names) since i juste typed their name on the correct order, but blue can't figure out if the target(coordinate) has more or less Z and Xcoordinate since it's just letters xD thx for your help so far btw, the more i learn, the more i'll be able to do in the future.

Last edited by Senneih_cram (Aug. 14, 2020 08:26:08)

1guy444
Scratcher
92 posts

Detecting if a sprite is more likely north, south, east, or west ?

No problem! It certainly is a useful tool to have under your belt. I think mstone326 has the answer for your north, south, east west problem. Have a nice day!
Senneih_cram
Scratcher
18 posts

Detecting if a sprite is more likely north, south, east, or west ?

i think i didn't explained well mb, the problem is that the list with sprite name only contains letters, you commands works very well, but in the result i don't have any coordinates to compare, i just have names, it can't be used for mstone326 solution :p
Senneih_cram
Scratcher
18 posts

Detecting if a sprite is more likely north, south, east, or west ?

but i bet i can figure out by myself from now on anyway, thx for helping, i don't know how to close a thread yay
1guy444
Scratcher
92 posts

Detecting if a sprite is more likely north, south, east, or west ?

Hehe, good luck with your project!

Last edited by 1guy444 (Aug. 14, 2020 08:41:43)

deck26
Scratcher
1000+ posts

Detecting if a sprite is more likely north, south, east, or west ?

Senneih_cram wrote:

but i bet i can figure out by myself from now on anyway, thx for helping, i don't know how to close a thread yay
Should be an option to close at the bottom of the page for the topic (for the owner and only after 24 hours).

Powered by DjangoBB