Can anybody help me make a block that looks like this:
(closest sprite)
So that it will report the name of the closest sprite?
I already have the Blockspec, i just need help with the code.
----Help is appreciated---------
Offline
bump
Offline
One way to do this with blocks that are already there would be to put all the sprites names in a list and have it run through it to find the closest one using the distance to [ ] block. Sorry I can't help you with making it a real block.
Offline
markyparky56 wrote:
One way to do this with blocks that are already there would be to put all the sprites names in a list and have it run through it to find the closest one using the distance to [ ] block. Sorry I can't help you with making it a real block.
That would work, but how can i make it so that it will do it for all sprites?
Offline
hello12345678910 wrote:
markyparky56 wrote:
One way to do this with blocks that are already there would be to put all the sprites names in a list and have it run through it to find the closest one using the distance to [ ] block. Sorry I can't help you with making it a real block.
That would work, but how can i make it so that it will do it for all sprites?
True. If I were you i'd use squeak for it.
Something along the lines of:
sprites do: [:sp|(self distanceTo: sp < lastDist) ifTrue: [lastDist _ self distanceTo: sp. closestSprite _ sp].
^ clostestSprite.
Offline
IDEA!!!
i already have a (sprite name) block, so in every sprite, i'll just put:
|WhenFlagClicked|
If <"name's" contains (sprite name)>
add (sprite name) to "name's"
End
then i'll add the distances to a list, sort it out, then find the name of the lowest one.
Thanks Markyparky56!!
Last edited by hello12345678910 (2011-01-18 11:19:50)
Offline
LS97 wrote:
hello12345678910 wrote:
markyparky56 wrote:
One way to do this with blocks that are already there would be to put all the sprites names in a list and have it run through it to find the closest one using the distance to [ ] block. Sorry I can't help you with making it a real block.
That would work, but how can i make it so that it will do it for all sprites?
True. If I were you i'd use squeak for it.
Something along the lines of:
sprites do: [:sp|(self distanceTo: sp < lastDist) ifTrue: [lastDist _ self distanceTo: sp. closestSprite _ sp].
^ clostestSprite.
LS97, it's telling me that the "sprites" at the beginning is an unknown var. is there another way to make all the sprites perform
:sp|(self distanceTo: sp < lastDist) ifTrue: [lastDist _ self distanceTo: sp. closestSprite _ sp].
??????????????
Offline
getClosestSprite
| minSprite minDistance |
minDistance _ 1000.
ScratchSpriteMorph
allInstancesDo: [:sprite | sprite = self
ifFalse: [(self distanceTo: sprite objName)
< minDistance
ifTrue:
[minDistance _ self distanceTo: sprite objName.
minSprite _ sprite objName]]].
^ minSpriteOffline
rubiks_cube_guy238 wrote:
Code:
getClosestSprite | minSprite minDistance | minDistance _ 1000. ScratchSpriteMorph allInstancesDo: [:sprite | sprite = self ifFalse: [(self distanceTo: sprite objName) < minDistance ifTrue: [minDistance _ self distanceTo: sprite objName. minSprite _ sprite objName]]]. ^ minSprite
Thanks. i see how to make all the other sprites do that now.
Offline