How can I get a bullet to find the nearest enemy?

id: 749441

category: Help with Scripts

posts: 2

Create-Scratch101 wrote:

How can I get a bullet to find the nearest enemy?
I have been wondering for my simple game, Monster Brawl. Find it here.
MineTurte MineTurte loading

Create-Scratch101 wrote:

Create-Scratch101 wrote:

How can I get a bullet to find the nearest enemy?
I have been wondering for my simple game, Monster Brawl. Find it here.
For enemy sprite:
when green flag clicked
set [ clones] to [0] //Do NOT set this sprite to "this sprite only"
set [ clone] to [0] //SET to "this sprite only"
repeat (50)
add [placeholder] to [distances]
add [placeholder] to [CloneXs]
add [placeholder] to [CloneYs]
end

when I receive [ makeEnemy]
change [ clones] by (1)
create clone of [ myself]

when I start as a clone
set [ clone] to (clones)
forever
replace item ( clone) of [distances] with (distance to [ player])
replace item ( clone) of [CloneXs] with (x position)
replace item ( clone) of [CloneYs] with (y position)
broadcast [ logDistances]
end

when I receive [ logDistances] //Forever run this on clones
set [ slot] to [0]
findClosestDistances

define findClosestDistances // run without screen refresh
set [ currentClosest] to [999]
repeat (length of [ distances] :: list)
change [ slot] by (1)
if <(item ( slot) of [distances] :: list) < (currentClosest)> then
set [ currentClosest] to (item ( slot) of [distances] :: list)
end
end
broadcast [ bullet]


For bullet sprite:
when I receive [ bullet]
go to x: (item ( currentClosest) of [CloneXs] :: list) y: (item ( currentClosest) of [CloneYs] :: list)

Hope this helps!! (Also not sure if this works perfectly but whatever. I already spent like 15 minutes making this lol.)