Discuss Scratch

deepkamat
Scratcher
10 posts

How to make a clone shoot a clone?

Hi everyone I am making a TD Game. In which I need help making a coned turret shoot a cloned enemy if its within range of the turret. I don't want it to switch between cloned enemies unless the first one is either out of range or destroyed

deck26
Scratcher
1000+ posts

How to make a clone shoot a clone?

Search for ‘nearest clone’ to see examples of this sort of thing.
deepkamat
Scratcher
10 posts

How to make a clone shoot a clone?

@deck26 I did that but it only points out to the nearest clone . I did not find anything related to range over there. I want it to point and shoot only if the enemy is within 100-150 pixels. Is there any way to do that?
deck26
Scratcher
1000+ posts

How to make a clone shoot a clone?

While you're working out the nearest clone it must be looking at distance. Shouldn't be hard to modify it to ignore any clones outside a certain range.
-ShadowOfTheFuture-
Scratcher
1000+ posts

How to make a clone shoot a clone?

deepkamat wrote:

@deck26 I did that but it only points out to the nearest clone . I did not find anything related to range over there. I want it to point and shoot only if the enemy is within 100-150 pixels. Is there any way to do that?

Assuming you have multiple cloned enemies, one way to do it is to store each clone's x/y position in a list:

when gf clicked // in enemy sprite
... // whatever cloning you need to do for the enemies
forever
delete (all v) of [enemies v]
broadcast [update enemy list v] and wait // the list is updated once every frame
end

when I receive [update enemy list v] // clones receive broadcasts, like parent sprites
if <...> then // whatever condition lets you tell that it's a clone, like a clone ID local variable or something
add (x position) to [enemies v]
add (y position) to [enemies v]
end

Then, the turret sprite clone loops through the list and checks every pair of items in it to see if they work in the distance formula.

when I start as a clone
set [i v] to [0]
repeat ((length of [enemies v]) / (2)) // you want to divide by 2, because the position of any enemy takes up two items in the list
set [distance v] to ([sqrt v] of ((((x position) - (item ((i) + (1)) of [enemies v])) * ((x position) - (item ((i) + (1)) of [enemies v]))) + (((y position) - (item ((i) + (2)) of [enemies v])) * ((y position) - (item ((i) + (2)) of [enemies v])))))
... // ^^^^^ basically, distance formula. (Dear god that block is long)
if <(distance :: variables) < [150]> then // if you want the max distance to be 150
... // nearest clone detection or whatever you want
end
end

Sorry if this was a bit confusing.

Last edited by -ShadowOfTheFuture- (Nov. 11, 2018 19:43:52)


<Insert uncreative signature here>









██       ██  ██            ██  ██       ██
██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
███ ███ ██ ████ ██ ███ ███
█████████ █████ █████ █████████

“Though the seasons come and go, and sunshine turns to snow, we will always have tomorrow up ahead.”
deepkamat
Scratcher
10 posts

How to make a clone shoot a clone?

@-ShadowOfTheFuture- I did not get what do you mean by the distance formula and I did not get what you mean by nearest clone detection. Can you explain please?
deck26
Scratcher
1000+ posts

How to make a clone shoot a clone?

deepkamat wrote:

@-ShadowOfTheFuture- I did not get what do you mean by the distance formula and I did not get what you mean by nearest clone detection. Can you explain please?
Distance, using Pythagoras, is the square root of (x difference squared plus y difference squared).

@ShadowOfTheFuture is leaving the nearest clone detection to you to provide.
-ShadowOfTheFuture-
Scratcher
1000+ posts

How to make a clone shoot a clone?

deepkamat wrote:

@-ShadowOfTheFuture- I did not get what do you mean by the distance formula and I did not get what you mean by nearest clone detection. Can you explain please?
Distance formula is basically Pythagorean theorem (© = square root of ((a)^2 + (b)^2), in which (a) and (b) are the two legs of a right triangle and © is the hypotenuse). In the special case of the distance formula which you use to find the distance between two points on a coordinate plane, the two legs of the right triangle ((a) and (b)) are the difference between the two x coordinates and the difference between the two y coordinates, and the hypotenuse © is the distance. (More information in the link I gave you)

I was assuming that you already had some form of detection for finding the nearest clone. If you don't, try searching up “nearest clone”, and several projects will come up. @TheLogFather made a project for detecting the nearest clone without using lists, for example. You can combine the scripts and make it so that the turret detects the nearest clone, but only among clones within 150 pixels.

<Insert uncreative signature here>









██       ██  ██            ██  ██       ██
██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
███ ███ ██ ████ ██ ███ ███
█████████ █████ █████ █████████

“Though the seasons come and go, and sunshine turns to snow, we will always have tomorrow up ahead.”
imfh
Scratcher
1000+ posts

How to make a clone shoot a clone?

Also check out this other tutorial by @TheLogFather which allows you to point to any clone, not just the nearest one.

Scratch to Pygame converter: https://scratch.mit.edu/discuss/topic/600562/
deepkamat
Scratcher
10 posts

How to make a clone shoot a clone?

How would I apply distance formula into scripts???
deck26
Scratcher
1000+ posts

How to make a clone shoot a clone?

deepkamat wrote:

How would I apply distance formula into scripts???
OK, we've pointed you to code to work out the nearest clone. Essentially this looks at each clone in turn and checks to see if it is closer than the nearest one found so far.

If you only want to shoot clones that are less than 150 pixels away you do the same thing but before you shoot you check the minimum distance that was found and only shoot if that value was less than 150.

Alternatively you could set a variable ‘clone-found’ to 0 before searching for the nearest clone and set it to the ID of the nearest one but only if it is within range. If having checked all clones the clone-found variable is still 0 you haven't found one to shoot at.

The nearest clone scripts go through each clone of spriteA and find the nearest clone of spriteB. If spriteA is your tower you get each tower to check for the nearest clone of the enemy, check whether the distance is OK and shoot if it is.

You need to understand how this works to use it. We can help you understnad but only if we know exactly where you're stuck.
deepkamat
Scratcher
10 posts

How to make a clone shoot a clone?

@deck26 I finally got it. Thanks everyone for their help. this really helped me.
wtc602
Scratcher
78 posts

How to make a clone shoot a clone?

This will be in the clone
forever
if <key [f] pressed?> then
add (x position) to [list v]
add (y position) to [list w]
broadcast [fire]
end



This will be in the thing that shoots the clone
when I receive [fire]
glide (3) secs to x: (item (1) of [list v] :: list) y: (item (1) of [list w] :: list)
delete ( 1) of [list v]
delete ( 1) of [list w]

Powered by DjangoBB