This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-01-18 08:36:48

hello12345678910
Scratcher
Registered: 2009-07-11
Posts: 100+

Source code help!!

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---------


http://tinyurl.com/8yt32o9 http://tinyurl.com/6tgwp5r || Fish = F+I+S+H = 6+9+19+8 = 42<<The answer to Life, the Universe and Everything

Offline

 

#2 2011-01-18 09:00:58

hello12345678910
Scratcher
Registered: 2009-07-11
Posts: 100+

Re: Source code help!!

bump


http://tinyurl.com/8yt32o9 http://tinyurl.com/6tgwp5r || Fish = F+I+S+H = 6+9+19+8 = 42<<The answer to Life, the Universe and Everything

Offline

 

#3 2011-01-18 09:59:27

markyparky56
Scratcher
Registered: 2008-03-20
Posts: 1000+

Re: Source code help!!

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.


http://j.mp/jgVnTq
Check out my game engine development site: NewDawn I'm a Level 171 Scratcher.I am http://bit.ly/nkvLNT

Offline

 

#4 2011-01-18 11:11:26

hello12345678910
Scratcher
Registered: 2009-07-11
Posts: 100+

Re: Source code help!!

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?


http://tinyurl.com/8yt32o9 http://tinyurl.com/6tgwp5r || Fish = F+I+S+H = 6+9+19+8 = 42<<The answer to Life, the Universe and Everything

Offline

 

#5 2011-01-18 11:15:33

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Source code help!!

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

 

#6 2011-01-18 11:19:26

hello12345678910
Scratcher
Registered: 2009-07-11
Posts: 100+

Re: Source code help!!

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)


http://tinyurl.com/8yt32o9 http://tinyurl.com/6tgwp5r || Fish = F+I+S+H = 6+9+19+8 = 42<<The answer to Life, the Universe and Everything

Offline

 

#7 2011-01-18 11:24:15

hello12345678910
Scratcher
Registered: 2009-07-11
Posts: 100+

Re: Source code help!!

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

Code:

:sp|(self distanceTo: sp < lastDist) ifTrue: [lastDist _ self distanceTo: sp. closestSprite _ sp].

??????????????


http://tinyurl.com/8yt32o9 http://tinyurl.com/6tgwp5r || Fish = F+I+S+H = 6+9+19+8 = 42<<The answer to Life, the Universe and Everything

Offline

 

#8 2011-01-18 11:57:07

rubiks_cube_guy238
Scratcher
Registered: 2009-07-02
Posts: 100+

Re: Source code help!!

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

The glass is never half full nor half empty; it is twice as large as it needs to be.

Offline

 

#9 2011-01-18 12:14:18

hello12345678910
Scratcher
Registered: 2009-07-11
Posts: 100+

Re: Source code help!!

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.


http://tinyurl.com/8yt32o9 http://tinyurl.com/6tgwp5r || Fish = F+I+S+H = 6+9+19+8 = 42<<The answer to Life, the Universe and Everything

Offline

 

Board footer