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

#1 2011-10-17 05:51:10

csn
New Scratcher
Registered: 2011-10-17
Posts: 27

Show/hide blocks depending on the spritename

Hello,

is there a possibility to show/hide a block depending on the name of the sprite?

I tried with
'spriteName' = objName ifTrue ...
in ScratchSpriteMorph class block specs but objName is not known in this class. So my question is probably if there an easy way to let objName be known in this class?

Thanks csn

Offline

 

#2 2011-10-17 06:38:12

nathanprocks
Scratcher
Registered: 2011-04-14
Posts: 1000+

Re: Show/hide blocks depending on the spritename

can u post the full blockspec and code?


http://carrot.cassiedragonandfriends.org/Scratch_Signature/randomsig.php
http://trinary.site40.net/images/scratchrank.php?username=nathanprocks&display=small

Offline

 

#3 2011-10-17 06:58:58

csn
New Scratcher
Registered: 2011-10-17
Posts: 27

Re: Show/hide blocks depending on the spritename

Thank you for your reply. Here is part of the code:

In Scratch-Objects | ScratchSpriteMorph | block specs | blockspecs

blockSpecs

| blocks |

('NXT' = objName )
    ifTrue: [
        blocks _ #(
            'motion'
                ('control NXT motion'            -    nxtControl)
                ('start turning %o %O'            -    nxtStartM:d: L forward)
        ).
    ].


The problem is: objName is not known in "block specs"
"block specs" is a class of ScratchSpriteMorph and objName is defined as a variable for instances in ScratchSpriteMorph.

But to be honest, I don't know the difference. I'm just look at other instances an try to figure out my new blocks by looking at other blocks.

cns

Offline

 

#4 2011-10-17 07:06:37

nathanprocks
Scratcher
Registered: 2011-04-14
Posts: 1000+

Re: Show/hide blocks depending on the spritename

csn wrote:

Thank you for your reply. Here is part of the code:

In Scratch-Objects | ScratchSpriteMorph | block specs | blockspecs

blockSpecs

| blocks |

('NXT' = objName )
    ifTrue: [
        blocks _ #(
            'motion'
                ('control NXT motion'            -    nxtControl)
                ('start turning %o %O'            -    nxtStartM:d: L forward)
        ).
    ].


The problem is: objName is not known in "block specs"
"block specs" is a class of ScratchSpriteMorph and objName is defined as a variable for instances in ScratchSpriteMorph.

But to be honest, I don't know the difference. I'm just look at other instances an try to figure out my new blocks by looking at other blocks.

cns

hmmm... sorry but i am not really good at squeak... u might have to wait until someone better than me answers this  tongue


http://carrot.cassiedragonandfriends.org/Scratch_Signature/randomsig.php
http://trinary.site40.net/images/scratchrank.php?username=nathanprocks&display=small

Offline

 

#5 2011-10-17 14:51:16

csn
New Scratcher
Registered: 2011-10-17
Posts: 27

Re: Show/hide blocks depending on the spritename

The problem is more complex. I tried to compare objName to a string in an instance code block:

self say: objName.
    t1 <- 'NXT'.
    [t1 = objName] value
    ifFalse: [ ...

The first line leads to bubble with NXT. So objName is known and has the expected value. But the comparison is always False.

Is objName not a string? Is the comparison o.K.? Is there a possibility to check the sprite name?
I should know a little bit more about squeak and smalltalk I assume.
csn

Offline

 

#6 2011-10-17 15:50:01

csn
New Scratcher
Registered: 2011-10-17
Posts: 27

Re: Show/hide blocks depending on the spritename

I figured it out for instances but don't ask for explanation:

    t1 <- #NXT.
    t2 <- objName asSymbol.
t1 = t2   
ifFalse: [...]
ifTrue: [...].

Now the value of t1 = t2 is True.

But the problem I started with remains. In Scratch-Objects | ScratchSpriteMorph | block specs | blockspecs >objName< is unknown.

Does anybody know how to change it?

Offline

 

#7 2011-10-17 15:51:43

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Show/hide blocks depending on the spritename

It's an instance variable, not a class variable.  wink


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#8 2011-10-17 16:23:18

csn
New Scratcher
Registered: 2011-10-17
Posts: 27

Re: Show/hide blocks depending on the spritename

bbbeb wrote:

It's an instance variable, not a class variable.  wink

Ok. the real question is:

Can I get the sprites name as a class variable?

csn

Offline

 

#9 2011-10-17 16:27:41

csn
New Scratcher
Registered: 2011-10-17
Posts: 27

Re: Show/hide blocks depending on the spritename

csn wrote:

t1 <- #NXT.
    t2 <- objName asSymbol.
t1 = t2   
ifFalse: [...]
ifTrue: [...].

I'm learning.
String comparison:

('NXT' compare: objName) =2
ifFalse: [...]
ifTrue: [...].

is also working as expected.

csn

Offline

 

#10 2011-10-17 16:33:53

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

Re: Show/hide blocks depending on the spritename

Unfortunately your work goes beyond the ScratchSpriteMorph class.

You need to trace back to the method that creates all the blocks, and there you can add a condition for the objName (since the method I'm talking about is a ScriptableScratchMorph which has objName defined as an instance var).

Offline

 

#11 2011-10-17 16:47:09

csn
New Scratcher
Registered: 2011-10-17
Posts: 27

Re: Show/hide blocks depending on the spritename

LS97 wrote:

You need to trace back to the method that creates all the blocks, and there you can add a condition for the objName (since the method I'm talking about is a ScriptableScratchMorph which has objName defined as an instance var).

But objName is still an instance var and I can't use it in the class part of ScriptableScratchMorph either. The blockspecs are defined in the class part.

I can use objName in ScriptableSpriteMorph in instances, I tried it. I think this is because ScriptableSpriteMorph seems to be a subclass of ScriptableScratchMorph.

But I'm just sort of guessing, there is no deeper knowledge.
csn

Offline

 

#12 2011-10-18 11:27:13

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

Re: Show/hide blocks depending on the spritename

csn wrote:

LS97 wrote:

You need to trace back to the method that creates all the blocks, and there you can add a condition for the objName (since the method I'm talking about is a ScriptableScratchMorph which has objName defined as an instance var).

But objName is still an instance var and I can't use it in the class part of ScriptableScratchMorph either. The blockspecs are defined in the class part.

I can use objName in ScriptableSpriteMorph in instances, I tried it. I think this is because ScriptableSpriteMorph seems to be a subclass of ScriptableScratchMorph.

But I'm just sort of guessing, there is no deeper knowledge.
csn

You're right about not being able to access objName from the class method of ScriptableScratchMorph, because it's instance. But what I meant is looking at the method that creates the blocks -- and that's an instance method.

EDIT
Maybe I wasn't clear enough. The various blockSpecs methods are simply lists of blocks, they are not methods. They are then used by the block creating method as a list to reference!

Last edited by LS97 (2011-10-18 11:28:14)

Offline

 

#13 2011-10-18 12:26:41

csn
New Scratcher
Registered: 2011-10-17
Posts: 27

Re: Show/hide blocks depending on the spritename

Thanks a lot! It finally works.

In ScriptableScratchMorph | blocks | viewerPageForMotion
I inserted:

    ('NXT' compare: objName) = 2
    ifTrue: [
        (self blocksFor: 'motionNXT') do: [...].
    ]
    ifFalse: [
        (self blocksFor: 'motion') do: [ ...].
    ].
instead of

    (self blocksFor: 'motionNXT') do: [...].

Then I made an extra blocklist for motionNXT and it works. Now I just have to find the place where the block-color is set. I have seen it these days. I will get this.

Thanks a lot LS97!

csn

Offline

 

#14 2011-10-18 12:30:35

csn
New Scratcher
Registered: 2011-10-17
Posts: 27

Re: Show/hide blocks depending on the spritename

And the color is set in ScriptableScratchMorph | block specs | blockColorFor:

csn

Offline

 

#15 2011-10-18 12:35:01

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

Re: Show/hide blocks depending on the spritename

Very good! That's exactly what I was suggesting!

As an added bonus, you might want to say

if name is NXT, add blocks for nxt motion.
regardless of what sprite it is, add the default motion category.

to avoid having to duplicate the motion category  smile

Offline

 

Board footer